Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shared condition variable is invalid after the waiting process crashes #109

Open
ianosd opened this issue Feb 21, 2020 · 2 comments
Open

Comments

@ianosd
Copy link

ianosd commented Feb 21, 2020

I have two processes, synchronized using a condition variable in shared memory.
If the process that calls wait crashes, the notifying process sometimes blocks in the notify call.
This happens on windows, using visual C++.

Here's a minimal example:

#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <boost/interprocess/sync/interprocess_condition.hpp>

#include <iostream>

struct shared_data
{
   boost::interprocess::interprocess_mutex mutex;
   boost::interprocess::interprocess_condition condition;

   bool test_bool = false;
};


int main(int argc, char *argv[])
{
    using namespace boost::interprocess;

    if (argc == 1) {
        shared_memory_object shm(create_only, "MySharedMemory", read_write);

        shm.truncate(sizeof(shared_data));
        mapped_region region(shm, read_write);
        void* addr = region.get_address();
        shared_data* data = new (addr) shared_data;

        while (true) {
            scoped_lock<interprocess_mutex> lock(data->mutex);
            while (!data->test_bool) {
                data->condition.wait(lock);
            }
            std::cout << "test_bool became true" << std::endl;
            data->test_bool = false;
        }
    }
    else {
        shared_memory_object shm(open_only, "MySharedMemory", read_write);
        mapped_region region(shm, read_write);
        shared_data* data = static_cast<shared_data*>(region.get_address());
        while (true) {
            {
                scoped_lock<interprocess_mutex> lock(data->mutex);
                data->test_bool = true;
            }
            std::cout << "Entering notify" << std::endl;
            data->condition.notify_one();
            std::cout << "Exiting notify" << std::endl;
        }
    }
}

As far as I understand, this is because the waiting thread is supposed to unlock the internal mutex of the condition, but it doesn't get to. (The generic emulation implemetation is being used)
Any suggestions to work around this?
A straightforward thing to do would be to allow for a timeout in the notify method. What do you think?

@igaztanaga
Copy link
Member

Doesn't BOOST_INTERPROCESS_ENABLE_TIMEOUT_WHEN_LOCKING help?

@wangbinio
Copy link

wangbinio commented Dec 10, 2023

@ianosd ,hello,i meet the same question. have you solve it?

#200 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants