Skip to content

Commit

Permalink
cli: unix: catch SIGTERM in addition to SIGINT and terminate gracefully
Browse files Browse the repository at this point in the history
The default signal ‘kill’ command sends is SIGTERM so catch it in
addition ta SIGINT when running under Unix-like system.

Issue: near#3266
  • Loading branch information
mina86 committed Jul 5, 2021
1 parent b7c8a3b commit cefa49e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions neard/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ openssl-probe = "0.1.2"
near-rust-allocator-proxy = { version = "0.2.8", optional = true }
lazy_static = "1.4"
tokio = "1.1"
futures = "0.3"

nearcore = { path = "../nearcore" }
near-primitives = { path = "../core/primitives" }
Expand Down
17 changes: 15 additions & 2 deletions neard/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::{DEFAULT_HOME, NEARD_VERSION, NEARD_VERSION_STRING, PROTOCOL_VERSION};
use clap::{AppSettings, Clap};
use futures::future::FutureExt;
use near_primitives::types::{Gas, NumSeats, NumShards};
use nearcore::get_store_path;
use std::net::SocketAddr;
Expand Down Expand Up @@ -263,8 +264,20 @@ impl RunCmd {
let sys = actix::System::new();
sys.block_on(async move {
nearcore::start_with_config(home_dir, near_config);
tokio::signal::ctrl_c().await.unwrap();
info!("Got Ctrl+C, stopping");

let sig = if cfg!(windows) {
tokio::signal::ctrl_c().await.unwrap();
"Ctrl+C"
} else {
use tokio::signal::unix::{signal, SignalKind};
let mut sigint = signal(SignalKind::interrupt()).unwrap();
let mut sigterm = signal(SignalKind::terminate()).unwrap();
futures::select! {
_ = sigint .recv().fuse() => "SIGINT",
_ = sigterm.recv().fuse() => "SIGTERM"
}
};
info!("Got {}, stopping", sig);
actix::System::current().stop();
});
sys.run().unwrap();
Expand Down

0 comments on commit cefa49e

Please sign in to comment.