fix: gracefully close goosed listening port (#5321)

Signed-off-by: V. Lascik <vlascik@users.noreply.github.com>
This commit is contained in:
vlascik
2025-10-28 22:43:48 +01:00
committed by GitHub
parent 5377b6df02
commit 14858cf717
+24 -1
View File
@@ -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(())
}