From 304f9eb7d250bbe2b6b09082fb74d44fe68e89b3 Mon Sep 17 00:00:00 2001 From: "Daniel Gruber (PVPi-Secondary)" Date: Thu, 20 Nov 2025 17:02:56 +0200 Subject: [PATCH] Shutdown on Ctrl+C and SIGTERM --- Cargo.lock | 17 ++++++++++------- Cargo.toml | 2 +- src/lib.rs | 14 +++++++++++++- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9746986..c67f369 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -482,7 +482,6 @@ dependencies = [ "env_logger", "futures", "log", - "scopeguard", "serde", "structopt", "tokio", @@ -703,12 +702,6 @@ version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741" -[[package]] -name = "scopeguard" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" - [[package]] name = "serde" version = "1.0.192" @@ -751,6 +744,15 @@ dependencies = [ "digest", ] +[[package]] +name = "signal-hook-registry" +version = "1.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2a4719bff48cee6b39d12c020eeb490953ad2443b7055bd0b21fca26bd8c28b" +dependencies = [ + "libc", +] + [[package]] name = "slab" version = "0.4.9" @@ -871,6 +873,7 @@ dependencies = [ "libc", "mio", "pin-project-lite", + "signal-hook-registry", "socket2", "tokio-macros", "windows-sys", diff --git a/Cargo.toml b/Cargo.toml index a6b2858..7f7e21b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,7 @@ publish = true maintenance = { status = "experimental" } [dependencies] -tokio = { version = "1.34", features = ["rt", "net", "macros", "io-util", "sync", "time"] } +tokio = { version = "1.34", features = ["rt", "net", "macros", "io-util", "sync", "time", "signal"] } structopt = "0.3" log = "0.4" env_logger = "0.10" diff --git a/src/lib.rs b/src/lib.rs index 94b850d..6e300cc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,8 @@ use futures::future::join_all; use serde::Deserialize; use tokio::io::{AsyncReadExt, AsyncWriteExt, BufReader, BufWriter}; use tokio::net::{TcpListener, TcpStream}; +#[cfg(unix)] +use tokio::signal::unix::{signal, SignalKind}; use tokio::sync::{mpsc, oneshot}; // Use Jemalloc only for musl-64 bits platforms @@ -240,7 +242,17 @@ impl Server { for mut bridge in self.devices { tasks.push(tokio::spawn(async move { bridge.run().await })); } - join_all(tasks).await; + + #[cfg(unix)] + let mut sigterm = signal(SignalKind::terminate()).unwrap(); + tokio::select! { + _ = join_all(tasks) => debug!("All tasks finished"), + _ = tokio::signal::ctrl_c() => debug!("Received Ctrl+C"), + //#[cfg(unix)] + _ = sigterm.recv() => debug!("Received SIGTERM"), + } + + info!("Shutting down"); } pub async fn launch(config_file: &str) -> std::result::Result<(), config::ConfigError> {