icinga2 icon indicating copy to clipboard operation
icinga2 copied to clipboard

Implement synchronous signal handling in umbrella/worker processes

Open jschmidt-icinga opened this issue 1 year ago • 7 comments

This PR is an attempt to fix #9676.

Problem

As documented in the issue linked above, on the current master, Signal handling is done in asynchronous handlers installed with sigaction(). As per the POSIX standard, these handlers aren't allowed to use any functions that aren't listed as async-signal-safe, which we currently violate by calling functions such as Log() that perform allocations using operator new().

I've had difficulty reproducing the original problem, even when adding multiple big allocations in the event loops. But even if we can't easily reproduce it, doing these things in signal handlers is still incorrect and should be fixed.

Considerations

There are two potential solutions to this problem. Either remove all non-async-signal-safe functions from the handlers and make them as simple as possible, or make the signal handling synchronous instead, as suggested in #9676 by using sigtimedwait().

Currently there is additional effort involved in setting atomic variables (which is necessary because all threads can potentially receive the signal) that get then queried in the processes main event loops.

Considering this additional effort in re-synchronizing the results from signal handling, the synchronous approach seemed simpler and actually less invasive if we want to still keep a way of logging inside of signal handlers.

The asynchronous handlers for SIGABRT will remain as its critical to what it does.

Implementation

I moved blocking the relevant signals to earlier in DeamonCommand::Run() and now we leave them blocked, instead of unblocking them after forking into "umbrella" and worker processes and installing signal handlers instead. Except for SIGHUP, which gets set to be ignore (SIG_IGN) for the worker, same as before.

From there, on the "umbrella"-side, pending signals are polled individually directly where needed, removing the necessity for all of the global state variables. https://github.com/Icinga/icinga2/blob/42999027f0c49001d37b3d97927112337458132b/lib/cli/daemoncommand.cpp#L472-L477

However on the worker side, this is not easily possible because the state variables (static members of Application:: in this case) are shared with the WIN32 code-paths and have other input sources as part of the regular control flow. Therefore a WorkerSignalHandler(), similar to the old one in lib/cli/daemoncommand.cpp was added to lib/base/application.cpp and replaces the Utility::Sleep() in Application::RunEventLoop() on non-WIN32 systems. https://github.com/Icinga/icinga2/blob/42999027f0c49001d37b3d97927112337458132b/lib/base/application.cpp#L474-L478

The function Application::GetPendingSignal() contains all the platform-y ugliness to support MacOS and OpenBSD, so the rest of the code doesn't have to deal with it. It also supports being called with an infinite timeout and zero timeout.

Testing

Manual testing has been done to verify that the signals involved in the umbrella/worker setup and termination (SIGUSR1, SIGUSR2, SIGTERM, SIGINT, SIGHUP) are processed and propagated as expected.

In the future (or pending discussion in the context of this PR), adding automated test cases may be a good idea but considering how entangled these classes and functions currently are this would be difficult without refactoring into more self-contained parts first.

jschmidt-icinga avatar Apr 28 '25 13:04 jschmidt-icinga

Well, I've not considered OpenBSD just not implementing POSIX (again)...

It seems you're meant to use sigwait() in combination with timer_settime() on OpenBSD. I will look into it.

jschmidt-icinga avatar Apr 28 '25 14:04 jschmidt-icinga

FreeBSD should work btw: https://man.freebsd.org/cgi/man.cgi?query=sigwaitinfo&sektion=2&format=html

Al2Klimov avatar Apr 28 '25 14:04 Al2Klimov

I've just pushed a new version where I addressed some of @Al2Klimov's comments and also added some additional clarification and validation for the timeout passed to GetPendingSignal().

jschmidt-icinga avatar Apr 30 '25 08:04 jschmidt-icinga

Another update that now replaces the timeout value in GetPendingSignal() with std::chrono::microseconds in a variant with bool to signify infinite or zero wait.

Also fixed another small oversight where EINTR wasn't handled for sigwaitinfo(). I don't know how relevant that even is, but now it's there.

jschmidt-icinga avatar Apr 30 '25 12:04 jschmidt-icinga

@Al2Klimov We're also using the PID for the other signals, even SIGTERM on the worker side.

But I get what you mean. And there's already helper classes that would make this much simpler. I will talk to the others and see what's their opinion about this. If they think it's worth the effort I'll get to work towards a separate PR, since that would be far outside the scope of this one anyway.

jschmidt-icinga avatar May 05 '25 07:05 jschmidt-icinga

separate PR, since that would be far outside the scope of this one anyway

Maybe.

Maybe not, remember where this discussion started: https://github.com/Icinga/icinga2/pull/10423#discussion_r2066724052.

Sure, it's an option to keep those two implementations for now – IF you test the final PR version on all different OS to be supported.

Al2Klimov avatar May 05 '25 08:05 Al2Klimov

Sure, it's an option to keep those two implementations for now – IF you test the final PR version on all different OS to be supported.

I didn't necessarily mean that I want this PR to be merged first. In fact, it makes little sense to merge this if we decide on something else. What I meant is that if we end up deciding on a different approach, it should be implemented in a new PR (with this one being closed as a consequence).

jschmidt-icinga avatar May 05 '25 08:05 jschmidt-icinga

It has become clear that this minimal, messy, platform-dependent solution isn't satisfactory. At some point in the future I'll make another attempt at this based on boost::asio::signal_set and local socket pair, which should get rid of all the sleeps in the main event loops of both umbrella and workers. In the meantime I'm closing this PR.

jschmidt-icinga avatar Jul 23 '25 12:07 jschmidt-icinga