From e0ad39a489b3398f8208713bf728db0cb11475b0 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Tue, 3 Feb 2026 22:22:53 +0000 Subject: Handle SIGTERM for graceful shutdown with systemd Listen for both SIGINT (Ctrl+C) and SIGTERM (systemd) signals to ensure graceful shutdown cleanup runs when stopping the service via systemd. Previously, only SIGINT was handled, causing purgatory state and rejected events cache to be lost on every systemd restart. Now both signals trigger the cleanup code that saves state files and removes placeholder refs. Fixes issue 0f73 --- src/main.rs | 49 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6c9da05..dd2c903 100644 --- a/src/main.rs +++ b/src/main.rs @@ -207,18 +207,45 @@ async fn main() -> Result<()> { info!("Starting HTTP server on {}", config.bind_address); // Run server until shutdown signal, then cleanup - tokio::select! { - result = http::run_server( - config, - relay_with_db.relay, - relay_with_db.database, - metrics, - purgatory, - ) => { - result? + #[cfg(unix)] + { + use tokio::signal::unix::{signal, SignalKind}; + let mut sigterm = signal(SignalKind::terminate())?; + + tokio::select! { + result = http::run_server( + config, + relay_with_db.relay, + relay_with_db.database, + metrics, + purgatory, + ) => { + result? + } + _ = signal::ctrl_c() => { + info!("Received SIGINT (Ctrl+C), cleaning up..."); + } + _ = sigterm.recv() => { + info!("Received SIGTERM, cleaning up..."); + } } - _ = signal::ctrl_c() => { - info!("Received shutdown signal, cleaning up..."); + } + + #[cfg(not(unix))] + { + tokio::select! { + result = http::run_server( + config, + relay_with_db.relay, + relay_with_db.database, + metrics, + purgatory, + ) => { + result? + } + _ = signal::ctrl_c() => { + info!("Received SIGINT (Ctrl+C), cleaning up..."); + } } } -- cgit v1.2.3