Skip to content

Commit

Permalink
feat(cli): properly handle sigterm
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexagon committed May 3, 2024
1 parent 031dee4 commit c51ae04
Show file tree
Hide file tree
Showing 5 changed files with 54 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.

3 changes: 2 additions & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ everscale-crypto = { workspace = true }
everscale-types = { workspace = true }
futures-util = { workspace = true }
hex = { workspace = true }
libc = { workspace = true }
public-ip = { workspace = true }
rand = { workspace = true }
serde = { workspace = true }
Expand All @@ -34,7 +35,7 @@ tikv-jemallocator = { workspace = true, features = [
"unprefixed_malloc_on_supported_platforms",
"background_threads",
], optional = true }
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
tokio = { workspace = true, features = ["macros", "rt-multi-thread", "signal"] }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }

Expand Down
16 changes: 15 additions & 1 deletion cli/src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use tycho_util::FastHashMap;

use crate::util::error::ResultExt;
use crate::util::logger::LoggerConfig;
use crate::util::signal;

use self::config::{NodeConfig, NodeKeys};

Expand Down Expand Up @@ -82,7 +83,20 @@ impl CmdRun {
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()?
.block_on(self.run_impl())
.block_on(async move {
let run_fut = tokio::spawn(self.run_impl());
let stop_fut = signal::any_signal(signal::TERMINATION_SIGNALS);
tokio::select! {
res = run_fut => res.unwrap(),
signal = stop_fut => match signal {
Ok(signal) => {
tracing::info!(?signal, "received termination signal");
Ok(())
}
Err(e) => Err(e.into()),
}
}
})
}

async fn run_impl(self) -> Result<()> {
Expand Down
1 change: 1 addition & 0 deletions cli/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use everscale_types::prelude::*;

pub mod error;
pub mod logger;
pub mod signal;

// TODO: move into types
pub fn compute_storage_used(account: &Account) -> Result<StorageUsed> {
Expand Down
35 changes: 35 additions & 0 deletions cli/src/util/signal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use tokio::signal::unix;

pub const TERMINATION_SIGNALS: [libc::c_int; 5] = [
libc::SIGINT,
libc::SIGTERM,
libc::SIGQUIT,
libc::SIGABRT,
20, // SIGTSTP
];

pub fn any_signal<I, T>(signals: I) -> tokio::sync::oneshot::Receiver<unix::SignalKind>
where
I: IntoIterator<Item = T>,
T: Into<unix::SignalKind> + Send + 'static,
{
let (tx, rx) = tokio::sync::oneshot::channel();

let any_signal = futures_util::future::select_all(signals.into_iter().map(|signal| {
Box::pin(async move {
let signal = signal.into();
unix::signal(signal)
.expect("Failed subscribing on unix signals")
.recv()
.await;
signal
})
}));

tokio::spawn(async move {
let signal = any_signal.await.0;
tx.send(signal).ok();
});

rx
}

0 comments on commit c51ae04

Please sign in to comment.