From 2fabc975b7ae66bdcd7bb49a4b1d5131f0f246f1 Mon Sep 17 00:00:00 2001 From: internationalcrisis Date: Tue, 1 Oct 2024 03:59:48 -0500 Subject: [PATCH] fix: gracefully shutdown on CTRL+C and SIGTERM Fixes #205 --- src/server.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/server.rs b/src/server.rs index f69d2007..3da0c3ad 100644 --- a/src/server.rs +++ b/src/server.rs @@ -267,8 +267,19 @@ impl Server { // Bind server to address specified above. Gracefully shut down if CTRL+C is pressed let server = HyperServer::bind(address).serve(make_svc).with_graceful_shutdown(async { + #[cfg(windows)] // Wait for the CTRL+C signal tokio::signal::ctrl_c().await.expect("Failed to install CTRL+C signal handler"); + + #[cfg(unix)] + { + // Wait for CTRL+C or SIGTERM signals + let mut signal_terminate = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()).expect("Failed to install SIGTERM signal handler"); + tokio::select! { + _ = tokio::signal::ctrl_c() => (), + _ = signal_terminate.recv() => () + } + } }); server.boxed()