From 209e46308291e88de931729d417c07be7227da48 Mon Sep 17 00:00:00 2001 From: bianbiandashen <282758717@qq.com> Date: Sat, 14 Feb 2026 14:29:11 +0800 Subject: [PATCH] refactor: extract shutdown timeout magic numbers to constants - Add GRACEFUL_SHUTDOWN_TIMEOUT_MS (10 seconds) - Add SHUTDOWN_POLL_INTERVAL_MS (100ms) - Makes the shutdown behavior more self-documenting and easier to tune --- src/process.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) 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