Skip to content

Commit fab0ee7

Browse files
authored
Fixed graceful shutdown (#414)
1 parent 82a2993 commit fab0ee7

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

test-bins/src/rpc.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
mod shutdown;
2+
13
use log::*;
24
use magicblock_api::{
35
ledger,
@@ -8,6 +10,8 @@ use magicblock_config::{EphemeralConfig, GeyserGrpcConfig};
810
use solana_sdk::signature::{Keypair, Signer};
911
use test_tools::init_logger;
1012

13+
use crate::shutdown::Shutdown;
14+
1115
// mAGicPQYBMvcYveUZA5F5UNNwyHvfYh5xkLS2Fr1mev
1216
const TEST_KEYPAIR_BYTES: [u8; 64] = [
1317
7, 83, 184, 55, 200, 223, 238, 137, 166, 244, 107, 126, 189, 16, 194, 36,
@@ -98,7 +102,6 @@ async fn main() {
98102
ledger::lock_ledger(api.ledger().ledger_path(), &mut ledger_lock);
99103

100104
api.start().await.expect("Failed to start validator");
101-
102105
info!("");
103106
info!("🧙 Magicblock Validator is running!");
104107
info!(
@@ -113,11 +116,9 @@ async fn main() {
113116
info!("Ready for connections!");
114117
info!("");
115118

116-
// validator is supposed to run forever, so we wait for
117-
// termination signal to initiate a graceful shutdown
118-
let _ = tokio::signal::ctrl_c().await;
119-
120-
info!("SIGTERM has been received, initiating graceful shutdown");
119+
if let Err(err) = Shutdown::wait().await {
120+
error!("Failed to gracefully shutdown: {}", err);
121+
}
121122
// weird panic behavior in json rpc http server, which panics when stopped from
122123
// within async context, so we just move it to a different thread for shutdown
123124
//

test-bins/src/shutdown.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use std::io;
2+
3+
use log::info;
4+
use tokio::{signal, signal::unix::SignalKind};
5+
6+
pub struct Shutdown;
7+
impl Shutdown {
8+
pub async fn wait() -> Result<(), io::Error> {
9+
#[cfg(unix)]
10+
return Self::wait_unix().await;
11+
#[cfg(not(unix))]
12+
return Self::wait_other().await;
13+
}
14+
15+
#[cfg(unix)]
16+
async fn wait_unix() -> Result<(), io::Error> {
17+
let mut terminate_signal =
18+
signal::unix::signal(SignalKind::terminate())?;
19+
tokio::select! {
20+
_ = terminate_signal.recv() => {
21+
info!("SIGTERM has been received, initiating graceful shutdown");
22+
},
23+
_ = tokio::signal::ctrl_c() => {
24+
info!("SIGINT signal received, initiating graceful shutdown");
25+
},
26+
}
27+
28+
Ok(())
29+
}
30+
31+
#[cfg(not(unix))]
32+
async fn wait_other() -> Result<(), io::Error> {
33+
tokio::signal::ctrl_c().await
34+
}
35+
}

0 commit comments

Comments
 (0)