From bbe5f8191475ea20e41e5a4eb1572fbd2a45d8b3 Mon Sep 17 00:00:00 2001 From: internationalcrisis <97415475+internationalcrisis@users.noreply.github.com> Date: Sun, 2 Feb 2025 22:40:19 -0600 Subject: [PATCH] fix: gracefully shutdown on CTRL+C and SIGTERM (#273) Fixes #205 --- src/server.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/server.rs b/src/server.rs index 5297c22..a287de2 100644 --- a/src/server.rs +++ b/src/server.rs @@ -282,8 +282,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()