Repository navigation
fix: wait for a stopped windows mcp server before deleting its capture - #47880
winnythedev wants to merge 1 commit into
Conversation
On Windows, close() stops a script-mode MCP server with kill_popen_tree, whose closing Popen.kill() runs after taskkill. TerminateProcess is refused on a process whose exit has begun, and CPython then stores the exit code, so the wait() that follows returns at once while the server still holds the stderr capture it inherited. Windows refuses the delete, close() logs "could not delete MCP stderr capture" and the file stays behind, which also fails the Windows test shard's strict residue check after the answer-time test. close() now clears the stored code before it waits, so wait() waits on the process handle Popen keeps open. A server that has already exited still returns at once. A new test drives the real close() Windows branch and the real kill_popen_tree over a fake server teardown. Fixes #47853
UX Review (Opus 5.5) — ⏭️ skippedRevision |
Design Review (Opus 5.5) — ✅ PASSDesign-level review of Design-Verdict: PASS Nothing blocks. The fix targets the real cause, the exit code that CPython's Windows Notes
[DESIGN-REVIEWED] 4054dbd Description read[DESCRIPTION-READ] 260f57e1686c8765300456e104a6d16a07106bf0cfc23b2dcb7a9e0069723cbe That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with |
First Principles Review (Opus 5.5) — ✅ PASSPremise-level review of First-Principles-Verdict: PASS The fix is in the caller, but the stale exit code comes from the shared helper. Whether to change the helper is outside this PR's stated goal, so that is a note, not a blocker. Not checked: whether CPython's Windows Notes
What this change shipsInventory (3 items) — 2 justifiedIntent: when a script cron stops an MCP server on Windows, it should delete the server's stderr capture file instead of leaving it behind with a warning. This is a FIX.
[FIRST-PRINCIPLES-REVIEWED] 4054dbd Description read[DESCRIPTION-READ] aed92b04f1323d61c153435d0ae5271f9b0b3e60852e387d12324e0ae905089f That sha256 names the PR description this verdict read. Recompute it with |
Opus 5.5 Review — ✅ no blocking findingsReviewed Verdict parsed from the review's SHA-scoped output markers for commit False positive or not applicable? A repository writer can comment: |
GPT 6.1 Review — ✅ no blocking findingsGPT 6.1 completed its review of This comment is updated in place on each push. Review detailsNo findings. False positive or not applicable? A repository writer can comment: |
Problem / Motivation
Goal: On Windows, a script cron's MCP server stop deletes the server's stderr capture file instead of leaving it behind with a warning.
On Windows,
McpToolClient.close()can leave anmcp-stderr-<random>.logfile in the temp folder and logcould not delete MCP stderr capture. The same race failsBackend Tests (Windows) (6)at session teardown, through the strict temp-residue check, aftertest/test_cron_script_mcp_answer_time.py::test_an_unanswered_call_raises_by_the_answer_time_and_stops_its_server, on PRs that do not touch this code (twice on 5 Dec, both on bases that already carry #47517).Why it matters
Every script cron that calls an MCP tool on Windows can leave a stray capture file per stopped server, and nothing deletes it later. On CI the leftover turns an unrelated PR's Windows shard red.
Not a goal
kill_popen_tree, job objects, process groups or any other process-isolation code..cmdwrapper case, where a grandchild server can still hold the capture aftercmd.exeexits.TimeoutExpiredfallback); it is named as a known limit in the code and the spec.What changed (motivation → approach → change)
close()callsplatform_compat.kill_popen_tree(proc)(taskkill /T /F, thenproc.kill()), thenproc.wait(timeout=_STOP_GRACE_SECS), then deletes the capture under_capture_lock. CPython's WindowsPopen.terminate()(whichkill()is) catches thePermissionErrorthatTerminateProcessraises on a process whose exit has begun, readsGetExitCodeProcess, and stores it asreturncode._wait()callsWaitForSingleObjectonly whilereturncode is None. Sowait()returns at once while the server still holds its inherited stderr handle, and Windows refuses the delete.close()clearsself._proc.returncodeafterkill_popen_treeand beforewait(), sowait()waits on the process handlePopenkeeps open. A server that has already exited still returns at once. Ashortcut:comment names the timeout-fallback limit. The "Script-mode MCP server lifetime" spec sentence indocs/system-specs/modules/learn-cron-dashboard.mdsays the same.Backwards compatibility
Compatible: only the Windows stop path waits for a server that is already being stopped; a server that exited returns at once, as before.
Removes nothing: the diff adds a wait on an existing path and takes away no key, record, path or function.
Tests
test/test_cron_script_mcp_close.py::test_windows_close_deletes_the_capture_only_once_the_stopped_server_lets_go_of_it(new): a fake Windows server teardown (the firstTerminateProcessstarts the stop, a second is refused, the exit code reads back, the process object is signalled only when the test releases it) under CPython's Windowskill/poll/wait. It drives the realclose()Windows branch and the realkill_popen_tree, withIS_POSIXandkill_process_treepatched through theplatform_compatfacade astest/test_facade_mirror_guards.pyrequires. It asserts the capture is gone and no WARNING was logged. It waits on events only, bounded bydefault_timeout().Proof: PROVEN test/test_cron_script_mcp_close.py::test_windows_close_deletes_the_capture_only_once_the_stopped_server_lets_go_of_it (head 4054dbd)
Verified by test on Linux x86_64 with the platform flag patched; this relies on CI's Windows lane for the real-platform run.
Checks run locally on Linux x86_64 (Python 3.12):
close()Windows branch and CPython's Windowskill/waitsemantics, refusing the delete while any process holds the file open: on main, forced 20 of 20 and natural 4 of 400 (given-up and end-of-run paths) left the capture with the product's WARNING; with the fix, 0 of 20 and 0 of 400.close()(deleted);taskkillfailing sokill()stops the server (deleted). Not covered: a.cmdwrapper's grandchild and a teardown longer than the grace wait (both out of scope, see Not a goal).test/test_cron_script*.pyandtest/test_facade_mirror_guards.py: 561 passed, 1 skipped.test/test_wait_discipline_ratchet.pyandtest/test_flake_pattern_ratchet.pygreen in committed-baseline and PR-base modes.test/test_interrupted_children_spawn_unblocked.pygreen.scripts/local-gate.py --tree-scansagainst upstream main: related tests 1150 passed. Tree-scan shard 2 passed. Shard 1 had 16 reds, all in files this PR does not touch, that come from this host (child interpreters that cannot load a system library, a missing pytest plugin, a coverage version, load-timed probes), plus one real red from this PR's first draft (test_interrupted_children_spawn_unblocked.pycounting the fake'sterminate()), fixed by renaming the fake's method and re-run green.Review (multi-model interrogate)
Independent reviewers (three valid reviews plus one re-check of the revised hunks) found no critical issue.
shortcut:comment names it); the test thread could outlive a failedwait_until(now released and joined infinally); the re-check's clearer wording of that comment.kill()stopped, and this change only adds that wait for a server whose teardown has already begun. "A secondclose()call" (warning): callers close once, fromKeptMcpServersand the run'sfinally.Manual verification
N/A — the fix is Windows-only logic, proven by the unit test and the repro above on Linux; CI's Windows lane runs it on the real platform. No live dashboard or gateway run was done.
Real-machine verification
Machine: own computer, Linux x86_64.
Before (main dcc6fd1):
python -m pytest test/test_cron_script_mcp_close.py -n0 -q-> 1 failed (close() deleted the capture before the server let go of it), 6 passed; the test repeated 200 times -> 200 failed; the forced repro -> 20 of 20 left the capture.After (head 4054dbd): the same commands -> 7 passed; 200 of 200 passed; the forced repro 0 of 20.
Cleanup: own computer; throwaway repro files removed from the tree.
Related Issues
Fixes #47853. Completes #47487 (fixed by #47517).
Overlap: open #43906 also edits
close()insrc/kiro_crew/cron_script_mcp.py(it adds a working-directory cleanup after the capture delete, a different line); #43611 edits other parts of the same file. Many open PRs editdocs/system-specs/modules/learn-cron-dashboard.md; this PR changes one sentence of "Script-mode MCP server lifetime" and is rebased onto current main.Sibling sweep
Swept: git grep -n 'kill_popen_tree(' -- src/
Sibling: src/kiro_crew/dashboard_verify.py:1016 -- not affected -- the same kill then wait shape, but its profile delete already retries until a deadline
Sibling: src/kiro_crew/mcp_gateway/footprint_probe.py:272 -- not affected -- deletes no file the child inherited after the wait
Sibling: src/kiro_crew/apps/git_install.py:270 -- not affected -- same kill then wait shape, but a checkout it cannot delete is reported through rmtree_force's result instead of left silently; out of this issue's scope
Pattern harvest
Not generalizable: only this caller deletes a file the stopped child inherited right after
kill_popen_treeandwait(), and changingkill_popen_treeitself is out of scope.Checklist
feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...); PR Hygiene does not count commitsContribution License Agreement
Contributions are accepted under the project's Apache-2.0 license; see the Licensing section of CONTRIBUTING.md.