fix: treat missing taskkill process as terminated

Avoid retrying Windows MCP shutdown when taskkill reports the process has already exited. This keeps stale PID retries from targeting a reused process tree while preserving surfaced errors for other taskkill failures.

Co-authored-by: Codex <noreply@openai.com>
This commit is contained in:
jif-oai
2026-04-30 17:33:19 +01:00
co-authored by Codex
parent 64faf26d8c
commit f42c63a511
@@ -339,19 +339,20 @@ impl LocalProcessTerminator {
#[cfg(windows)]
fn terminate(&self) -> io::Result<()> {
let status = std::process::Command::new("taskkill")
let output = std::process::Command::new("taskkill")
.arg("/PID")
.arg(self.pid.to_string())
.arg("/T")
.arg("/F")
.status()?;
if status.success() {
Ok(())
} else {
Err(io::Error::other(format!(
"taskkill exited with status {status}"
)))
.output()?;
if output.status.success() || taskkill_output_reports_missing_process(&output) {
return Ok(());
}
Err(io::Error::other(format!(
"taskkill exited with status {}",
output.status
)))
}
#[cfg(not(any(unix, windows)))]
@@ -360,6 +361,19 @@ impl LocalProcessTerminator {
}
}
#[cfg(windows)]
fn taskkill_output_reports_missing_process(output: &std::process::Output) -> bool {
taskkill_text_reports_missing_process(&output.stdout)
|| taskkill_text_reports_missing_process(&output.stderr)
}
#[cfg(any(windows, test))]
fn taskkill_text_reports_missing_process(bytes: &[u8]) -> bool {
String::from_utf8_lossy(bytes)
.to_ascii_lowercase()
.contains("not found")
}
impl StdioServerProcessHandle {
fn local(program_name: String, terminator: Option<LocalProcessTerminator>) -> Self {
Self {
@@ -671,4 +685,18 @@ mod tests {
);
assert!(!env.contains_key("UNREQUESTED_SECRET"));
}
#[test]
fn taskkill_missing_process_output_is_treated_as_already_terminated() {
assert!(taskkill_text_reports_missing_process(
br#"ERROR: The process "1234" not found."#
));
}
#[test]
fn taskkill_other_failure_output_is_not_treated_as_missing_process() {
assert!(!taskkill_text_reports_missing_process(
b"ERROR: Access is denied."
));
}
}