Skip to content

fix(windows): skip orphans older than the root when a tree candidate vanishes unopened - #20407

Merged
bolichen97 merged 1 commit into
kirodotdev:mainfrom
rinbn:fix/runtime-cleanup-terminate-window-flake
Oct 29, 2026
Merged

bolichen97 merged 1 commit into
kirodotdev:mainfrom
rinbn:fix/runtime-cleanup-terminate-window-flake

Conversation

@rinbn

@rinbn rinbn commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: A Windows tree drain no longer refuses because an unrelated process, older than the root, still names a vanished tree member's recycled PID as its parent.

test_a_member_exiting_inside_the_terminate_window_reads_as_exited (#20380) failed on a Windows backend shard with OSError: Windows descendant ancestry incomplete: vanished unopened parents; ... [(6892, 6680), (6920, 6680)]. The first Toolhelp snapshot listed 6680 under the root, OpenProcess(6680) failed, and 6680 was gone from the fresh snapshot. The fresh snapshot still had 6892 and 6920 naming 6680 as their parent, so descendant_termination_handles raised before the drain reached the terminate window the test is about.

Why it matters

The red landed on #18285, which only touches ACP client files, and keeps PR Readiness red until someone with write access reruns the job. Outside tests, the same refusal can fail a real Windows tree cleanup whenever a short-lived member, such as a console host, exits before its handle opens and its PID once belonged to a process whose children are still running.

Not a goal

  • Changing the dangling-parent refusal for any entry that is not a provable stranger as defined below. Those still raise.
  • Changing _windows_foreign_descendant_pids or the check on unopened candidates that are still running. Both are byte-identical to base.
  • Changing the test in test_runtime_cleanup_windows.py. Removing the sleeper's console host there would hide the production refusal instead of fixing it.

What changed (motivation → approach → change)

Toolhelp never updates a parent PID after the parent exits, so an orphan keeps naming a PID that Windows can hand to a new process. The dangling-parent refusal in descendant_termination_handles (platform_compat_winproc.py, the owner module this function moved to in #22044) read every fresh entry that names a vanished unopened candidate as that candidate's child, with no age check, so an orphan created before the root still counted as a surviving grandchild of the owned tree.

The refusal now skips an entry that _windows_is_older_orphan proves is a stranger. A process that has held its PID since before the root cannot descend from it, and neither can any process naming that PID: it is the stranger's child, or the child of an earlier holder that died before the root started. That argument holds one level down only, because a grandchild's parent may have been created after the root on a PID recycled from one of the root's own processes. So an entry is a stranger only when:

  • its own creation instant, read from its handle or by PID, predates the root, and the first snapshot already listed it under the same parent;
  • every child either snapshot lists for it was opened in this call, so no retained, unpinned or newly started child is involved;
  • the fresh snapshot, taken after those handles opened, still lists each child under it, so no child's PID was reused before its handle opened;
  • none of those children has a child of its own in either snapshot.

Strangers and their children have their handles closed and leave the tree. This is deliberately narrower than the existing disproof for unopenable candidates, which drops a disproven node's whole numeric subtree; the spec paragraph says why the two must not be merged. Any other dangling pair raises as before. When no fresh entry names a vanished candidate, no query handle is opened. The platform-compat.md spec paragraph on the dangling-parent rule now states this exception.

The platform_compat.py facade forwards the new helper with the module's other names, and test/test_platform_compat_refactor_facade.py lists it among them.

Backwards compatibility

Compatible: the change only affects scans that raised vanished unopened parents before. In those, a provable stranger is skipped instead of raising.

Removes nothing.

Tests

All in TestWindowsRecycledDescendantAgeDisproof (test/test_platform_compat_windows_tree.py), on the class's fake Toolhelp and handle layer. Against the parent's platform_compat_winproc.py, only the four cases this PR is meant to let through fail, each with the message CI showed; every case that must keep refusing also refuses on the parent:

parent     FAILED ...[older-refused]
           FAILED ...[older-opened]
           FAILED ...test_an_older_orphan_is_skipped_with_its_pinned_childless_child
           FAILED ...test_an_older_orphan_closes_a_child_only_the_fresh_scan_lists_under_it
           (all: OSError: Windows descendant ancestry incomplete: vanished unopened parents; ...)
           4 failed, 21 passed, 1 skipped
this head  25 passed, 1 skipped
  • test_an_older_stranger_naming_a_vanished_candidate_is_not_its_child, six cases: 101 is listed under the root and then vanishes unopened; 102 and 103 still name it, refused or opened, created before the root, at the same instant, or after it. Created before the root, discovery returns no handles and closes the orphans' handles. Otherwise it still raises.
  • test_an_older_orphan_is_skipped_with_its_pinned_childless_child: an older orphan and its own opened, childless console host are both skipped.
  • test_an_older_orphan_closes_a_child_only_the_fresh_scan_lists_under_it: a PID the first snapshot lists under the root opens on a new child of the orphan, so its handle is closed with the orphan's.
  • These keep the refusal: test_an_older_orphan_does_not_vouch_for_a_grandchild_across_a_vanished_pid (the GPT 6.1 review's input: an orphan's unopened child vanished, and an opened process below it names that PID), test_an_older_orphan_whose_child_has_a_child_keeps_the_refusal, test_an_older_orphan_whose_child_gained_a_child_keeps_the_refusal (the child started a process between the snapshots), test_an_older_orphan_whose_child_pid_was_reused_keeps_the_refusal (the fresh snapshot lists the child under another parent), test_an_older_orphan_with_a_child_only_the_fresh_scan_lists_keeps_the_refusal (the GPT 6.1 review's second input: the orphan starts a child between the snapshots), test_an_older_orphan_whose_child_is_not_opened_here_keeps_the_refusal with the child refused or retained, test_a_reused_pid_under_a_vanished_candidate_keeps_the_refusal (a new process on an unopened PID reused between the snapshots), and test_an_older_ancestor_does_not_disprove_a_child_of_a_vanished_candidate (an old ancestor proves nothing about a newer child).
  • Mutations: < changed to <= fails [same-instant-opened]; dropping the opened-in-this-call rule fails both not_opened_here cases; dropping the first-snapshot childless rule, or letting grandchildren through, fails whose_child_has_a_child; dropping the fresh-snapshot childless rule fails whose_child_gained_a_child; dropping the fresh-parent rule fails whose_child_pid_was_reused; reading children from the first snapshot only fails with_a_child_only_the_fresh_scan_lists; closing only children the first snapshot lists under the orphan fails closes_a_child_only_the_fresh_scan_lists_under_it.
  • Determinism for test/test_platform_compat_windows_tree.py: 20/20 at -n0; 10/10 at -n 4 --dist loadgroup --max-worker-restart=2 beside test/test_runtime_cleanup_windows.py and test/test_platform_compat_kill_reap.py; and one shuffled run of all 73 ids (seed 9302), green.

Manual verification

N/A — the Toolhelp race cannot be produced from Linux. The faked snapshot sequence above reproduces the exact refusal, and the Windows backend shards run the original test on every PR.

Screenshots / video

N/A — no user-visible change.

Related Issues

Closes #20380

Pattern harvest

Not generalizable: the defect is one missing creation-order check in a single Windows discovery routine, not a pattern that recurs across call sites.

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title (feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff

Contribution License Agreement

@rinbn
rinbn requested review from a team and KiroCrewCaptain as code owners October 14, 2026 17:56
@rinbn
rinbn requested a review from mbriones98 October 14, 2026 17:56
@github-actions github-actions Bot added fork Pull request from a fork (external contributor) readiness: checking Automated validation is still running labels Oct 14, 2026
@github-actions

github-actions Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5, fork) — ✅ PASS

Premise-level review of 2cdba4d11b2c4a8124dca465097bd4f7789bed30 via the fork AI-review pipeline — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

First-Principles-Verdict: PASS

Nothing blocks. The fix works at the mechanism level: it adds the missing age check to the dangling-parent refusal, and the CI failure in #20380 gives it provenance. One small duplication is worth trimming.

Notes

  • Item 1 — oversized: _windows_is_older_orphan restates the pinned-handle / query-by-PID creation read (identity[1] if identity is not None and identity[0] == child_pid else None). Grepping identity\[1\] if identity is not None and identity\[0\] == finds 1 existing copy, the created() closure at platform_compat_winproc.py:297-307. This PR adds a second spelling. Subtraction: lift that closure into one module-level reader and call it from both places, deleting the inline copy in the new helper.

What this change ships

Inventory (4 items) — 3 justified

Intent: a Windows tree cleanup no longer refuses because an unrelated process, older than the root, still names a vanished member's recycled PID as its parent. This is a FIX.

  1. A Windows drain skips an older orphan and its pinned, childless children instead of raising "vanished unopened parents" — oversized (duplicate instant read)
  2. The handles of those orphans and their children are closed, so they leave the returned tree — justified
  3. The platform_compat facade forwards _windows_is_older_orphan, and the facade test lists it — justified
  4. The platform-compat.md dangling-parent paragraph states the exception and why it must not merge with the existing disproof — justified

[FIRST-PRINCIPLES-REVIEWED] 2cdba4d

Description read

[DESCRIPTION-READ] 797be831ca1f5da943f4db2894d4d713c9aad284000698a49e0671f6bface5fa

That sha256 names the PR description this verdict read. Recompute it with .github/scripts/pr-description-capture.sh; if it differs, the description changed after this verdict was formed and any finding drawn from the description is unproven.

@github-actions

github-actions Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5, fork) — ✅ PASS

Design-level review of 2cdba4d11b2c4a8124dca465097bd4f7789bed30 via the fork AI-review pipeline — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

Design-Verdict: PASS

Nothing blocks: the dangling-parent refusal is relaxed only for an entry proven older than the root, one level deep, with children pinned by handles opened in this call. Every other dangling pair still raises.

Notes

[DESIGN-REVIEWED] 2cdba4d

Description read

[DESCRIPTION-READ] 80077c520093d860cd4196a6d03d4f22b8a0cddba956acec7734da045c26c0d3

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 .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

github-actions Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5, fork) — ⏭️ skipped

Revision 2cdba4d11b2c4a8124dca465097bd4f7789bed30 touches no user-facing surface (no changes under website/ or committed screenshots), so the UX review was skipped. Does not block merge.

@github-actions

github-actions Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review (fork) — ✅ no blocking findings

Reviewed 2cdba4d11b2c4a8124dca465097bd4f7789bed30 via the fork AI-review pipeline; updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] 2cdba4d

