Harden local MCP server process tree cleanup (#37366)

## Why

A local stdio MCP server can exit while its descendants remain alive. Cleaning up
only the server process can therefore leak processes after client shutdown.

## What changed

- Contain locally launched MCP servers in non-breakaway Windows job objects and
  terminate the job during shutdown so descendants exit with the server.
- Preserve compatibility when job creation or assignment is unavailable by
  retrying without containment and terminating an owned process handle instead.
- Use the process-group member fallback when terminating piped processes on
  macOS.

## Testing

- Cover descendant cleanup and breakaway prevention for both local MCP protocol
  modes on Windows.
- Cover the Windows nested-job assignment fallback and verify the suspended
  process resumes before handle-based termination.

GitOrigin-RevId: 33d3cc122de8f7ce0ae69a649b12ea0ff64b7c6f
This commit is contained in:
Cam moten authored and copyberry committed 2026-08-07 03:34:49 +00:00
1 parent 81b9bc2109
commit 9daa491f7c
6 files changed
+440 -55

No files matched your search

+59
View File
@@ -241,6 +241,65 @@ async fn contained_spawn_owns_immediate_descendant() -> anyhow::Result<()> {
Ok(())
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn rejected_job_assignment_resumes_existing_job_member() -> anyhow::Result<()> {
let Some(python) = find_python() else {
eprintln!("python not found; skipping Windows nested-job fallback test");
return Ok(());
};
let owning_job = crate::JobObject::create()?;
let rejected_job = crate::JobObject::create_without_breakaway()?;
let mut occupied_command = Command::new(&python);
occupied_command
.args(["-c", "import time; time.sleep(60)"])
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null());
let mut existing_member = rejected_job.spawn_contained(&mut occupied_command)?;
let mut command = Command::new(&python);
command
.args([
"-u",
"-c",
"import time; print('resumed',flush=True); time.sleep(60)",
])
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::null());
rejected_job.prepare_suspended_spawn(&mut command);
let mut root = command.spawn()?;
let process_handle = root
.raw_handle()
.ok_or_else(|| anyhow::anyhow!("missing suspended process handle"))?;
owning_job.assign_process(process_handle)?;
let process_id = root
.id()
.ok_or_else(|| anyhow::anyhow!("missing suspended process id"))?;
assert!(
!rejected_job.assign_and_resume_process(process_id)?,
"unrelated nested job unexpectedly accepted the process"
);
let stdout = root
.stdout
.take()
.ok_or_else(|| anyhow::anyhow!("missing resumed process stdout"))?;
let mut stdout = BufReader::new(stdout);
let mut marker = String::new();
tokio::time::timeout(Duration::from_secs(10), stdout.read_line(&mut marker)).await??;
assert_eq!(marker.trim(), "resumed");
let process_handle = crate::JobObject::open_process_handle(process_id)?;
crate::JobObject::terminate_process_handle(&process_handle)?;
rejected_job.terminate()?;
let status = tokio::time::timeout(Duration::from_secs(10), root.wait()).await??;
assert_eq!(status.code(), Some(1));
tokio::time::timeout(Duration::from_secs(10), existing_member.wait()).await??;
Ok(())
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn conpty_delivers_input_to_foreground_children() -> anyhow::Result<()> {
let Some(python) = find_python() else {