Why would signals not get caught? Why would signals not get caught? unix unix

Why would signals not get caught?


I don't know Objective C but I do have experience with signal handlers, so I'll answer as if for POSIX and C.

Calling any function that is not documented as "async signal safe" in a signal handler is a risk and should be avoided. You cannot make any assumptions about the stack or any other state when the signal handler is called. The stack might even be "trashed" (in the middle of creation or destruction of a frame) when your signal handler is called. Your libraries might have inconsistent state when your signal handler is called.

Declare a volatile flag (int) that gets checked in your event loop, or whatever, to see if it has changed. The signal handler should ONLY set that flag and return, nothing else. (Unless your platform does SVR4-style signals, in which case you also need to re-install the signal handler within the signal handler.)

The log messages and other activity in response to the signal should be done by whatever code checks the flag and processes the event implied by the flag.

The symptoms you are seeing might not be due to the library calls in the signal handler (my money would honestly be on gdb interaction), but I definitely recommend taking all library calls out of the signal handler.


Shawn, so you're using CZMQ, which diverts the signals for its own purposes whenever you do a zctx_new(). My advice is if possible to let CZMQ do its thing and trap the interrupt using the mechanisms it provides, which are the global zctx_interrupted variable, and in any blocking ZMQ call, a null return and EINTR error code.


It turns out to have something to do with czmq's zsocket_new. Switching to zctx__socket_new fixed it. I haven't yet dug in to find out exactly what is happening.