Skip to content

Commit

Permalink
opt(katana): graceful shutdown on SIGTERM (#2434)
Browse files Browse the repository at this point in the history
  • Loading branch information
kariy authored Sep 16, 2024
1 parent 2d6e5de commit f4e15f1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
7 changes: 3 additions & 4 deletions bin/katana/src/cli/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,11 @@ use katana_primitives::genesis::constant::DEFAULT_PREFUNDED_ACCOUNT_BALANCE;
use katana_primitives::genesis::Genesis;
use katana_rpc::config::ServerConfig;
use katana_rpc_api::ApiKind;
use tokio::signal::ctrl_c;
use tracing::{info, Subscriber};
use tracing_subscriber::{fmt, EnvFilter};
use url::Url;

use crate::utils::{parse_genesis, parse_seed};
use crate::utils::{parse_genesis, parse_seed, wait_signal};

#[derive(Parser, Debug)]
pub struct NodeArgs {
Expand Down Expand Up @@ -234,9 +233,9 @@ impl NodeArgs {
print_intro(&self, genesis, node.rpc.addr);
}

// Wait until ctrl-c signal is received or TaskManager signals shutdown
// Wait until an OS signal is received or TaskManager shutdown
tokio::select! {
_ = ctrl_c() => {},
_ = wait_signal() => {},
_ = node.task_manager.wait_for_shutdown() => {}
}

Expand Down
23 changes: 23 additions & 0 deletions bin/katana/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,29 @@ pub fn parse_genesis(value: &str) -> Result<Genesis, anyhow::Error> {
Ok(genesis)
}

pub async fn wait_signal() {
use tokio::signal::ctrl_c;

#[cfg(unix)]
tokio::select! {
_ = ctrl_c() => {},
_ = sigterm() => {},
}

#[cfg(not(unix))]
tokio::select! {
_ = ctrl_c() => {},
}
}

/// Returns a future that can be awaited to wait for the SIGTERM signal.
#[cfg(unix)]
async fn sigterm() -> std::io::Result<()> {
use tokio::signal::unix::{signal, SignalKind};
signal(SignalKind::terminate())?.recv().await;
Ok(())
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit f4e15f1

Please sign in to comment.