From 9c3c17cc030dd97cc3bc9b34986b462a2a287ffd Mon Sep 17 00:00:00 2001 From: Michal Nazarewicz Date: Thu, 1 Jul 2021 21:30:52 +0200 Subject: [PATCH 1/2] cli: unix: catch SIGTERM in addition to SIGINT and terminate gracefully MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The default signal ‘kill’ command sends is SIGTERM so catch it in addition ta SIGINT when running under Unix-like system. Issue: https://github.com/near/nearcore/issues/3266 --- Cargo.lock | 1 + neard/Cargo.toml | 1 + neard/src/cli.rs | 17 +++++++++++++++-- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 96d28ef353d..0dbb4e6ec93 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3758,6 +3758,7 @@ version = "1.2.0" dependencies = [ "actix", "clap 3.0.0-beta.2", + "futures", "git-version", "lazy_static", "near-performance-metrics", diff --git a/neard/Cargo.toml b/neard/Cargo.toml index a38eaa433c6..087b83d2b64 100644 --- a/neard/Cargo.toml +++ b/neard/Cargo.toml @@ -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" } diff --git a/neard/src/cli.rs b/neard/src/cli.rs index 8b908813ec0..5daf29e062e 100644 --- a/neard/src/cli.rs +++ b/neard/src/cli.rs @@ -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; @@ -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!(unix) { + 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" + } + } else { + tokio::signal::ctrl_c().await.unwrap(); + "Ctrl+C" + }; + info!("Got {}, stopping", sig); actix::System::current().stop(); }); sys.run().unwrap(); From d648bc0dddfe91eb471373f826d4cf7c0df576a9 Mon Sep 17 00:00:00 2001 From: Michal Nazarewicz Date: Tue, 6 Jul 2021 18:16:19 +0200 Subject: [PATCH 2/2] Update neard/src/cli.rs Co-authored-by: Aleksey Kladov --- neard/src/cli.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neard/src/cli.rs b/neard/src/cli.rs index 5daf29e062e..b538c13eb7f 100644 --- a/neard/src/cli.rs +++ b/neard/src/cli.rs @@ -277,7 +277,7 @@ impl RunCmd { tokio::signal::ctrl_c().await.unwrap(); "Ctrl+C" }; - info!("Got {}, stopping", sig); + info!(target: "neard", "Got {}, stopping", sig); actix::System::current().stop(); }); sys.run().unwrap();