Skip to content

Fix segfaults when interrupting bayestar.localize

BAYESTAR now terminates gracefully if you interrupt it by typing control-C or sending the process in which it is running a SIGINT signal. Previously, there was a bug in the signal handling code that caused it to segfault on keyboard interrupt.

Note that if you invoke BAYESTAR multiple times in different threads of the same process, then only one of the invocations will stop early due the interrupt, because signal handlers are process-wide.

Also, add a unit test.

Flaws in the old signal handling code

The old signal handling code attempted to allow multiple BAYESTAR invocations to stop early by keeping the interrupted flag on the caller's stack and saving a thread-local pointer to the flag. This would segfault when interrupted because newly spawned OpenMP threads would have their own uninitialized thread-local pointer, and process-directed signals are handled by any randomly selected thread.

New signal handler design

There are very few synchronization primitives that are safe to call from a signal handler. Instead, we have a process-wide mutex lock that only allows a single thread to install a signal handler.

Edited by Leo P. Singer

Merge request reports