From 81ae26849fea6de533d3b15ce9f19dffe2359810 Mon Sep 17 00:00:00 2001 From: Tomasz Polaczyk Date: Tue, 29 Sep 2020 12:12:21 +0200 Subject: [PATCH] fix(wallet): fix ctrl-c handler on windows platflorm --- wallet/src/signal.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/wallet/src/signal.rs b/wallet/src/signal.rs index 613af0827..81261c59a 100644 --- a/wallet/src/signal.rs +++ b/wallet/src/signal.rs @@ -3,15 +3,23 @@ use actix::prelude::*; /// It will call `cb` function for Ctrl-c events (or SIGTERM signals in Unix). pub fn ctrl_c(cb: T) { + // This is received when doing kill $(pidof witnet), removing this handler will make the wallet + // shutdown instantly and corrupt the database. #[cfg(unix)] let sigterm = tokio_signal::unix::Signal::new(tokio_signal::unix::SIGTERM) - .map(|s| s.map(|_| ())) + .map(|s| s.map(|_| log::trace!("Received SIGTERM signal"))) .flatten_stream(); + // There is no equivalent to SIGTERM on Windows, so use empty stream #[cfg(windows)] - let sigterm = futures::future::ok(()).into_stream(); + let sigterm = futures::stream::empty(); - let ctrl_c = tokio_signal::ctrl_c().flatten_stream(); + // This is received when pressing CTRL-C, and it works on both Unix and Windows + let ctrl_c = tokio_signal::ctrl_c() + .flatten_stream() + .inspect(|_| log::trace!("Received CTRL-C")); + + // Handle both CTRL-C and SIGTERM using the same callback let handle_shutdown = ctrl_c .select(sigterm) .for_each(move |_| {