diff --git a/src/process.rs b/src/process.rs index 8543e25..e252f96 100644 --- a/src/process.rs +++ b/src/process.rs @@ -6,6 +6,12 @@ use std::path::PathBuf; use std::time::{Duration, SystemTime, UNIX_EPOCH}; use tracing::info; +/// Maximum time to wait for graceful shutdown before sending SIGKILL (in milliseconds). +const GRACEFUL_SHUTDOWN_TIMEOUT_MS: u64 = 10_000; + +/// Interval between checks when waiting for process to exit (in milliseconds). +const SHUTDOWN_POLL_INTERVAL_MS: u64 = 100; + /// Default configuration directory. pub const CONFIG_DIR: &str = "/etc/clawshell"; @@ -76,13 +82,14 @@ pub fn stop_process(pid: u32) -> Result<(), Box> { signal::kill(nix_pid, Signal::SIGTERM) .map_err(|e| format!("Failed to send SIGTERM to process {}: {}", pid, e))?; - // Wait for the process to exit (up to 10 seconds) - for _ in 0..100 { + // Wait for the process to exit gracefully + let max_iterations = GRACEFUL_SHUTDOWN_TIMEOUT_MS / SHUTDOWN_POLL_INTERVAL_MS; + for _ in 0..max_iterations { if !is_process_running(pid) { remove_pid_file(); return Ok(()); } - std::thread::sleep(Duration::from_millis(100)); + std::thread::sleep(Duration::from_millis(SHUTDOWN_POLL_INTERVAL_MS)); } // Force kill if still running