Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Throwing an exception from within a signal handler

Tags:

We have a library that deals with many aspects of error reporting. I have been tasked to port this library to Linux. When running though my little test suite, one of the tests failed. A simplified version of the test appears below.

// Compiler: 4.1.1 20070105 RedHat 4.1.1-52 // Output: Terminate called after throwing an instance of 'int' abort  #include <iostream> #include <csignal> using namespace std;  void catch_signal(int signalNumber) {     signal(SIGINT, SIG_DFL);     throw(signalNumber); }  int test_signal() {     signal(SIGINT, catch_signal);      try     {         raise(SIGINT);     }     catch (int &z)     {         cerr << "Caught exception: " << z << endl;     }     return 0; }  int main() {     try     {         test_signal();     }     catch (int &z)     {         cerr << "Caught unexpected exception: " << z << endl;     }     return 0; } 

My expectation is that the Caught exception: message will be displayed. What actually happens is that the program terminates as no catch handler appears to be present for the thrown int.

There are a few questions on SO that seem related. I found a number of Google pages that were related. The 'wisdom' seems to boil down to.

  1. Ya can't throw exceptions from signal handlers, cause the signal handler runs with its own stack, so there are no handlers defined on it.
  2. Ya can throw exceptions from signal handlers, just reconstruct a fake frame on the stack, and you are good to go.
  3. Ya, we do it all the time. It works for me on platform X
  4. Ya, that used to be available with gcc, but doesn't seem to work any more. Try the -fnon-call-exceptions option, maybe that will work

    The code works as expected on our AIX/TRU64/MSVC compiler/environments. It fails in our Linux environment.


I am looking for suggestions to help resolve this issue so the library behavior on Linux will match my other platforms, or some sort or workaround that might achieve the same sort of functionality.
Letting the program core dump on signal, is not a viable option.

like image 754
EvilTeach Avatar asked Nov 11 '09 20:11

EvilTeach


People also ask

What happens when a catch handler throws an exception?

The code which can throw any exception is kept inside(or enclosed in) a try block. Then, when the code will lead to any error, that error/exception will get caught inside the catch block.

Can a signal handler be interrupted by another signal?

However, your handler can still be interrupted by delivery of another kind of signal. To avoid this, you can use the sa_mask member of the action structure passed to sigaction to explicitly specify which signals should be blocked while the signal handler runs.

Can signal handlers be nested?

Signal handlers aren't nested, and a handler can't be called a second time while a current instance is still being executed. If func is SIG_DFL, the default action for the condition is taken. If the default action is to stop the process, the execution of that process is temporarily suspended.

How do you handle Sigbus?

From a signal handler, it is better to use the POSIX sigsetjmp() and siglongjmp() so that any signals that were blocked by the C runtime or operating system just before calling the signal handler gets reset to the values they had upon returning to the saved context.


1 Answers

Signals are totally different than C++ exceptions. You can't use a C++ try/catch block to handle a signal. Specifically, signals are a POSIX concept, not a C++ language concept. Signals are delivered asynchronously to your application by the kernel, whereas C++ exceptions are synchronous events defined by the C++ standard.

You are quite limited in what you can do portably in a POSIX signal handler. A common strategy is to have a global flag of type sig_atomic_t which will be set to 1 in the signal handler, and then possibly longjmp to the appropriate execution path.

See here for help writing proper signal handlers.

like image 79
Charles Salvia Avatar answered Sep 20 '22 17:09

Charles Salvia