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-unix): honour the same shutdown signals and unlink the socket

Nicolas Geysseaa0a3f018c0a
diff --git a/src/main.rs b/src/main.rs
index 12708a33..609b799e 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1253,7 +1253,22 @@ async fn async_main() -> anyhow::Result<()> {
}
}
eprintln!("Graph has {} triples total", graph.triple_count());
- open_ontologies::socket::serve(&socket_path, graph).await?;
+
+ use tokio_util::sync::CancellationToken;
+ // Same rationale as the ServeHttp arm: this token is the sole
+ // shutdown trigger. Without the task below, the accept loop in
+ // socket::serve_with_shutdown never exits, the process can only be
+ // killed, and the socket file stays on disk after it is gone.
+ let ct = CancellationToken::new();
+ tokio::spawn({
+ let ct = ct.clone();
+ async move {
+ shutdown_signal().await;
+ eprintln!("Shutdown signal received — closing socket");
+ ct.cancel();
+ }
+ });
+ open_ontologies::socket::serve_with_shutdown(&socket_path, graph, ct).await?;
}
#[cfg(windows)]
Commands::ServeUnix { .. } => {
diff --git a/src/socket.rs b/src/socket.rs
index 483b1dee..2e28cc41 100644
--- a/src/socket.rs
+++ b/src/socket.rs
@@ -10,6 +10,7 @@ use std::sync::Arc;
use serde::{Deserialize, Serialize};
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::net::UnixListener;
+use tokio_util::sync::CancellationToken;
use tracing::{error, info, warn};
use crate::graph::GraphStore;
@@ -56,6 +57,24 @@ struct ConsistencyResponse {
/// Each accepted connection is handled in a spawned task. The listener
/// runs until the process is killed or the socket is removed.
pub async fn serve(socket_path: &str, graph: Arc<GraphStore>) -> anyhow::Result<()> {
+ // A token nobody holds is never cancelled, so this keeps the historical
+ // "accept forever" behaviour for existing callers.
+ serve_with_shutdown(socket_path, graph, CancellationToken::new()).await
+}
+
+/// Same as [`serve`], but stops accepting when `shutdown` is cancelled and
+/// unlinks the socket path on the way out.
+///
+/// Without this the accept loop is unconditionally infinite: the process can
+/// only be killed, and the socket file it bound is left on disk afterwards, so
+/// anything that treats the path's existence as liveness sees a server that is
+/// no longer there. The next `serve` call papers over it by unlinking a stale
+/// path at bind time, which hides the leak rather than avoiding it.
+pub async fn serve_with_shutdown(
+ socket_path: &str,
+ graph: Arc<GraphStore>,
+ shutdown: CancellationToken,
+) -> anyhow::Result<()> {
// Clean up stale socket file if it exists.
let _ = std::fs::remove_file(socket_path);
@@ -63,20 +82,32 @@ pub async fn serve(socket_path: &str, graph: Arc<GraphStore>) -> anyhow::Result<
info!("Unix socket listening on {socket_path}");
loop {
- match listener.accept().await {
- Ok((stream, _addr)) => {
- let g = graph.clone();
- tokio::spawn(async move {
- if let Err(e) = handle_connection(stream, &g).await {
- warn!("Connection error: {e}");
- }
- });
+ tokio::select! {
+ _ = shutdown.cancelled() => {
+ info!("Shutdown requested — no longer accepting on {socket_path}");
+ break;
}
- Err(e) => {
- error!("Accept error: {e}");
+ accepted = listener.accept() => match accepted {
+ Ok((stream, _addr)) => {
+ let g = graph.clone();
+ tokio::spawn(async move {
+ if let Err(e) = handle_connection(stream, &g).await {
+ warn!("Connection error: {e}");
+ }
+ });
+ }
+ Err(e) => {
+ error!("Accept error: {e}");
+ }
}
}
}
+
+ // Release the bound address before unlinking, then remove the path so the
+ // next bind starts clean and nothing mistakes the file for a live server.
+ drop(listener);
+ let _ = std::fs::remove_file(socket_path);
+ Ok(())
}
// ── Per-connection handler ───────────────────────────────────────────