gracefully exit in case of many SIG*s

This commit is contained in:
hosted-fornet 2023-12-21 16:11:44 -08:00
parent 1845cf1c48
commit 62b2bf08cb
4 changed files with 41 additions and 3 deletions

1
Cargo.lock generated
View File

@ -4698,6 +4698,7 @@ dependencies = [
"mio", "mio",
"num_cpus", "num_cpus",
"pin-project-lite", "pin-project-lite",
"signal-hook-registry",
"socket2 0.5.4", "socket2 0.5.4",
"tokio-macros", "tokio-macros",
"windows-sys 0.48.0", "windows-sys 0.48.0",

View File

@ -55,7 +55,9 @@ rand = "0.8.4"
reqwest = "0.11.18" reqwest = "0.11.18"
ring = "0.16.20" ring = "0.16.20"
rmp-serde = "1.1.2" rmp-serde = "1.1.2"
rocksdb = { version = "0.21.0", features = ["multi-threaded-cf"] }
route-recognizer = "0.3.1" route-recognizer = "0.3.1"
rusqlite = { version = "0.30.0", features = ["bundled"] }
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
serde_urlencoded = "0.7" serde_urlencoded = "0.7"
@ -63,7 +65,7 @@ sha2 = "0.10"
snow = { version = "0.9.3", features = ["ring-resolver"] } snow = { version = "0.9.3", features = ["ring-resolver"] }
static_dir = "0.2.0" static_dir = "0.2.0"
thiserror = "1.0" thiserror = "1.0"
tokio = { version = "1.28", features = ["fs", "macros", "rt-multi-thread", "sync"] } tokio = { version = "1.28", features = ["fs", "macros", "rt-multi-thread", "signal", "sync"] }
tokio-tungstenite = "0.20.1" tokio-tungstenite = "0.20.1"
url = "2.4.1" url = "2.4.1"
uuid = { version = "1.1.2", features = ["serde", "v4"] } uuid = { version = "1.1.2", features = ["serde", "v4"] }
@ -71,5 +73,3 @@ warp = "0.3.5"
wasmtime = "15.0.1" wasmtime = "15.0.1"
wasmtime-wasi = "15.0.1" wasmtime-wasi = "15.0.1"
zip = "0.6" zip = "0.6"
rocksdb = { version = "0.21.0", features = ["multi-threaded-cf"] }
rusqlite = { version = "0.30.0", features = ["bundled"] }

25
src/intercept_sigs.rs Normal file
View File

@ -0,0 +1,25 @@
use tokio::signal::unix::{signal, SignalKind};
/// trigger cleanup if receive signal to kill process
pub async fn intercept_sigs() -> String {
let mut sigalrm = signal(SignalKind::alarm()).expect("uqdev run-tests: failed to set up SIGALRM handler");
let mut sighup = signal(SignalKind::hangup()).expect("uqdev run-tests: failed to set up SIGHUP handler");
let mut sigint = signal(SignalKind::interrupt()).expect("uqdev run-tests: failed to set up SIGINT handler");
let mut sigpipe = signal(SignalKind::pipe()).expect("uqdev run-tests: failed to set up SIGPIPE handler");
let mut sigquit = signal(SignalKind::quit()).expect("uqdev run-tests: failed to set up SIGQUIT handler");
let mut sigterm = signal(SignalKind::terminate()).expect("uqdev run-tests: failed to set up SIGTERM handler");
let mut sigusr1 = signal(SignalKind::user_defined1()).expect("uqdev run-tests: failed to set up SIGUSR1 handler");
let mut sigusr2 = signal(SignalKind::user_defined2()).expect("uqdev run-tests: failed to set up SIGUSR2 handler");
tokio::select! {
_ = sigalrm.recv() => "exiting due to SIGALRM",
_ = sighup.recv() => "exiting due to SIGHUP",
_ = sigint.recv() => "exiting due to SIGINT",
_ = sigpipe.recv() => "exiting due to SIGPIPE",
_ = sigquit.recv() => "exiting due to SIGQUIT",
_ = sigterm.recv() => "exiting due to SIGTERM",
_ = sigusr1.recv() => "exiting due to SIGUSR1",
_ = sigusr2.recv() => "exiting due to SIGUSR2",
}.into()
}

View File

@ -13,6 +13,7 @@ use ring::{rand::SystemRandom, signature, signature::KeyPair};
mod eth_rpc; mod eth_rpc;
mod http; mod http;
mod intercept_sigs;
mod kernel; mod kernel;
mod keygen; mod keygen;
mod kv; mod kv;
@ -472,6 +473,7 @@ async fn main() {
)); ));
// if a runtime task exits, try to recover it, // if a runtime task exits, try to recover it,
// unless it was terminal signaling a quit // unless it was terminal signaling a quit
// or a SIG* was intercepted
let quit_msg: String = tokio::select! { let quit_msg: String = tokio::select! {
Some(Ok(res)) = tasks.join_next() => { Some(Ok(res)) = tasks.join_next() => {
format!( format!(
@ -493,6 +495,9 @@ async fn main() {
Err(e) => e.to_string(), Err(e) => e.to_string(),
} }
} }
s = intercept_sigs::intercept_sigs() => {
s.into()
}
}; };
// gracefully abort all running processes in kernel // gracefully abort all running processes in kernel
@ -521,6 +526,13 @@ async fn main() {
// abort all remaining tasks // abort all remaining tasks
tasks.shutdown().await; tasks.shutdown().await;
let stdout = std::io::stdout();
let mut stdout = stdout.lock();
let _ = crossterm::execute!(
stdout,
crossterm::event::DisableBracketedPaste,
crossterm::terminal::SetTitle(""),
);
let _ = crossterm::terminal::disable_raw_mode(); let _ = crossterm::terminal::disable_raw_mode();
println!("\r\n\x1b[38;5;196m{}\x1b[0m", quit_msg); println!("\r\n\x1b[38;5;196m{}\x1b[0m", quit_msg);
return; return;