Unix signal handling

From Rust Community Wiki

Unix signals are a mechanism for IPC which can be hard to handle correctly and safely. If you're implementing something like a Unix-like daemon, then handling Unix signals correctly may be quite important since daemons, by convention, take certain actions when they receive certain signals.

Unix (and maybe Windows)[edit]

The crates which are discussed in this section will work (or are intended to work) on Unix-likes and POSIX-compliant systems. They may also provide varying degrees of support for the Windows family of operating systems.


If you want to handle Unix signals, and only Unix signals, then Cargo vec.svgsignal-hook is likely your best bet. It is well-documented and provides different mechanisms for handling signals, each of which is geared towards different kinds of applications. It is also the most downloaded crate on crates.io out of all the crates discussed on this page.

The following mechanisms are available in Cargo vec.svgsignal-hook :

  • Registering a function to be run when a signal is received. The register function is unsafe.
  • Registering an Arc<AtomicBool> whose value is set to true when the signal is received.
  • Registering an Arc<AtomicUsize whose value is set to a value, which you decide upon registration, when the signal is received.
  • A pipe which has both ends in your application. When a signal is received, one byte (whose value is arbitrary) is written to the write end of the pipe.
  • Iterators.
  • Integration with Cargo vec.svgmio and Cargo vec.svgtokio (hidden behind feature flags).

General-purpose alternatives[edit]

These crates try to let you handle more than just SIGINT.

  • Cargo vec.svgasygnal - Async signal-handling. Advertises support for CTRL + C signals, which likely means SIGINT.
  • Cargo vec.svgsignal-simple - Completely undocumented. No examples. Dependency of Cargo vec.svgterm-handler .
  • Cargo vec.svgsignalbool - Set a flag when a signal is received. Uses an internal static AtomicUsizeThis links to official Rust documentation to store bitflags representing the set of signals which have been recieved. The library's SignalBool struct only stores a bitmask of the set of signals it handles. Compiles on Windows, but can only handle CTRL_C_EVENT and CTRL_BREAK_EVENT (disguised as SIGINT).

SIGINT/CTRL+C handlers[edit]

These crates are mostly interested in handling SIGINT.

  • Cargo vec.svgasync-ctrlc - Async wrapper around Cargo vec.svgctrlc .
  • Cargo vec.svgctrlc - Handles SIGINT and optionally SIGTERM (through the "termination" feature). Handles CTRL_C_EVENT and CTRL_BREAK_EVENT on Windows. Has the most downloads on crates.io after Cargo vec.svgsignal-hook .
  • Cargo vec.svgctrlc_fnonce - Tiny wrapper around Cargo vec.svgctrlc which lets you provide a FnOnce closure which is called before process::exitThis links to official Rust documentation is called.

Inactive crates[edit]

These are crates which have received no updates on crates.io for over a year.

Deprecated crates[edit]

These are crates which will receive no further development and should not be used in new projects.

Windows only[edit]

The following crates technically don't do anything in regard to handling Unix signals, but they do offer functionality which is somewhat similar.

  • Cargo vec.svgwintrap - Handle CTRL_C_EVENT, CTRL_BREAK_EVENT, CTRL_CLOSE_EVENT and WM_CLOSE.