alexrp’s blog

ramblings usually related to software

SDB 1.2 Released

SDB 1.2 has been released. The change log is here. A binary release is available, though you can also just build from source using the v1.2 Git tag.

The only real highlight of this release is that SIGINT (the signal sent when you press Ctrl-C) is now handled using Mono.Unix.UnixSignal from the Mono.Posix assembly. This is significant because it means SDB will no longer potentially crash when interrupted this way.

Previously, SDB’s SIGINT handler was a managed method. This was problematic because managed/native trampolines contain all sorts of signal-unsafe code that can make the I/O layer in Mono blow up in various interesting ways. There’s no real workaround for this. Managed signal handlers just aren’t practical.

The way UnixSignal solves it is by having signal handlers in a helper library written in C. Those are then registered from managed land. This way, there’s no interference from Mono in the process. When a signal is delivered, the helper library increments a counter and returns. Managed land can then call WaitOne on the UnixSignal which observes any change in the counter and returns. It is then clear that a signal has been delivered, and managed land can do whatever it needs to do.

In effect, signal handling happens synchronously, rather than asynchronously, which is how signals are normally delivered. This does mean that managed land has to have a separate thread that waits on the UnixSignal, or at least poll it with a timeout every so often. On the other hand, this mechanism is much, much safer than attempting to write signal-safe code in a signal handler.

In the case of SDB, a separate thread is started when the Ctrl-C handler is set. This thread just waits indefinitely on the UnixSignal for SIGINT. This thread is simply aborted when the handler is unset.