Skip to content

Commit

Permalink
fix(wallet): fix ctrl-c handler on windows platflorm
Browse files Browse the repository at this point in the history
  • Loading branch information
tmpolaczyk authored and mariocao committed Oct 4, 2020
1 parent 47cfd32 commit 81ae268
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions wallet/src/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: Fn() + 'static>(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 |_| {
Expand Down

0 comments on commit 81ae268

Please sign in to comment.