Stiffstream / sobjectizer

An implementation of Actor, Publish-Subscribe, and CSP models in one rather small C++ framework. With performance, quality, and stability proved by years in the production.
https://stiffstream.com/en/products/sobjectizer.html
Other
481 stars 47 forks source link

How to safely catch and forward a OS signal (SIGINT, SIGHUP, etc) #47

Closed jcarrano closed 2 years ago

jcarrano commented 2 years ago

I need to be able to stop the environment when the application receives a SIGINT/SIGQUIT and send a message to a component to reload the configuration when SIGHUP is received.

Doing this seems to work:

  auto stop_app = [](int signal) -> void {
      main_env->stop();
  };
...
std::signal(SIGTERM, stop_app);

However, reading the code in environment_t::stop() I suspect it is not safe to call from the context of an async signal. The same goes for sending a message to a mbox/mchain, AFAIU there is the possibility of a deadlock if the signal just happens to be delivered while the message box mutex is being held.

How can OS signals be handled safely. I can only think of a few options:

eao197 commented 2 years ago

Hi!

You can find an approach that we used earlier in our arataga project:

Signal handling loop: https://github.com/Stiffstream/arataga/blob/b6e1720dd2df055949847425f12bb1af56edbe83/arataga/main.cpp#L602-L639 The core part of the main function: https://github.com/Stiffstream/arataga/blob/b6e1720dd2df055949847425f12bb1af56edbe83/arataga/main.cpp#L866-L877

Some of approaches you mentioned should also work and don't seem to be complex in implementation, for example:

But this case:

seems to be overkill in implementation complexity.

jcarrano commented 2 years ago

Thanks for the quick response. I had failed to notice that one can use a custom loop through a wrapped env!