Skip to main content

polyc_runtime/
signals.rs

1//! Signal-driven graceful shutdown (PRD ยง11).
2
3use tokio::signal::unix::{SignalKind, signal};
4use tokio_util::sync::CancellationToken;
5
6/// Spawn a task that cancels `token` on SIGTERM or SIGINT, and logs SIGHUP
7/// (config reload is not yet implemented).
8///
9/// # Panics
10///
11/// Panics if the process cannot install the SIGTERM/SIGINT/SIGHUP handlers
12/// (e.g. the signal already has a conflicting handler). This is a fatal
13/// startup condition.
14pub fn spawn_handler(token: CancellationToken) {
15    tokio::spawn(async move {
16        let mut term = signal(SignalKind::terminate()).expect("install SIGTERM handler");
17        let mut int = signal(SignalKind::interrupt()).expect("install SIGINT handler");
18        let mut hup = signal(SignalKind::hangup()).expect("install SIGHUP handler");
19        loop {
20            tokio::select! {
21                _ = term.recv() => { tracing::info!("received SIGTERM"); token.cancel(); break; }
22                _ = int.recv() => { tracing::info!("received SIGINT"); token.cancel(); break; }
23                _ = hup.recv() => tracing::info!("received SIGHUP (config reload not yet implemented)"),
24            }
25        }
26    });
27}