From 14858cf717cd02531752ff687e2d35a420493b02 Mon Sep 17 00:00:00 2001 From: vlascik Date: Tue, 28 Oct 2025 22:43:48 +0100 Subject: [PATCH] fix: gracefully close goosed listening port (#5321) 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 84e42b5565..a1eb422d97 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(()) }