Skip to content

Commit 05644fc

Browse files
committed
wip
1 parent 7a9e2ac commit 05644fc

2 files changed

Lines changed: 56 additions & 60 deletions

File tree

bin/ev-reth/src/main.rs

Lines changed: 48 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -189,69 +189,65 @@ fn main() {
189189

190190
// Set up graceful shutdown handling
191191
let shutdown_signal = async {
192-
let signal_result = async {
193-
#[cfg(unix)]
194-
{
195-
// Set up SIGTERM handler with proper error handling
196-
// Note: We only handle SIGTERM explicitly; ctrl_c() handles SIGINT automatically
197-
match signal::unix::signal(signal::unix::SignalKind::terminate()) {
198-
Ok(mut sigterm) => {
199-
tokio::select! {
200-
_ = sigterm.recv() => {
201-
info!("=== EV-RETH: Received SIGTERM, initiating graceful shutdown ===");
202-
Ok(())
203-
}
204-
result = signal::ctrl_c() => {
205-
match result {
206-
Ok(_) => {
207-
info!("=== EV-RETH: Received SIGINT/Ctrl+C, initiating graceful shutdown ===");
208-
Ok(())
209-
}
210-
Err(err) => {
211-
tracing::error!("Failed to wait for Ctrl+C: {}", err);
212-
Err(err)
213-
}
192+
#[cfg(unix)]
193+
{
194+
// On Unix systems, handle both SIGTERM and SIGINT (Ctrl+C) separately for clarity
195+
// SIGTERM is typically sent by process managers for graceful shutdown
196+
// SIGINT is sent by Ctrl+C from terminal
197+
match signal::unix::signal(signal::unix::SignalKind::terminate()) {
198+
Ok(mut sigterm) => {
199+
// Successfully set up SIGTERM handler, now wait for either SIGTERM or SIGINT
200+
tokio::select! {
201+
_ = sigterm.recv() => {
202+
info!("=== EV-RETH: Received SIGTERM, initiating graceful shutdown ===");
203+
}
204+
result = signal::ctrl_c() => {
205+
match result {
206+
Ok(_) => {
207+
info!("=== EV-RETH: Received SIGINT/Ctrl+C, initiating graceful shutdown ===");
208+
}
209+
Err(err) => {
210+
tracing::error!("Failed to wait for SIGINT: {}", err);
211+
// Continue with shutdown even if SIGINT handling failed
214212
}
215213
}
216214
}
217215
}
218-
Err(err) => {
219-
tracing::warn!("Failed to install SIGTERM handler: {}, falling back to SIGINT only", err);
220-
// Fall back to just handling SIGINT/Ctrl+C
221-
match signal::ctrl_c().await {
222-
Ok(_) => {
223-
info!("=== EV-RETH: Received SIGINT/Ctrl+C, initiating graceful shutdown ===");
224-
Ok(())
225-
}
226-
Err(ctrl_c_err) => {
227-
tracing::error!("Failed to wait for Ctrl+C: {}", ctrl_c_err);
228-
Err(ctrl_c_err)
229-
}
216+
}
217+
Err(err) => {
218+
tracing::warn!("Failed to install SIGTERM handler: {}, falling back to SIGINT only", err);
219+
// Fall back to just handling SIGINT/Ctrl+C
220+
match signal::ctrl_c().await {
221+
Ok(_) => {
222+
info!("=== EV-RETH: Received SIGINT/Ctrl+C, initiating graceful shutdown ===");
223+
}
224+
Err(ctrl_c_err) => {
225+
tracing::error!("Failed to wait for SIGINT: {}", ctrl_c_err);
226+
// If we can't handle any signals, we should still shut down gracefully
227+
// This prevents the application from hanging indefinitely
228+
tracing::warn!("No signal handling available, shutdown will only occur on natural node exit");
229+
// Wait indefinitely - this branch should rarely be reached
230+
std::future::pending::<()>().await;
230231
}
231232
}
232233
}
233234
}
235+
}
234236

235-
#[cfg(not(unix))]
236-
{
237-
// On non-Unix systems, only handle Ctrl+C
238-
match signal::ctrl_c().await {
239-
Ok(_) => {
240-
info!("=== EV-RETH: Received SIGINT/Ctrl+C, initiating graceful shutdown ===");
241-
Ok(())
242-
}
243-
Err(err) => {
244-
tracing::error!("Failed to wait for Ctrl+C: {}", err);
245-
Err(err)
246-
}
237+
#[cfg(not(unix))]
238+
{
239+
// On non-Unix systems, only handle Ctrl+C (SIGINT)
240+
match signal::ctrl_c().await {
241+
Ok(_) => {
242+
info!("=== EV-RETH: Received SIGINT/Ctrl+C, initiating graceful shutdown ===");
243+
}
244+
Err(err) => {
245+
tracing::error!("Failed to wait for SIGINT: {}", err);
246+
tracing::warn!("No signal handling available, shutdown will only occur on natural node exit");
247+
// Wait indefinitely - this branch should rarely be reached
248+
std::future::pending::<()>().await;
247249
}
248250
}
249-
}.await;
250-
251-
// Handle signal errors gracefully - if we can't set up signal handling,
252-
// we should still allow the application to continue running
253-
if let Err(err) = signal_result {
254-
tracing::warn!("Signal handling failed: {}, application will continue without graceful shutdown", err);
255251
}
256252
};
257253

bin/ev-reth/src/signal_tests.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ mod tests {
2525
}
2626
};
2727

28-
// Use a very short timeout since we're not actually sending a signal
29-
let result = timeout(Duration::from_millis(10), shutdown_signal).await;
28+
// Use a reasonable timeout that works reliably across different systems
29+
let result = timeout(Duration::from_millis(100), shutdown_signal).await;
3030

3131
// The timeout should occur since we're not actually sending a signal
3232
assert!(
@@ -106,8 +106,8 @@ mod tests {
106106
}
107107
};
108108

109-
// Use a very short timeout since we're not actually sending signals
110-
let result = timeout(Duration::from_millis(10), shutdown_signal).await;
109+
// Use a reasonable timeout that works reliably across different systems
110+
let result = timeout(Duration::from_millis(100), shutdown_signal).await;
111111

112112
// The timeout should occur since we're not actually sending signals
113113
assert!(
@@ -159,8 +159,8 @@ mod tests {
159159
}
160160
};
161161

162-
// Use a very short timeout since we're not actually sending signals
163-
let result = timeout(Duration::from_millis(10), shutdown_signal).await;
162+
// Use a reasonable timeout that works reliably across different systems
163+
let result = timeout(Duration::from_millis(100), shutdown_signal).await;
164164

165165
// The timeout should occur since we're not actually sending signals
166166
assert!(
@@ -203,8 +203,8 @@ mod tests {
203203
}
204204
};
205205

206-
// Use a very short timeout since we're not actually sending signals
207-
let result = timeout(Duration::from_millis(10), shutdown_signal).await;
206+
// Use a reasonable timeout that works reliably across different systems
207+
let result = timeout(Duration::from_millis(100), shutdown_signal).await;
208208

209209
// The timeout should occur since we're not actually sending signals
210210
assert!(

0 commit comments

Comments
 (0)