Skip to content

Commit f84ab89

Browse files
committed
wip
1 parent af9b382 commit f84ab89

File tree

2 files changed

+108
-31
lines changed

2 files changed

+108
-31
lines changed

bin/ev-reth/src/main.rs

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -192,23 +192,38 @@ fn main() {
192192
// Set up graceful shutdown handling
193193
let shutdown_signal = async {
194194
#[cfg(unix)]
195-
let mut sigterm = signal::unix::signal(signal::unix::SignalKind::terminate())
196-
.expect("Failed to install SIGTERM handler");
195+
{
196+
// Set up SIGTERM handler with proper error handling
197+
let sigterm_result = signal::unix::signal(signal::unix::SignalKind::terminate());
198+
match sigterm_result {
199+
Ok(mut sigterm) => {
200+
tokio::select! {
201+
_ = sigterm.recv() => {
202+
info!("=== EV-RETH: Received SIGTERM, initiating graceful shutdown ===");
203+
}
204+
_ = signal::ctrl_c() => {
205+
info!("=== EV-RETH: Received SIGINT/Ctrl+C, initiating graceful shutdown ===");
206+
}
207+
}
208+
}
209+
Err(err) => {
210+
tracing::warn!("Failed to install SIGTERM handler: {}, falling back to SIGINT only", err);
211+
// Fall back to just handling SIGINT/Ctrl+C
212+
if let Err(ctrl_c_err) = signal::ctrl_c().await {
213+
tracing::error!("Failed to wait for Ctrl+C: {}", ctrl_c_err);
214+
} else {
215+
info!("=== EV-RETH: Received SIGINT/Ctrl+C, initiating graceful shutdown ===");
216+
}
217+
}
218+
}
219+
}
197220

198221
#[cfg(not(unix))]
199-
let sigterm = std::future::pending::<()>(); // Never resolves on non-Unix
200-
201-
tokio::select! {
202-
#[cfg(unix)]
203-
_ = sigterm.recv() => {
204-
info!("=== EV-RETH: Received SIGTERM, initiating graceful shutdown ===");
205-
}
206-
#[cfg(not(unix))]
207-
_ = sigterm => {
208-
// This branch will never be reached on non-Unix systems
209-
unreachable!("SIGTERM handler should never resolve on non-Unix systems");
210-
}
211-
_ = signal::ctrl_c() => {
222+
{
223+
// On non-Unix systems, only handle Ctrl+C
224+
if let Err(err) = signal::ctrl_c().await {
225+
tracing::error!("Failed to wait for Ctrl+C: {}", err);
226+
} else {
212227
info!("=== EV-RETH: Received SIGINT/Ctrl+C, initiating graceful shutdown ===");
213228
}
214229
}

bin/ev-reth/src/signal_tests.rs

Lines changed: 78 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -132,28 +132,44 @@ mod tests {
132132
);
133133
}
134134

135-
/// Test cross-platform signal handling (works on all platforms)
135+
/// Test cross-platform signal handling with proper error handling (works on all platforms)
136136
#[tokio::test]
137137
async fn test_cross_platform_signal_handling() {
138-
// Test the pattern used in main.rs that works on all platforms
138+
// Test the new safer pattern used in main.rs that works on all platforms
139139
let shutdown_signal = async {
140140
#[cfg(unix)]
141-
let mut sigterm = signal::unix::signal(signal::unix::SignalKind::terminate())
142-
.expect("Failed to install SIGTERM handler");
141+
{
142+
// Test proper error handling for SIGTERM
143+
let sigterm_result = signal::unix::signal(signal::unix::SignalKind::terminate());
144+
match sigterm_result {
145+
Ok(mut sigterm) => {
146+
tokio::select! {
147+
_ = sigterm.recv() => {
148+
println!("=== TEST: SIGTERM received ===");
149+
}
150+
_ = signal::ctrl_c() => {
151+
println!("=== TEST: Ctrl+C received ===");
152+
}
153+
}
154+
}
155+
Err(err) => {
156+
println!("TEST: Failed to install SIGTERM handler: {}, falling back to SIGINT only", err);
157+
// Fall back to just handling SIGINT/Ctrl+C
158+
if let Err(ctrl_c_err) = signal::ctrl_c().await {
159+
println!("TEST: Failed to wait for Ctrl+C: {}", ctrl_c_err);
160+
} else {
161+
println!("=== TEST: Ctrl+C received ===");
162+
}
163+
}
164+
}
165+
}
143166

144167
#[cfg(not(unix))]
145-
let sigterm = std::future::pending::<()>(); // Never resolves on non-Unix
146-
147-
tokio::select! {
148-
#[cfg(unix)]
149-
_ = sigterm.recv() => {
150-
println!("=== TEST: SIGTERM received ===");
151-
}
152-
#[cfg(not(unix))]
153-
_ = sigterm => {
154-
unreachable!("SIGTERM handler should never resolve on non-Unix systems");
155-
}
156-
_ = signal::ctrl_c() => {
168+
{
169+
// On non-Unix systems, only handle Ctrl+C - no unreachable!() panic
170+
if let Err(err) = signal::ctrl_c().await {
171+
println!("TEST: Failed to wait for Ctrl+C: {}", err);
172+
} else {
157173
println!("=== TEST: Ctrl+C received ===");
158174
}
159175
}
@@ -168,4 +184,50 @@ mod tests {
168184
"Signal handlers should timeout when no signals are sent"
169185
);
170186
}
187+
188+
/// Test that signal handler setup gracefully handles errors
189+
#[tokio::test]
190+
#[cfg(unix)]
191+
async fn test_signal_handler_error_handling() {
192+
// Test that our error handling pattern works correctly
193+
let sigterm_result = signal::unix::signal(signal::unix::SignalKind::terminate());
194+
195+
// This should succeed in normal circumstances
196+
match sigterm_result {
197+
Ok(_sigterm) => {
198+
println!("=== TEST: SIGTERM handler created successfully ===");
199+
// Success case - handler was created
200+
assert!(true, "SIGTERM handler should be created successfully");
201+
}
202+
Err(err) => {
203+
println!("TEST: SIGTERM handler creation failed: {}", err);
204+
// Error case - should not panic, just log and continue
205+
// This tests that our error handling is robust
206+
assert!(true, "Error handling should not panic");
207+
}
208+
}
209+
}
210+
211+
/// Test that non-Unix systems handle signals gracefully without panicking
212+
#[tokio::test]
213+
#[cfg(not(unix))]
214+
async fn test_non_unix_signal_handling() {
215+
// Test that non-Unix systems can handle Ctrl+C without any unreachable!() panics
216+
let shutdown_signal = async {
217+
if let Err(err) = signal::ctrl_c().await {
218+
println!("TEST: Failed to wait for Ctrl+C: {}", err);
219+
} else {
220+
println!("=== TEST: Ctrl+C received on non-Unix system ===");
221+
}
222+
};
223+
224+
// Use a very short timeout since we're not actually sending signals
225+
let result = timeout(Duration::from_millis(10), shutdown_signal).await;
226+
227+
// The timeout should occur since we're not actually sending signals
228+
assert!(
229+
result.is_err(),
230+
"Signal handler should timeout when no signal is sent"
231+
);
232+
}
171233
}

0 commit comments

Comments
 (0)