Skip to content

Plan, apply and roll back changes to a production ontology, with a blast radius report and a proof an auditor can re-check.

fix(serve-http): keep listening so a second signal can force the exit

Nicolas Geysse1994c5cc1a2c
diff --git a/src/main.rs b/src/main.rs
index 609b799e..8caeb7e6 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -687,14 +687,14 @@ fn build_tool_filter(
})
}
-/// Resolves when the process is asked to terminate.
+/// Resolves when the process is asked to terminate, naming the signal.
///
/// Listens for ctrl-c on every platform and, on unix, for `SIGTERM` as well —
/// the latter is what `docker stop`, systemd and Kubernetes actually send, so
/// handling only ctrl-c would leave container shutdown unhandled. The unix arm
/// is behind `#[cfg(unix)]` so the `x86_64-pc-windows-msvc` target in the
/// release matrix still builds; there the future simply never resolves.
-async fn shutdown_signal() {
+async fn shutdown_signal() -> &'static str {
let ctrl_c = async {
if let Err(e) = tokio::signal::ctrl_c().await {
eprintln!("Failed to install ctrl-c handler: {e}");
@@ -720,8 +720,18 @@ async fn shutdown_signal() {
let terminate = std::future::pending::<()>();
tokio::select! {
- _ = ctrl_c => {}
- _ = terminate => {}
+ _ = ctrl_c => "ctrl-c",
+ _ = terminate => "SIGTERM",
+ }
+}
+
+/// Exit code conventionally reported for a process ended by `sig`.
+fn signal_exit_code(sig: &str) -> i32 {
+ // 128 + signal number: SIGINT = 2, SIGTERM = 15.
+ if sig == "ctrl-c" {
+ 130
+ } else {
+ 143
}
}
@@ -1003,9 +1013,20 @@ async fn async_main() -> anyhow::Result<()> {
tokio::spawn({
let ct = ct.clone();
async move {
- shutdown_signal().await;
- eprintln!("Shutdown signal received — stopping HTTP server");
+ let sig = shutdown_signal().await;
+ eprintln!("{sig} received — stopping HTTP server");
ct.cancel();
+ // Tokio installs its signal handlers process-wide and never
+ // removes them, so from here the default terminate
+ // disposition is gone for good. If graceful shutdown then
+ // stalls — a long synchronous /api/query or /api/load holds
+ // its connection open, and axum waits for in-flight
+ // requests — a second signal would be swallowed and only
+ // SIGKILL would end the process. Stay listening so the
+ // second one is decisive.
+ let sig = shutdown_signal().await;
+ eprintln!("Second {sig} while shutting down — forcing exit");
+ std::process::exit(signal_exit_code(sig));
}
});
// rmcp >=1.4 marks StreamableHttpServerConfig #[non_exhaustive], so it
@@ -1262,10 +1283,20 @@ async fn async_main() -> anyhow::Result<()> {
let ct = CancellationToken::new();
tokio::spawn({
let ct = ct.clone();
+ let socket_path = socket_path.clone();
async move {
- shutdown_signal().await;
- eprintln!("Shutdown signal received — closing socket");
+ let sig = shutdown_signal().await;
+ eprintln!("{sig} received — closing socket");
ct.cancel();
+ // Same reasoning as the ServeHttp arm: tokio's handlers are
+ // installed for the process lifetime, so a second signal
+ // would be swallowed if the accept loop were slow to unwind.
+ // Unlink on the forced path too, so the escape hatch does
+ // not reintroduce the leaked socket this commit removes.
+ let sig = shutdown_signal().await;
+ eprintln!("Second {sig} while shutting down — forcing exit");
+ let _ = std::fs::remove_file(&socket_path);
+ std::process::exit(signal_exit_code(sig));
}
});
open_ontologies::socket::serve_with_shutdown(&socket_path, graph, ct).await?;