@github-actions

github-actions Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review (fork) — ✅ no blocking findings

Reviewed 2cdba4d11b2c4a8124dca465097bd4f7789bed30 via the fork AI-review pipeline; updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] 2cdba4d

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Oct 14, 2026
@rinbn
rinbn force-pushed the fix/runtime-cleanup-terminate-window-flake branch from b2d3073 to b67c324 Compare October 14, 2026 22:29
@rinbn

rinbn commented Oct 14, 2026

Copy link
Copy Markdown
Contributor Author
  • span=eeb991b58a1d — Orphan age disproof drops a live descendant across a vanished PID

Fixed in b67c324. The orphan seeding of _windows_foreign_descendant_pids is reverted, so that helper and the unopened-candidate block are byte-identical to main. The dangling-parent refusal now skips an entry only when _windows_is_older_orphan proves it is a stranger: its own creation instant predates the root, and each child the first snapshot lists for it was opened in this call, is still listed under it by the fresh snapshot, and has no child in either snapshot. A process that has held its PID since before the root has no descendant of the root as a child, but that holds one level down only, so your input (102, its unopened child 103, then 104) now refuses: test_an_older_orphan_does_not_vouch_for_a_grandchild_across_a_vanished_pid. The repair ran on a model-pinned subagent; the GPT models listed for this account were restricted, so it fell back to Opus 5.5.

