Skip to main content

vtcode_core/
shutdown.rs

1//! Shared shutdown signal helpers.
2
3use std::io::Result as IoResult;
4
5/// Wait for a process shutdown signal.
6///
7/// On Unix this treats `SIGTERM` the same as `Ctrl+C` so long-lived services can
8/// drain gracefully when stopped by process supervisors.
9pub async fn shutdown_signal() -> IoResult<()> {
10    #[cfg(unix)]
11    {
12        use tokio::signal::unix::{SignalKind, signal};
13
14        let mut terminate = signal(SignalKind::terminate())?;
15        tokio::select! {
16            ctrl_c_result = tokio::signal::ctrl_c() => ctrl_c_result,
17            _ = terminate.recv() => Ok(()),
18        }
19    }
20
21    #[cfg(not(unix))]
22    {
23        tokio::signal::ctrl_c().await
24    }
25}
26
27/// Wait for a shutdown signal and log listener errors consistently.
28pub async fn shutdown_signal_logged(context: &'static str) {
29    if let Err(err) = shutdown_signal().await {
30        tracing::warn!("Failed to listen for {context} shutdown signal: {err}");
31    }
32}