Skip to content

fix(process-host): let a slow Windows taskkill finish instead of orphaning the grandchild - #29501

Merged
nwparker merged 5 commits into
mainfrom
fix/windows-taskkill-background-completion
Oct 21, 2026
Merged

nwparker merged 5 commits into
mainfrom
fix/windows-taskkill-background-completion

Conversation

@nwparker

@nwparker nwparker commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

ELI5

When Orca stops a command on Windows, it asks Windows' taskkill tool to end the command and everything the command started. On a busy machine taskkill can take many seconds. Orca used to give up after 5 seconds, kill only the top process, and leave the command's children running. Now Orca stops waiting after 5 seconds but lets taskkill keep going in the background until it finishes. Nothing new waits on that background cleanup: the next git command is not held for it.

What Changed

Before (Windows, loaded machine): a git command (or any other "termination barrier" command) times out or is cancelled. taskkill /t /f is still walking the process tree when its 5 s cap hits. Orca kills that taskkill, then kills the top process directly. With the top process gone, taskkill can no longer find its children, so they (for example a node started by a .cmd) keep running.

After:

  • One tracked taskkill per process tree (new src/packages/process-host/src/windows-tree-kill.ts). It is keyed by the child-process handle, not the pid, so a reused pid can never join another tree's kill. A second stop or force request for the same child joins the running taskkill instead of starting another one.
  • The caller's wait and the cleanup's lifetime are separate. A caller still waits at most 5 s and is told "not verified" if taskkill is not done. The taskkill itself keeps running for up to 30 s.
  • The top process is not killed while taskkill is still running. Killing it early is exactly what orphaned the children. The tracked kill does the root-by-handle fallback itself, only once taskkill has failed or been abandoned.
  • taskkill is abandoned 2 s after the top process exits. Once the top process is gone, Windows may hand its pid to another process (possibly one of Orca's own, Windows PTY teardown: ancestry own is not true root identity (PID recycle into Orca tree) #10680), and a late-starting taskkill /pid would walk that tree instead. Within those 2 s a taskkill that itself killed the top process can still report success. The old code's exposure window was 5 s, so this is no wider than before.
  • The tree's state uses three words: live (taskkill still working), exited (taskkill reported success, the only proof), unverifiable (taskkill failed or ran out of time). Nothing else is reported as dead.
  • runProcess barrier (run-process.ts):
    • A late taskkill success still verifies the barrier and settles the caller right away.
    • "Child terminated" is reported on the same rule as before: when the top process's pipes close, or earlier if taskkill proves the tree dead. This signal is what returns the git admission slot.
  • The caller-visible worst case is unchanged: timeout + 5 s wait + the existing 10 s unverified grace. Only the background cleanup runs longer.
  • macOS and Linux are untouched: the tracker is only reached from the win32 branch, and the new lookups return nothing on other platforms.

Git commands after a stopped git command (before / after)

The git "admission" slot is one of Orca's 2–4 shared git concurrency slots. It is not a lock on the repository. The per-repo operation lock is held only by the caller's own wait, which is unchanged.

before after
Next git command in the same repo waits only for the stopped command's caller (timeout + at most 15 s) same
Shared git slot, taskkill fast (idle machine) returned when the top process exits (~50 ms) same
Shared slot, taskkill slow, grandchild holds the pipes (the measured case) never returned: the grandchild survives and keeps the pipes open returned when taskkill finishes (7.7–9.0 s measured)
Shared slot, taskkill slow, nothing holds the pipes returned at ~5 s (top process killed early, children orphaned) returned when taskkill finishes, at most 30 s

What a longer hold would protect, and why there isn't one. A git process killed with taskkill /f cannot clean up after itself. Any index.lock it held stays behind whether or not Orca waits for it to die. Refs are written by lock-and-rename, so a killed git leaves a stale .lock, never a half-written ref. Holding later git commands until the tree is confirmed dead would therefore prevent no failure. An earlier revision of this PR kept the slot past the top process's exit (at most 2 s); that is removed.

A narrower "retry when the next git hits index.lock while a kill is in flight" was considered and not added. The lock a force-killed git leaves is stale, so a brief wait or retry would not clear it. Only one git path uses this barrier today: pull-with-rebase from the base branch, on its timeout or cancel.

Why

Measured on GitHub windows-2022 runners loaded with 2× CPU burners (see closed draft #28573): bare taskkill /t /f took 4.6–18 s (about 50 ms idle). With a 2 s cap the grandchild survived 8/8 runs; with a 15 s cap it was killed 8/8. #28577 raised the cap to 5 s, which is still inside the measured range, so a loaded machine still leaks children. Any fixed cap is a guess at the tail.

Alternatives considered:

  • Raise the cap again (15–30 s). Every barrier caller would wait that long under load, and the bound would still be a guess.
  • Keep the cap but kill the root at it (today). That is the bug: it breaks the tree walk.
  • Owned Windows Job Objects remove the tree walk entirely and are the long-term answer, but they need native work at every spawn site. This change is the bounded fix that works with today's spawn model.

Linked Issue

N/A (maintainer PR). Follows the design suggested in the review of closed draft #28573; replaces the cap-only mitigation from #28577.

Visual Proof

N/A: process termination behavior, no UI.

Testing

  • Unit tests for the tracker state machine (windows-tree-kill.test.ts): success; late success after the caller's wait; joining an in-flight kill; no respawn after a proven kill; failure, spawn error, and launch throw become unverifiable with a root kill and an allowed retry; the 30 s deadline; reused-pid isolation.
  • runProcess barrier tests on a simulated Windows host (run-process-windows-tree-kill.test.ts): the root is not killed while taskkill is in flight; a late success settles the caller; "child terminated" is reported on root close as before, while a late taskkill success still settles the caller; after the 30 s abandon the root is killed.
  • process-tree-termination.test.ts updated for the wait-versus-deadline split.
  • Real Windows test (windows-tree-kill-late-taskkill.win32.test.ts, added to package (windows)): a .cmd starts a hanging node grandchild; taskkill is started 8 s late (longer than the caller's 5 s wait); the test proves the grandchild is dead. A second CI step runs the same file with CPU burners (2× cores) and also runs the unmodified taskkill under that load.
  • Windows runs (windows-2022), from a temporary diagnostic branch that runs only these tests (the PR's own package (windows) job was blocked by unrelated failures on main at the time). 6 job runs, all green; each runs the delayed test once idle and three times under 2× CPU burners, plus the real taskkill under burners three times. 42 out of 42 runs killed the grandchild.
    • The real taskkill under load took 7.7–9.0 s in all 18 loaded runs, which is past the old 5 s cap. Before this change, the cap would have cut each of those walks short.
    • In the run that logged exit codes, taskkill exited with code 255, not 0, in all 3 loaded runs (a process in the tree exited on its own mid-walk). The tree is reported unverifiable, not exited. The grandchild is still dead, and the caller settles at the existing timeout + 15 s bound.
  • This PR's own package (windows) job: 1 run plus 3 reruns, all green. The real taskkill under load took 7.8–8.9 s, with exit code 255 each time; the grandchild was killed every time.
  • Local vitest (macOS) on process-host, git command-runner, OpenCode, ripgrep, and preflight suites passes.
  • Automated tests added/updated

AI Disclosure

Review

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

  • SSH / remote: no wire change. Only the executing host's Windows termination policy changes.
  • WSL: WSL git uses its own process-group barrier and does not go through taskkill; unchanged.
  • Other Windows callers of signalProcessTree (hooks, notebook kernel, WSL ripgrep stop, OpenCode preflight): they now share one taskkill per child instead of starting a second one on force, and the top process is killed only after taskkill gives up instead of at 5 s.
    • OpenCode's model preflight on a loaded machine: its stop can take up to about 11.5 s instead of about 6.5 s, and more often ends as "not proven stopped" (so it returns no context). That is the honest answer when the tree isn't confirmed dead.
  • Known limits:
    • A taskkill that is still running, with the top process still alive, keeps that process's shared git slot until it finishes (at most 30 s). That is the same rule as before: the slot is returned when the top process's pipes close.
    • A taskkill exit code other than 0 (including 255 from a benign race) is never treated as proof. On a loaded machine, those callers wait the existing 10 s unverified grace.
    • Custom barriers (WSL) don't use taskkill, so they don't get the late-success hook.
  • Not touched: the daemon/PTY and git-runner (killSpawnedCommandTree) termination paths have their own taskkill code.
  • EDR: no new interpreters, flags, or encoded commands; still one taskkill.exe per tree, now at most one at a time.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

@coderabbitai

coderabbitai Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

📝 Walkthrough

The change adds a Windows tree-kill module that tracks taskkill operations by child-process handle. Callers can stop waiting while an active taskkill continues, and later calls can join that operation. Process termination barriers now observe late results and defer root killing and termination reporting while a tree kill remains active. Unit tests and Windows integration tests cover these behaviors, including an optional CPU-load test.

Priority: ➖ Normal

Merge Risk: 🟡 Moderate · up to ce0ae

On Windows, a slow tree kill can leave a grandchild running after the root exits. Resolve the post-root cutoff before merging.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 46.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 15 functions across 10 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly and concisely describes the main change: allowing slow Windows taskkill operations to finish instead of orphaning child processes.
Description check Passed The description is comprehensive and follows the required template. It explains the problem, behavior change, rationale, testing, platform impact, limitations, and checklist status. The maintainer cor…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Description check

Explanation

The description is comprehensive and follows the required template. It explains the problem, behavior change, rationale, testing, platform impact, limitations, and checklist status. The maintainer correctly marks the linked issue as not applicable, and the unmarked full command checklist is acceptable because CI coverage is allowed by the template.

✨ Finishing Touches: 💡 1
📝 Generate docstrings: 💡
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@nwparker
nwparker force-pushed the fix/windows-taskkill-background-completion branch from 72901ef to ff799ba Compare October 16, 2026 07:05

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to GitHub limitations.

⚠️ Outside diff range comments (1)

🟠 Major · Do not abandon an active tree kill when the root exits. · windows-tree-kill.ts:90-113

src/packages/process-host/src/windows-tree-kill.ts:90-113
🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift

Do not abandon an active tree kill when the root exits.

When the root emits exit, the 2-second timer calls killer.kill() and finish('unverifiable'). finish then calls killRoot(), which can only target the already-exited root. If taskkill /t /f is still terminating descendants, this can leave the grandchild alive. The PID-reuse guard does not prove that an active tree walk has finished.

Track whether taskkill has reached the tree, and keep an active tree walk alive until it completes. Apply the PID-reuse abandonment only to a taskkill that has not started traversal.


ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 4090098d-aaac-4d4d-8865-8e75bbef5d7b
📥 Commits

Reviewing files that changed from the base of the PR and between 402a51e and ce0ae3c.

📒 Files selected for processing: 5
  • src/packages/process-host/src/process-tree-termination.test.ts
  • src/packages/process-host/src/run-process-windows-tree-kill.test.ts
  • src/packages/process-host/src/run-process.ts
  • src/packages/process-host/src/windows-tree-kill.ts
  • src/shared/__tests__/process-host/windows-tree-kill-late-taskkill.win32.test.ts
🚧 Files skipped from review as they are similar to previous changes: 1
  • src/shared/tests/process-host/windows-tree-kill-late-taskkill.win32.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

@nwparker
nwparker merged commit d6bc6cb into main Oct 21, 2026
91 of 93 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant