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):
SIGINTthrough thectrlccrate;SIGTERMandSIGHUPthroughsignal-hook.SIGPIPEis reset to its default disposition inmain, so a closed stdout pipe kills the process with the conventional exit 141. - Windows:
SetConsoleCtrlHandlercoversCTRL_C_EVENT,CTRL_BREAK_EVENT,CTRL_CLOSE_EVENT,CTRL_LOGOFF_EVENTandCTRL_SHUTDOWN_EVENT. There is noSIGTERM, noSIGHUPand noSIGPIPE: console-close/logoff/shutdown are the closest equivalents of a termination request, and the exit-141 half of the contract is produced inmainby classifying the stdout write error asErrorKind::BrokenPipeinstead of by a signal. - Anything else:
SIGINTonly, viactrlc.
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.