Skip to main content

Module signals

Module signals 

Source
Expand description

Cross-platform signal handling: SIGINT, SIGTERM, SIGHUP. Cooperative shutdown wiring, stated per platform.

Every platform reaches the SAME handle_first_signal body, so the observable contract — SHUTDOWN flag, cancellation token, stderr notice, JSON envelope with code: 19, forced exit 130 on the second event — does not vary. What varies is which OS events can reach it:

  • Unix (Linux, macOS): SIGINT through the ctrlc crate; SIGTERM and SIGHUP through signal-hook. SIGPIPE is reset to its default disposition in main, so a closed stdout pipe kills the process with the conventional exit 141.
  • Windows: SetConsoleCtrlHandler covers CTRL_C_EVENT, CTRL_BREAK_EVENT, CTRL_CLOSE_EVENT, CTRL_LOGOFF_EVENT and CTRL_SHUTDOWN_EVENT. There is no SIGTERM, no SIGHUP and no SIGPIPE: console-close/logoff/shutdown are the closest equivalents of a termination request, and the exit-141 half of the contract is produced in main by classifying the stdout write error as ErrorKind::BrokenPipe instead of by a signal.
  • Anything else: SIGINT only, via ctrlc.

Windows does NOT go through ctrlc. Console control handlers are called last-registered-first and ctrlc’s handler consumes every control type, so registering both would either double-count one Ctrl+C — which the second-event rule turns into an immediate exit 130 — or leave one of them dead. One handler owns the console, and it is this module’s.

Functions§

register_shutdown_handler
Registers the global shutdown handler for every event the platform offers.