Skip to content

Commit 7151c7d

Browse files
refactor(server): isolate platform shutdown signals
Signed-off-by: nachiketb <nachiketb@nvidia.com>
1 parent ead2dcc commit 7151c7d

2 files changed

Lines changed: 54 additions & 39 deletions

File tree

crates/switchyard-server/src/lib.rs

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ mod metrics;
88
mod observability;
99
mod response;
1010
mod routing_log;
11+
mod shutdown;
1112
mod sse;
1213
mod stats;
1314
mod usage_metrics;
@@ -247,7 +248,7 @@ pub async fn run_server(state: ServerState, options: ServerRunOptions) -> Server
247248

248249
let server = BoundServer::bind(state, options)?;
249250
println!("{}", server.startup_banner(std::io::stdout().is_terminal()));
250-
server.serve(shutdown_signal()).await
251+
server.serve(shutdown::signal()).await
251252
}
252253

253254
/// A configured server with its listening socket already bound.
@@ -438,44 +439,6 @@ fn server_io_error(error: std::io::Error) -> ServerError {
438439
ServerError::new(error.to_string())
439440
}
440441

441-
async fn shutdown_signal() {
442-
#[cfg(unix)]
443-
tokio::select! {
444-
_ = ctrl_c_signal() => {},
445-
_ = terminate_signal() => {},
446-
}
447-
448-
#[cfg(not(unix))]
449-
ctrl_c_signal().await;
450-
}
451-
452-
async fn ctrl_c_signal() {
453-
if let Err(error) = tokio::signal::ctrl_c().await {
454-
tracing::warn!(
455-
error = %error,
456-
"ctrl-c shutdown signal unavailable; continuing without shutdown trigger"
457-
);
458-
std::future::pending::<()>().await;
459-
}
460-
}
461-
462-
#[cfg(unix)]
463-
async fn terminate_signal() {
464-
let mut signal = match tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
465-
{
466-
Ok(signal) => signal,
467-
Err(error) => {
468-
tracing::warn!(
469-
error = %error,
470-
"SIGTERM shutdown signal unavailable; continuing without SIGTERM trigger"
471-
);
472-
std::future::pending::<()>().await;
473-
return;
474-
}
475-
};
476-
signal.recv().await;
477-
}
478-
479442
async fn openai_chat_completions(
480443
State(state): State<ServerState>,
481444
Extension(started): Extension<RequestStart>,
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
//! Platform-specific process shutdown signals.
5+
6+
/// Waits for the platform's normal process termination signal.
7+
pub(crate) async fn signal() {
8+
platform::signal().await;
9+
}
10+
11+
async fn ctrl_c() {
12+
if let Err(error) = tokio::signal::ctrl_c().await {
13+
tracing::warn!(
14+
error = %error,
15+
"ctrl-c shutdown signal unavailable; continuing without shutdown trigger"
16+
);
17+
std::future::pending::<()>().await;
18+
}
19+
}
20+
21+
#[cfg(unix)]
22+
mod platform {
23+
pub(super) async fn signal() {
24+
tokio::select! {
25+
_ = super::ctrl_c() => {},
26+
_ = terminate() => {},
27+
}
28+
}
29+
30+
async fn terminate() {
31+
let mut signal =
32+
match tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()) {
33+
Ok(signal) => signal,
34+
Err(error) => {
35+
tracing::warn!(
36+
error = %error,
37+
"SIGTERM shutdown signal unavailable; continuing without SIGTERM trigger"
38+
);
39+
std::future::pending::<()>().await;
40+
return;
41+
}
42+
};
43+
signal.recv().await;
44+
}
45+
}
46+
47+
#[cfg(not(unix))]
48+
mod platform {
49+
pub(super) async fn signal() {
50+
super::ctrl_c().await;
51+
}
52+
}

0 commit comments

Comments
 (0)