Skip to main content

sqlite_graphrag/
signals.rs

1//! Cross-platform signal handling: SIGINT, SIGTERM, SIGHUP.
2
3use std::sync::atomic::Ordering;
4
5/// Registers the global shutdown handler for Ctrl+C / SIGTERM / SIGHUP.
6///
7/// First signal: sets [`SHUTDOWN`](crate::SHUTDOWN) flag, cancels the global
8/// cancellation token and emits a best-effort notice on stderr.
9///
10/// Second signal: calls [`std::process::exit(130)`] for immediate termination
11/// following Unix convention (128 + SIGINT=2) — with ZERO I/O on that path.
12///
13/// # G42/S8 — panic-free by contract
14///
15/// The pre-v1.0.79 handler used `eprintln!` (second signal) and
16/// `tracing::warn!` (first signal). When the parent shell dies the CLI is
17/// reparented to PID 1 and stderr becomes a CLOSED pipe; `eprintln!` then
18/// panics with `BrokenPipe`, which under `panic = "abort"` becomes the
19/// SIGABRT observed on the "ctrl-c" thread (G42/C2 crash report). This
20/// handler therefore:
21/// - writes the first-signal notice with `writeln!` and IGNORES any I/O
22///   error (`let _ =`), never panicking;
23/// - performs NO I/O at all on the forced-exit path.
24///
25/// BrokenPipe on stdout/stderr elsewhere is handled by resetting SIGPIPE
26/// to its default disposition in `main` (clean exit 141, Unix convention).
27pub fn register_shutdown_handler() {
28    // SIGINT: ctrlc crate (cross-platform, the only signal that works on
29    // both Unix and Windows without a tokio runtime).
30    if let Err(e) = ctrlc::set_handler(move || {
31        handle_first_signal("SIGINT", 2);
32    }) {
33        tracing::warn!(target: "signals", error = %e, "SIGINT handler registration failed");
34    }
35
36    // SIGTERM + SIGHUP: signal-hook (Unix only; Windows uses TerminateProcess
37    // for SIGTERM equivalents and has no SIGHUP).
38    #[cfg(unix)]
39    {
40        use std::sync::mpsc;
41        let (tx, rx) = mpsc::channel::<i32>();
42
43        let mut signals = match signal_hook::iterator::Signals::new([
44            signal_hook::consts::SIGTERM,
45            signal_hook::consts::SIGHUP,
46        ]) {
47            Ok(s) => s,
48            Err(e) => {
49                tracing::warn!(target: "signals", error = %e, "SIGTERM/SIGHUP handler registration failed");
50                return;
51            }
52        };
53
54        // Detached thread: lives until process exit. The kernel kills it
55        // automatically on process termination. We do NOT join it because
56        // that would require the CLI to wait for an indeterminate signal.
57        std::thread::Builder::new()
58            .name("sqlite-graphrag-sigterm".into())
59            .spawn(move || {
60                for sig in signals.forever() {
61                    if tx.send(sig).is_err() {
62                        break;
63                    }
64                }
65            })
66            .expect("failed to spawn SIGTERM/SIGHUP handler thread");
67
68        // Drain thread: blocks on the channel and calls the same handler
69        // used by the SIGINT path. Synchronous main() can't await this,
70        // but the channel is bounded so a 100ms wait is fine.
71        std::thread::Builder::new()
72            .name("sqlite-graphrag-sigterm-drain".into())
73            .spawn(move || {
74                while let Ok(sig) = rx.recv() {
75                    let (name, number) = match sig {
76                        libc::SIGTERM => ("SIGTERM", 15u8),
77                        libc::SIGHUP => ("SIGHUP", 1u8),
78                        _ => continue,
79                    };
80                    handle_first_signal(name, number);
81                }
82            })
83            .expect("failed to spawn SIGTERM drain thread");
84    }
85}
86
87/// First-signal handler shared by both SIGINT (via  crate) and
88/// SIGTERM/SIGHUP (via signal-hook).
89///
90/// Idempotent: only the first invocation does work. The Ctrl+C handler is
91/// synchronous (no tokio runtime is built in the LLM-only main path).
92/// The SIGTERM/SIGHUP task is async but the underlying work is atomic via
93/// the  fetch_add pattern.
94fn handle_first_signal(signal_name: &'static str, signal_number: u8) {
95    let prev = crate::SIGNAL_COUNT.fetch_add(1, Ordering::AcqRel);
96    if prev != 0 {
97        // Second signal: forced shutdown, NO I/O (G42/S8).
98        std::process::exit(130);
99    }
100    crate::SHUTDOWN.store(true, Ordering::Release);
101    crate::SIGNAL_NUMBER.store(signal_number, Ordering::Release);
102    crate::cancel_token().cancel();
103
104    // Best-effort stderr notice: closed pipe must NEVER abort (G42/S8).
105    use std::io::Write;
106    let _ = writeln!(
107        std::io::stderr(),
108        "shutdown signal received ({signal_name}); finishing current operation gracefully"
109    );
110
111    // GAP-002 (v1.0.82): emit JSON envelope to stdout before exit so that
112    // piped consumers receive a parseable error with `code: 19`
113    // (SHUTDOWN_EXIT_CODE) instead of an empty stdout that triggers
114    // a parse error. Best-effort: if stdout is closed, writeln fails
115    // silently.
116    let envelope = format!(
117        "{{\"error\":true,\"code\":19,\"message\":\"shutdown signal received; operation cancelled by {signal_name}\",\"signal\":\"{signal_name}\",\"graceful\":true}}"
118    );
119    let mut stdout = std::io::stdout().lock();
120    let _ = writeln!(stdout, "{envelope}");
121    let _ = stdout.flush();
122}
123
124#[cfg(test)]
125mod tests {
126    /// G42/S8 regression guard: the SHARED `handle_first_signal` function
127    /// (called by both the SIGINT ctrlc closure and the SIGTERM/SIGHUP
128    /// signal-hook drain) must not contain `eprintln!` or `tracing::warn!`
129    /// — both can panic (and abort under `panic = "abort"`) when stderr
130    /// is a closed pipe in an orphaned process.
131    #[test]
132    fn handler_source_has_no_panicking_io() {
133        let source = include_str!("signals.rs");
134        // The shared first-signal body starts at `fn handle_first_signal`
135        // and ends at the closing brace of the function. We locate the
136        // start of the next free-standing function or the test module
137        // as the boundary.
138        let body_start = source
139            .find("fn handle_first_signal(")
140            .expect("handle_first_signal must exist");
141        let after_body = source[body_start..]
142            .find("\nfn ")
143            .or_else(|| source[body_start..].find("\n#[cfg(test)]"))
144            .expect("body boundary not found");
145        let body = &source[body_start..body_start + after_body];
146        assert!(
147            !body.contains("eprintln!"),
148            "handle_first_signal must not use eprintln! (BrokenPipe panic, G42/C2)"
149        );
150        assert!(
151            !body.contains("tracing::"),
152            "handle_first_signal must not use tracing (stderr I/O can panic, G42/C2)"
153        );
154        assert!(
155            body.contains("let _ = writeln!"),
156            "first-signal notice must be a best-effort write"
157        );
158        assert!(
159            body.contains("std::process::exit(130)"),
160            "forced-exit path must remain in the shared handler"
161        );
162    }
163
164    /// GAP-002 (v1.0.82) regression guard: the JSON envelope must use
165    /// the deterministic SHUTDOWN_EXIT_CODE (19) so LLM agents can
166    /// branch on a single code regardless of the triggering signal.
167    #[test]
168    fn envelope_uses_shutdown_exit_code() {
169        let source = include_str!("signals.rs");
170        // The envelope format string contains "code":19.
171        assert!(
172            source.contains("\\\"code\\\":19"),
173            "shutdown envelope must embed SHUTDOWN_EXIT_CODE = 19"
174        );
175    }
176
177    /// GAP-002 (v1.0.82) regression guard: `AppError::Shutdown` is the
178    /// canonical error variant for shutdown. Constants and i18n are
179    /// wired in lock-step — if SHUTDOWN_EXIT_CODE drifts away from 19,
180    /// this test fails.
181    #[test]
182    fn shutdown_exit_code_is_19() {
183        use crate::constants::SHUTDOWN_EXIT_CODE;
184        assert_eq!(SHUTDOWN_EXIT_CODE, 19);
185    }
186}