@rinbn

rinbn commented Oct 14, 2026

Copy link
Copy Markdown
Contributor Author
  • Check that the CI orphans (6892/6920) actually predate the root

The CI log cannot confirm it: the refusal prints only PID pairs, and Toolhelp carries no creation time. If they did not predate the root, _windows_is_older_orphan rejects them and discovery refuses exactly as on main, so this flake would stay but nothing main refuses gets certified. The [older-*] and [later-*] cases of test_an_older_stranger_naming_a_vanished_candidate_is_not_its_child pin both directions.

@rinbn

rinbn commented Oct 14, 2026

Copy link
Copy Markdown
Contributor Author
  • Check that the Windows backend shard runs the original test green on this head

On b2d3073 all eight Backend Tests (Windows) shards passed, and test_runtime_cleanup_windows.py runs on one of them. One green run says little about a race seen once in roughly 155 inspected runs, so the evidence this PR rests on is the faked snapshot sequence in TestWindowsRecycledDescendantAgeDisproof, which reproduces the exact refusal against main.

@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Oct 14, 2026
@rinbn
rinbn force-pushed the fix/runtime-cleanup-terminate-window-flake branch from b67c324 to 9c9a8dc Compare October 15, 2026 15:35
@rinbn

rinbn commented Oct 15, 2026

