Skip to content

Commit

Permalink
Merge pull request #1853 from eqlabs/krisztian/exit-gracefully-on-signal
Browse files Browse the repository at this point in the history
fix(pathfinder): exit gracefully on INT/TERM signal
  • Loading branch information
kkovaacs authored Mar 12, 2024
2 parents 98aa635 + 2bda80d commit 825853c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion crates/pathfinder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ starknet-gateway-types = { path = "../gateway-types" }
tempfile = "3.8"
thiserror = "1.0.48"
time = { version = "0.3.28", features = ["macros"] }
tokio = { workspace = true, features = ["rt-multi-thread", "macros"] }
tokio = { workspace = true, features = ["rt-multi-thread", "macros", "signal"] }
tokio-stream = "0.1.14"
tracing = { workspace = true }
tracing-subscriber = { version = "0.3.17", features = [
Expand Down
17 changes: 15 additions & 2 deletions crates/pathfinder/src/bin/pathfinder/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use std::net::SocketAddr;
use std::num::NonZeroU32;
use std::path::PathBuf;
use std::sync::{atomic::AtomicBool, Arc};
use tokio::signal::unix::{signal, SignalKind};
use tracing::info;

use crate::config::NetworkConfig;
Expand Down Expand Up @@ -251,6 +252,9 @@ Hint: This is usually caused by exceeding the file descriptor limit of your syst

tokio::spawn(update::poll_github_for_releases());

let mut term_signal = signal(SignalKind::terminate())?;
let mut int_signal = signal(SignalKind::interrupt())?;

// We are now ready.
readiness.store(true, std::sync::atomic::Ordering::Relaxed);

Expand All @@ -261,22 +265,31 @@ Hint: This is usually caused by exceeding the file descriptor limit of your syst
Ok(task_result) => tracing::error!("Sync process ended unexpected with: {:?}", task_result),
Err(err) => tracing::error!("Sync process ended unexpected; failed to join task handle: {:?}", err),
}
anyhow::bail!("Unexpected shutdown");
}
result = rpc_handle => {
match result {
Ok(_) => tracing::error!("RPC server process ended unexpectedly"),
Err(err) => tracing::error!(error=%err, "RPC server process ended unexpectedly"),
}
anyhow::bail!("Unexpected shutdown");
}
result = p2p_handle => {
match result {
Ok(_) => tracing::error!("P2P process ended unexpectedly"),
Err(err) => tracing::error!(error=%err, "P2P process ended unexpectedly"),
}
anyhow::bail!("Unexpected shutdown");
}
_ = term_signal.recv() => {
tracing::info!("TERM signal received, exiting gracefully");
Ok(())
}
_ = int_signal.recv() => {
tracing::info!("INT signal received, exiting gracefully");
Ok(())
}
}

anyhow::bail!("Unexpected shutdown");
}

#[cfg(feature = "tokio-console")]
Expand Down

0 comments on commit 825853c

Please sign in to comment.