Repository navigation
fix(windows): settle a cancelled descendant scan before releasing its handles - #26113
Conversation
UX Review (Opus 5.5) — ⏭️ skippedRevision |
First Principles Review (Opus 5.5) — ✅ PASSPremise-level review of First-Principles-Verdict: PASS Every item fixes the reported handle leak (#23108) at its cause, a started executor job that cannot be cancelled. The settle loop is a third copy of an existing shape: a note, not a blocker. Notes
What this change shipsInventory (3 items) — 3 justifiedIntent: on Windows, cancelling a prerequisite spawn no longer leaks process handles or closes handles a scan is still reading. This is a FIX.
[FIRST-PRINCIPLES-REVIEWED] a07af6b Description read[DESCRIPTION-READ] 9252d324524c31772b9f0f6dda1c15378c4c33866b803a18e73214c9d78ead58 That sha256 names the PR description this verdict read. Recompute it with |
Design Review (Opus 5.5) — ✅ PASSDesign-level review of Design-Verdict: PASS Nothing blocks: this is the repo's existing shield-and-settle pattern applied at the one site that hands back handle ownership, and the scan is bounded by the identity limit. Notes
[DESIGN-REVIEWED] a07af6b Description read[DESCRIPTION-READ] 96edc73e041669c8b4eb8d5a8d04eb20b094886135b7b348f7edeb51d0ce2582 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 |
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: |
… handles descendant_termination_handles_async awaited run_in_executor directly. Cancelling that await cancels only the asyncio future: a worker already running keeps reading the caller's root and retained handles and keeps opening new descendant handles, whose result is then discarded. The prerequisite-process supervisor cancels its tracker in `finally` and closes those borrowed handles right after, so they could close under the worker, and the worker's new handles leaked. The wrapper now awaits the worker through asyncio.shield. On cancellation it waits for the worker to settle (absorbing repeated cancellations), closes every handle a successful worker opened, since the cancelled caller never receives them, and re-raises. A failed worker has already closed what it opened, so nothing more is closed. Fixes #23108
7a5f9b8 to
a07af6b
Compare
buluoray
left a comment
There was a problem hiding this comment.
🤖 Kiro Crew Auto-Pipeline [operator: buluoray]:
Approving.
The fix matches the mechanism: a started executor job cannot be cancelled, so the old wrapper released the caller while the worker still read the borrowed root and retained handles, and the handles the worker opened afterwards reached nobody. Shielding the future, settling it across repeated cancels, closing only a successful result's handles, and then re-raising closes both holes. Checking worker.cancelled() before worker.exception() also covers an executor shutdown that cancels the queued job.
The tests pin both halves. Removing the close loop fails both test_cancel_settles_the_worker_and_closes_its_new_handles cases ([] == [9001, 9002]), and dropping the settle loop fails those two plus test_cancel_with_a_failing_worker_closes_nothing_and_still_cancels on assert not task.done().
The added wait on cancel is one bounded scan, and it runs on the private spawn loop, not the gateway loop, so it costs nothing visible.
Follow-up (non-blocking): docs/system-specs/common/platform-compat.md documents descendant_termination_handles but not this cancellation contract of the async variant. One sentence there saying a cancelled call settles the scan, closes the handles it opened and then re-raises would keep the spec in step.
|
Holding for human review because the PR touches the Windows process-termination path. The code looks correct, and I found no defect. Why HOLD: The fix matches the mechanism. A started executor job cannot be cancelled, so the old wrapper released the caller while the worker still read the borrowed handles, and the handles the worker opened afterwards were never closed. Shielding the future, settling it across repeated cancels, closing a successful result's handles and then re-raising closes both holes. Also for the reviewer: AI findings judged non-blocking:
All checks on this head are green and the branch merges cleanly with main. I read the test source and CI; I did not run tests locally or on Windows. Reviewed at a07af6b. — crew-pr-reviewer (r136) |
KiroCrewCaptain
left a comment
There was a problem hiding this comment.
Tier 1 auto-approve: fix (2 files). Criteria: no conflict, no requested changes, security path denylist clean, design-doc gate clean, security checklist all-NO, no reviewer-lane findings, readiness signal fresh for this head SHA, declared category matches the diff. Category: a cancelled Windows descendant scan settles its worker and closes the handles it opened; one module plus tests.
Problem / Motivation
Goal: On Windows, a cancelled descendant-process scan no longer leaks the process handles it opened, and its caller no longer closes handles the scan is still reading.
platform_compat.descendant_termination_handles_asyncawaitedloop.run_in_executordirectly. Cancelling the await cancels only the asyncio future. A worker that has already started keeps running: it keeps reading the caller's root and retained handles and keeps opening exact handles for new descendants. Its result is then thrown away, so nobody closes those new handles. The caller is released at once, so it can close the root and retained handles while the worker still uses them.There is a real caller that hits this:
kiro_prerequisite_process._run_processcancels and joins its descendant tracker infinally, then closes its retained and root handles. The tracker can be waiting on a scan at that moment.Why it matters
Every cancelled or timed-out prerequisite spawn on Windows can leak kernel process handles, which also pin the process objects they name. A long-running gateway keeps those for its whole life. Closing a handle under a running scan can also make the scan read a closed or reused handle value.
Not a goal
descendant_termination_handles), tracked separately in fix(windows): skip orphans older than the root when a tree candidate vanishes unopened #20407.run_in_executorcall sites inplatform_compat, none of which hands handle ownership back (see Sibling sweep).What changed (motivation → approach → change)
Symptom: a cancelled scan's new handles are never closed, and the borrowed handles can close under it. Root cause: a started executor job cannot be cancelled, but the wrapper let the cancellation through straight away and dropped the job's result.
The wrapper in
src/kiro_crew/platform_compat_winproc.pynow awaits the job throughasyncio.shield. When it is cancelled it:CancelledErrorAn uncancelled call is unchanged: it returns the job's handles to the caller. This is the same settle-then-re-raise shape
platform_compat_wintreealready uses for its owned-tree drain.Backwards compatibility
Compatible: the function's signature and its uncancelled result are unchanged; a cancelled call still raises
CancelledError, only later (once the bounded scan settles).Removes nothing: the change only adds a settle step on the cancellation path.
Tests
test/test_platform_compat_coverage_failure_paths.py::TestDescendantHandlesAsyncWindowsdrives the wrapper with an executor whosesubmitreturns a future already marked running, then completes it by hand (synthetic handles, no real process):CancelledError;CancelledError;Proof: PROVEN test/test_platform_compat_coverage_failure_paths.py::TestDescendantHandlesAsyncWindows::test_cancel_settles_the_worker_and_closes_its_new_handles[1], test/test_platform_compat_coverage_failure_paths.py::TestDescendantHandlesAsyncWindows::test_cancel_settles_the_worker_and_closes_its_new_handles[3], test/test_platform_compat_coverage_failure_paths.py::TestDescendantHandlesAsyncWindows::test_cancel_with_a_failing_worker_closes_nothing_and_still_cancels (head a07af6b)
Determinism: the class passed 20 repeats at
-n0, the whole file 10 repeats at-n 4, and a shuffled node order. Local gates: related backend tests (792 passed), isort, flake8, mypy, semgrep rules on the diff, flake-pattern ratchet.Manual verification
N/A for the unit path: the cancellation logic is plain asyncio and is exercised on Linux with a controlled executor. The real Win32 handle path (an actual cancelled prerequisite spawn on native Windows) is not exercised by CI; a hand test on Windows would confirm no handle growth after repeated cancelled spawns.
Related Issues
Fixes #23108
Sibling sweep
Swept: git grep -nE "run_in_executor(|to_thread(" -- src/kiro_crew/platform_compat*.py
Sibling: src/kiro_crew/platform_compat_kill.py:324 -- not affected -- returns a kill result, transfers no handle
Sibling: src/kiro_crew/platform_compat_kill.py:339 -- not affected -- returns a kill result, transfers no handle
Sibling: src/kiro_crew/platform_compat_wintree.py:312 -- not affected -- returns an identity tuple, the handle stays pinned in its cleanup state
Sibling: src/kiro_crew/platform_compat_wintree.py:657 -- not affected -- already shields and settles its drain before re-raising
Sibling: src/kiro_crew/platform_compat_runtime.py:291 -- not affected -- exit-time log drain, owns no handle
Pattern harvest
Rule candidate: review-prompt
Pattern: an
await loop.run_in_executor(...)whose job opens resources the caller must close, or borrows resources the caller closes on cancellation, needs a shield-and-settle onCancelledError.Checklist
feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)