Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -76,13 +82,14 @@ pub fn stop_process(pid: u32) -> Result<(), Box<dyn std::error::Error>> {
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
Expand Down