{"record":{"id":"e05109b8449870d9","repo":"nikivdev/code","slug":"kill-failed","errorCode":null,"errorMessage":"kill failed","messagePattern":"kill failed","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/log_server.rs","lineNumber":298,"sourceCode":"\nfn process_alive(pid: u32) -> bool {\n    Command::new(\"kill\")\n        .arg(\"-0\")\n        .arg(pid.to_string())\n        .status()\n        .map(|s| s.success())\n        .unwrap_or(false)\n}\n\nfn terminate_process(pid: u32) -> Result<()> {\n    let status = Command::new(\"kill\")\n        .arg(pid.to_string())\n        .status()\n        .context(\"failed to kill process\")?;\n    if status.success() {\n        Ok(())\n    } else {\n        bail!(\"kill failed\")\n    }\n}\n\nasync fn health() -> impl IntoResponse {\n    Json(json!({ \"status\": \"ok\" }))\n}\n\n#[derive(Debug, Deserialize)]\nstruct CodexSkillsQuery {\n    path: Option<String>,\n    limit: Option<usize>,\n}\n\n#[derive(Debug, Deserialize)]\nstruct CodexEvalQuery {\n    path: Option<String>,\n    limit: Option<usize>,\n}","sourceCodeStart":280,"sourceCodeEnd":316,"githubUrl":"https://github.com/nikivdev/code/blob/a747e741ae92c09071d0ae946ab48488adcff1ce/src/log_server.rs#L280-L316","documentation":"This error is thrown by terminate_process when a `kill` command was executed against a PID but the command exited with a non-zero status. It means the OS refused or failed to terminate the target process (the earlier `.context(\"failed to kill process\")` only covers spawn/IO errors). Callers (ensure_server, stop_server) surface it when trying to stop a previously running server instance.","triggerScenarios":"Calling stop_server or ensure_server when the recorded PID no longer exists (stale PID file), the process is owned by another user (permission denied), or the PID was reused by an unrelated process.","commonSituations":"A server crashed and left a stale PID file; running under a different user than the one that started the server; PID recycled by the OS after reboot.","solutions":["Check the process with `ps -p <pid>` — if it does not exist, delete the stale PID file and retry.","Run the stop/ensure command as the same user (or with sudo) that owns the target process.","Manually kill the process (`kill -9 <pid>`) if it ignores SIGTERM, then clean up the PID file.","Update the PID file to point at the correct current server PID before calling stop_server again."],"exampleFix":"// before\nensure_server(&pid_path)?; // fails with \"kill failed\" on stale pid\n// after\nif let Ok(pid) = std::fs::read_to_string(&pid_path) {\n    if !process_exists(pid.trim()) {\n        let _ = std::fs::remove_file(&pid_path); // clear stale pid first\n    }\n}\nensure_server(&pid_path)?;","handlingStrategy":"try-catch","validationCode":"let pid = std::fs::read_to_string(\"server.pid\")?;\nlet alive = std::process::Command::new(\"ps\")\n    .args([\"-p\", pid.trim()])\n    .status()\n    .map(|s| s.success())\n    .unwrap_or(false);\nif !alive { let _ = std::fs::remove_file(\"server.pid\"); }","typeGuard":"fn process_alive(pid: u32) -> bool {\n    std::process::Command::new(\"kill\").args([\"-0\", &pid.to_string()]).status().map(|s| s.success()).unwrap_or(false)\n}","tryCatchPattern":"match stop_server(&pid_path) {\n    Ok(()) => {},\n    Err(e) if e.to_string() == \"kill failed\" => {\n        // stale pid or permission issue: clear pid file, check ownership, retry once\n        let _ = std::fs::remove_file(&pid_path);\n        stop_server(&pid_path)?;\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Check the PID exists (kill -0 or ps -p) before attempting to stop it.","Clean up PID files when the server shuts down normally.","Run stop/ensure commands as the same user that started the server.","After reboot, assume PID files are stale and validate before use."],"tags":["process-management","kill","signals","stale-pid"],"backgroundTag":"process-kill-failed","analyzedSha":"a747e741ae92c09071d0ae946ab48488adcff1ce","analyzedAt":"2026-09-01T22:43:55.719Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}