{"record":{"id":"79915589bb67303b","repo":"nikivdev/code","slug":"taskkill-exited-with-status","errorCode":null,"errorMessage":"taskkill exited with status {}","messagePattern":"taskkill exited with status (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/daemon.rs","lineNumber":699,"sourceCode":"        if status.success() || pgid_kill.map(|s| s.success()).unwrap_or(false) {\n            return Ok(());\n        }\n        bail!(\n            \"kill command exited with status {}\",\n            status.code().unwrap_or(-1)\n        );\n    }\n\n    #[cfg(windows)]\n    {\n        let status = Command::new(\"taskkill\")\n            .args([\"/PID\", &pid.to_string(), \"/F\", \"/T\"]) // /T kills child processes too\n            .status()\n            .context(\"failed to invoke taskkill\")?;\n        if status.success() {\n            return Ok(());\n        }\n        bail!(\n            \"taskkill exited with status {}\",\n            status.code().unwrap_or(-1)\n        );\n    }\n}\n\n/// Extract port number from a URL like \"http://127.0.0.1:7201/health\"\npub fn extract_port_from_url(url: &str) -> Option<u16> {\n    // Simple extraction: find the port after the last colon before any path\n    let url = url\n        .strip_prefix(\"http://\")\n        .or_else(|| url.strip_prefix(\"https://\"))?;\n    let host_port = url.split('/').next()?;\n    let port_str = host_port.rsplit(':').next()?;\n    port_str.parse().ok()\n}\n\n/// Kill any process listening on the given port.","sourceCodeStart":681,"sourceCodeEnd":717,"githubUrl":"https://github.com/nikivdev/code/blob/a747e741ae92c09071d0ae946ab48488adcff1ce/src/daemon.rs#L681-L717","documentation":"On Windows, terminate_process uses `taskkill /PID <pid> /F /T`; if the command exits non-zero the library bails with `taskkill exited with status <code>`. It indicates Windows refused the forced termination of the daemon process tree (/T also targets children).","triggerScenarios":"stop_daemon_with_path, start_daemon_inner restart, or kill_process_on_port invoking terminate_process when the PID is invalid/already exited, the process runs elevated while the CLI does not, or the PID was reused by a protected system process.","commonSituations":"Daemon started from an Administrator shell but stopped from a normal one; stale PID file after a reboot; antivirus or a protected process rejecting the force kill; PID reuse assigning the old number to a system process.","solutions":["Verify the PID is still the daemon: `tasklist /FI \"PID eq <pid>\"`; refresh the PID file if stale.","Run the stop/restart command from an elevated (Administrator) prompt.","Kill by image name as fallback: `taskkill /IM <daemon.exe> /F`.","Reboot or use Task Manager to terminate a protected/elevated process manually."],"exampleFix":"// before: stop from non-elevated shell fails\nPS> f daemon stop  // taskkill exited with status 1\n// after: elevated shell\nPS (admin)> f daemon stop","handlingStrategy":"try-catch","validationCode":"// PowerShell: verify the PID exists and is the daemon before taskkill\n$proc = Get-Process -Id $pid -ErrorAction SilentlyContinue\nif ($null -eq $proc) { Write-Output 'already stopped' }\nelseif ($proc.ProcessName -ne 'myapp-daemon') { Write-Output 'PID reused by another process; aborting' }","typeGuard":null,"tryCatchPattern":"match terminate_process(pid) {\n    Ok(()) => println!(\"daemon stopped\"),\n    Err(e) if e.to_string().contains(\"taskkill exited\") => {\n        eprintln!(\"taskkill failed for pid {pid} — run from an elevated prompt or kill via Task Manager.\");\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Start and stop the Windows daemon from the same privilege level (both elevated or both normal).","Validate PID liveness (tasklist) before taskkill; stale PID files are the top cause.","On daemon exit, delete the PID file in a Drop/exit handler.","Fall back to `taskkill /IM <exe> /F` by image name when PID kill fails."],"tags":["windows","taskkill","process-termination"],"backgroundTag":"kill-command-failed","analyzedSha":"a747e741ae92c09071d0ae946ab48488adcff1ce","analyzedAt":"2026-09-01T22:43:55.719Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}