Unsafe
Signal handlers are very simple, and they are simply wrong. Here's the SEGV handler:
void default_segv() { throw std::runtime_error("Segmentation fault"); }
This is pretty illegal, at least on POSIX. Throwing an exception from the signal handler is a very, very bad idea.
Adding
So why is this a bad idea?
With SIGALRM, this is worse than a bad idea; this behavior is undefined because alarms are asynchronous. With SIGSEGV and SIGBUS, this is a very bad idea. With other signals, this is just a bad idea. This can sometimes work. In other cases, this is not so. The results can be quite disastrous when magic is not working.
First, take a look at SEGV. One common cause of segmentation disruptions and bus errors is stack splitting. There is no stack to relax if this was the cause of the signal. throw will try to expand the stack, which will raise another SEGV. Now what? On my computer, this is a disaster.
Regardless of the signal, throwing inside the signal handler is unsafe with respect to RAII, because free() (and therefore delete ) is not safe to call in the context of the handler. There are many functions that are unsafe to call from a signal handler context. Everything that happens after throw in the handler is executed from the context of the signal handler, because throw not returned from the handler. throw bypass returns.
These unsafe calls and unsafe untying mean that a new signal can be picked up while processing an old signal. This recursive signal is very problematic. For example, on my computer, the signal changes to SIGSTOP. The program does not exit; it does not drop the kernel. He just hangs there, constantly frozen, until I kill him or restart the car.
source share