Copy link
Copy Markdown
Contributor Author
  • span=eeb991b58a1d — Fresh-only children bypass the vanished-parent refusal

Fixed in 9c9a8dc. _windows_is_older_orphan now takes the orphan's direct children from both snapshots, and every existing check runs on that set: opened in this call, still under the orphan in the fresh snapshot, childless in both. A child the orphan starts between the snapshots was never opened in this call, so your input (orphan 102 starting a new 103 while 104 survives under the old PID 103) now refuses: test_an_older_orphan_with_a_child_only_the_fresh_scan_lists_keeps_the_refusal. The close loop also closes a child the fresh snapshot lists under the orphan, so no stranger's handle is returned. The repair again ran on Opus 5.5, because the GPT models listed for this account are restricted.

@rinbn

rinbn commented Oct 15, 2026

Copy link
Copy Markdown
Contributor Author
  • span=640bfbf13e63 — Say in the spec paragraph why the two age-disproof rules differ

Done in 9c9a8dc. The platform-compat.md paragraph now says this exception is deliberately narrower than the creation-order disproof for unopenable candidates, which drops a disproven node's whole numeric subtree: skipping the dangling-parent refusal is sound only one level below a process proven older than the root, and only for children pinned as described, so the two rules must not be merged.

@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention merge conflict Branch has merge conflicts with its base — author must resolve before merge and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Oct 15, 2026
@rinbn
rinbn force-pushed the fix/runtime-cleanup-terminate-window-flake branch from 9c9a8dc to e5b65ce Compare October 17, 2026 20:31
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention merge conflict Branch has merge conflicts with its base — author must resolve before merge and removed merge conflict Branch has merge conflicts with its base — author must resolve before merge readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Oct 17, 2026
@rinbn
rinbn force-pushed the fix/runtime-cleanup-terminate-window-flake branch from e5b65ce to df2a933 Compare October 22, 2026 18:22
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed merge conflict Branch has merge conflicts with its base — author must resolve before merge readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Oct 22, 2026
…vanishes unopened

The dangling-parent refusal read a fresh entry whose stale parent PID named a
vanished candidate as that candidate's child, even when the entry was created
before the root and so cannot descend from it. Skip such an entry when its own
creation instant predates the root, together with its children from either
snapshot, but only when each of those children was opened in this call, is
still listed under it in the fresh snapshot, and has no child of its own.
Anything else still refuses.
@iamwhatever
iamwhatever force-pushed the fix/runtime-cleanup-terminate-window-flake branch from df2a933 to 2cdba4d Compare October 28, 2026 06:42
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: passed Eligible automated validation passed for the current revision and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Oct 28, 2026
@iamwhatever iamwhatever self-assigned this Oct 28, 2026
@iamwhatever

Copy link
Copy Markdown
Contributor

Holding for a human reviewer on head 2cdba4d.

This changes descendant_termination_handles, which decides which processes a Windows tree drain terminates. An orphan proven older than the root, and its pinned childless children, now leave the kill set instead of making the drain refuse (platform_compat_winproc.py#L483-L516). Changes to process-killing paths are reserved for a human, so this is a policy hold, not a code objection.

I found no defect. The proof in _windows_is_older_orphan (L330-L376) holds one level down: a process that has held its PID since before the root cannot be the root's descendant, and neither can a direct child naming that PID. Grandchildren, unpinned children, children that appear between scans and reused PIDs still refuse. Retained handles are never closed by the new loop. The two GPT 6.1 findings and the Design note were answered with fixes and tests, and all checks on this head are green.

Non-blocking: the First Principles note that the creation read at L375 repeats L305 is a style point with no behaviour impact.

What clears it: a maintainer review of the termination-set change.

— crew-pr-reviewer (r133)

@bolichen97
bolichen97 merged commit 9d4e826 into kirodotdev:main Oct 29, 2026
114 checks passed
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fork Pull request from a fork (external contributor)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flaky: test_a_member_exiting_inside_the_terminate_window_reads_as_exited (Windows) raises "vanished unopened parents" for orphans older than the root

3 participants