C++ / Python Wrapping and system_signal_exception


While working on a C++ program that made use of a Python wrapped library to start a subprocess, when the subprocess crashed I got a boost::detail::system_signal_exception at the C++ level even though the executing context was still on the python layer. How is that possible?

My more experienced colleagues immediately told me to look at how python handles SIGCHLD. I did so. First, what is SIGCHLD? On Wikipedia it says:

The SIGCHLD signal is sent to process parent when it exits, is interrupted, or resumes after being interrupted. By default the signal is simply ignored.

On the Python Signals page it says:

signal.SIG_DFL: This is one of two standard signal handling options; it will simply perform the default function for the signal. For example, on most systems the default action for SIGQUIT is to dump core and exit, while the default action for SIGCHLD is to simply ignore it.

So Python is likely ignoring the SIGCHLD signal generated by the subprocess crash. If my colleagues pointed me toward that direction then I supposed that C++ must not be ignoring it. I made a new function to trap it at the Python level.

def sigchld_handler(signum, frame):

which I assigned as SIGCHLD handler.

signal.signal(signal.SIGCHLD, sigchld_handler)

Et voilà. Problem solved. Python now “handles” the signal (by doing nothing on it) and doesn’t let it go up to the C++ level. An alternative would have been to handle and ignore the signal at that level, but catching it at the Python level in my case was direct and easy.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.