This actually caused me brain freeze for a few minutes, and the reason why one should never use signal()
in this day and age only grew stronger in me.
First of all, from the man pages for signal()
The behavior of signal() varies across
UNIX versions, and has also varied
historically across different versions
of Linux. Avoid its use: use
sigaction(2) instead.
and further down :
- If the disposition is set to a function, then first either the
disposition is reset to SIG_DFL, or
the signal is blocked (see Portability
below), and then handler is called
with argument signum. If invocation
of the handler caused the signal to be
blocked, then the signal is unblocked
upon return from the handler.
In the original Unix systems, when a handler was installed, the disposition was reset to SIG_DFL, did not block incoming signals of the same type, and then it ran the handler function. System V provided this, and the linux kernel does the same.
This means that, once the code is run on a linux system, once second exception is called, it will exit directly.
Now to the fun part. BSD tried to improve this behaviour. From the man pages again:
On BSD, when a signal handler is
invoked, the signal disposition is not
reset, and further instances of the
signal are blocked from being
delivered while the handler is
executing.
And since mac osx is partly based on BSD, once the code is run on a mac osx, once second exception is called, it will be pending and wait for the handler of the first exception to exit. But since you will never exit, you have a deadlock.
Thats why one should use sigaction()
instead and never signal()
.
Now to some tips:
Handlers should be short, and return quickly. If you are performing calculations and calling other functions you are probably doing something wrong. Signals are not a substitute for an event driven framework.
Calling functions that are not async-safe is bad. Consider what would happen if an exception happened during a call to fprintf
, and inside the handler fprintf
was called again. Both the signal handlers and the programs data could be corrupted since they operate on the stream itself.
Some more reading : "Do" and "Don't" inside A Signal Handler