From 6d96d713fa054c2170ef8665098078aafef397ae Mon Sep 17 00:00:00 2001 From: "V. Lascik" Date: Wed, 22 Oct 2025 18:53:41 +0200 Subject: [PATCH] fix: gracefully close goosed listening port Signed-off-by: V. Lascik --- crates/goose-server/src/commands/agent.rs | 25 ++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/crates/goose-server/src/commands/agent.rs b/crates/goose-server/src/commands/agent.rs index 84e42b556527..a1eb422d970f 100644 --- a/crates/goose-server/src/commands/agent.rs +++ b/crates/goose-server/src/commands/agent.rs @@ -8,6 +8,25 @@ use tracing::info; use goose::providers::pricing::initialize_pricing_cache; +// Graceful shutdown signal +#[cfg(unix)] +async fn shutdown_signal() { + use tokio::signal::unix::{signal, SignalKind}; + + let mut sigint = signal(SignalKind::interrupt()).expect("failed to install SIGINT handler"); + let mut sigterm = signal(SignalKind::terminate()).expect("failed to install SIGTERM handler"); + + tokio::select! { + _ = sigint.recv() => {}, + _ = sigterm.recv() => {}, + } +} + +#[cfg(not(unix))] +async fn shutdown_signal() { + let _ = tokio::signal::ctrl_c().await; +} + pub async fn run() -> Result<()> { // Initialize logging and telemetry crate::logging::setup_logging(Some("goosed"))?; @@ -42,6 +61,10 @@ pub async fn run() -> Result<()> { let listener = tokio::net::TcpListener::bind(settings.socket_addr()).await?; info!("listening on {}", listener.local_addr()?); - axum::serve(listener, app).await?; + // Ensure the listener/socket is properly closed on cancellation by using graceful shutdown + axum::serve(listener, app) + .with_graceful_shutdown(shutdown_signal()) + .await?; + info!("server shutdown complete"); Ok(()) }