Skip to content

fix(win): taskkill agent PTY descendant trees on stop - #10100

Merged
nwparker merged 2 commits into
stablyai:mainfrom
innocarpe:fix/windows-pty-agent-descendant-tree-kill
Jul 24, 2026
Merged

nwparker merged 2 commits into
stablyai:mainfrom
innocarpe:fix/windows-pty-agent-descendant-tree-kill

Conversation

@innocarpe

@innocarpe innocarpe commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor

fix(win): taskkill agent PTY descendant trees on stop

Summary

On Windows, stopping an agent terminal now kills the whole ConPTY process tree, so orphaned agent/MCP children no longer hold the worktree open and block removal.

ELI5

When you closed an agent terminal on Windows, Orca only stopped the shell — the agent and its helper processes kept running and locked the folder, so deleting the worktree failed. Now Orca tells Windows to kill that entire process family.

Root cause & fix

Agent teardown used a POSIX ps descendant snapshot that is a no-op on Windows, so only the shell root was killed while Claude/Codex and their MCP children reparented, kept the worktree cwd handle, and made worktrees:remove fail with "Failed to physically stop every PTY" (#10004). killWithDescendantSweep() now branches on platform: Windows runs taskkill /pid <root> /T /F (5s timeout, windowsHide) to walk the ConPTY tree before killRoot, while POSIX keeps the existing snapshot + signal path. Any taskkill failure or timeout is swallowed so killRoot always runs.

Evidence

Validated & rebased onto latest main during a bug-bash triage on 2026-07-23.

  • Clean rebase onto origin/main (base 8a23618310, head 84dbdab7ad).
  • Tests: pty-descendant-termination.test.ts, windows-process-tree-kill.test.ts, local-pty-provider.test.ts.
  • On branch: 119 passed (119), 3 files.
  • Fix reverted (tests kept): 4 tests failed + 5 errors, 112 passed — the new Windows-sweep tests fail, windows-process-tree-kill import is absent on main, and the provider vi.mock errors because the wiring moved to killWithDescendantSweep. Restored fix → 119 passed again.
(a) on branch:        Test Files 3 passed (3) | Tests 119 passed (119)
(b) fix reverted:     Test Files 3 failed (3) | Tests 4 failed | 112 passed + 5 errors
(c) fix restored:     Test Files 3 passed (3) | Tests 119 passed (119)
  • Lint clean: oxlint on all three source files reports no warnings/errors.

Trade-offs

None — pure fix. taskkill /T /F is force-kill, but matches the pattern already used for git/codex/relay cancellation on Windows and ConPTY stop was already force-oriented.

Regression risk

Zero on macOS/Linux: the POSIX branch keeps the identical snapshot + signal + killRoot sequence. The new behavior is Windows-only, gated on agent-session membership, ownsRoot, and valid-pid checks, and taskkill is invoked only on win32. Every taskkill error is caught so root kill always proceeds. Proven by the passing branch tests that fail when the fix is reverted.


Original fix by @innocarpe. Validated, rebased, and (where applicable) hardened via automated bug-bash review; original fix design preserved.

@coderabbitai

coderabbitai Bot commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Adds a Windows process-tree termination helper using taskkill /T /F, including PID validation and error handling. Extends descendant sweeping with a Windows-specific path that conditionally runs the tree killer and always signals the root. Updates tracked agent PTY shutdown to use the sweep utility with ownership checks. Reworks related provider tests around sweep completion and adds Windows behavior coverage.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description covers the summary, fix, and evidence, but it omits required template sections like Screenshots, AI Review Report, Security Audit, and Notes. Add the missing template sections, especially Screenshots, the Testing checklist, AI Review Report, Security Audit, and Notes.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The PR addresses #10004 by killing the full Windows agent PTY tree via taskkill for daemon and local shutdown paths, with tests covering the failure cases.
Out of Scope Changes check ✅ Passed All code changes stay focused on Windows descendant termination and supporting tests/helpers, with no obvious unrelated edits.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Title check ✅ Passed The title clearly states the Windows taskkill fix for agent PTY descendant trees and matches the main code change.

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.

@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.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 9855eab8-811b-4ea7-a6ef-75ded1886c6e

📥 Commits

Reviewing files that changed from the base of the PR and between 8685cdb and 60ca616ac60ef3e97d1097a9c7650088c43abbcc.

📒 Files selected for processing (6)
  • src/main/providers/local-pty-provider.test.ts
  • src/main/providers/local-pty-provider.ts
  • src/main/pty-descendant-termination.test.ts
  • src/main/pty-descendant-termination.ts
  • src/main/windows-process-tree-kill.test.ts
  • src/main/windows-process-tree-kill.ts

Comment thread src/main/windows-process-tree-kill.ts Resolved
@innocarpe

Copy link
Copy Markdown
Contributor Author

Sync update (84dbdab7a)

Address CodeRabbit: taskkill timeout (5s) + windowsHide so killRoot cannot hang on a wedged tree-kill.

Windows agent teardown previously degraded to shell-only kill because
descendant snapshot is POSIX-only. Orphaned claude/codex/MCP children
kept worktree cwd handles open and blocked worktree remove.

Use taskkill /T /F on the PTY root in killWithDescendantSweep (daemon +
local agent shutdown) so the ConPTY tree is cleared before teardown.

Fixes stablyai#10004
Prevent a wedged taskkill from delaying killRoot, and hide the console
flash during agent PTY tree teardown (CodeRabbit on stablyai#10100).
@nwparker
nwparker force-pushed the fix/windows-pty-agent-descendant-tree-kill branch from 84dbdab to 7373001 Compare July 23, 2026 11:49
@nwparker
nwparker merged commit dc18ba9 into stablyai:main Jul 24, 2026
@nwparker

Copy link
Copy Markdown
Contributor

Thanks so much for this, @innocarpe — taskkill agent PTY descendant trees on stop. Verified (fail-on-main/pass-on-branch), reviewed, and merged. Really appreciate it! 🙏

OrcaWin added a commit that referenced this pull request Jul 24, 2026
* fix(win): taskkill plain-shell PTY trees on immediate teardown

Deleting a Windows worktree that still has a live process in a terminal
tab (a `pnpm i`, or any command that spawns a child tree) failed with
"Failed to physically stop every PTY for worktree" after the full 10s
teardown deadline.

Root cause: on Windows, closing a plain shell's ConPTY does not reap its
orphaned children — node-pty's `useConptyDll` skips the console-process
reap. A live `pnpm i`/`node` child survives the shell's exit, keeps the
ConPTY console non-empty (so the daemon still reports the session alive),
and holds the worktree cwd handle. The destructive-removal physical-stop
check then fails closed. #10100 fixed this for agent sessions
(killWithDescendantSweep -> taskkill /T /F) but plain shells were never
swept.

Fix: extend the Windows taskkill /T /F descendant tree-kill to non-agent
shells on the immediate/destructive teardown path, in both the daemon
(TerminalSessionTeardown) and local provider. Gated to win32 + immediate
with the same ownsRoot guard so a naturally-exited/recycled PID is never
signalled; POSIX shells keep reaching their child pgroup via forceKill
and are unchanged.

Reproduced and verified end to end via the Electron dev build: a worktree
with a live node child previously failed to delete after 10s; with the
fix the child tree is taskkilled, the directory is removed, and the
delete succeeds in ~1.8s.

* fix(win): claim plain-shell termination before the taskkill sweep

forceKillAndWaitForExit sets _isTerminating in its synchronous prologue.
Awaiting the Windows descendant sweep ahead of it left createOrAttach's
doomed-session guard open for the taskkill's duration, so a concurrent
attach could bind a pane to a session about to be tree-killed.
innocarpe added a commit to innocarpe/orca that referenced this pull request Jul 27, 2026
On Windows, ConPTY shell-only kill left npm/dev-server children listening,
so switching projects could still serve the previous app on the same port.

- taskkill /T /F via killWithDescendantSweep for every local PTY stop on Windows
- Ports UI kill path also tree-kills on Windows
- Keep POSIX agent-only descendant sweep; plain POSIX terminals unchanged

Closes stablyai#10150
Related: overlaps Windows agent path of stablyai#10004 / stablyai#10100
innocarpe added a commit to innocarpe/orca that referenced this pull request Aug 6, 2026
On Windows, ConPTY shell-only kill left npm/dev-server children listening,
so switching projects could still serve the previous app on the same port.

- taskkill /T /F via killWithDescendantSweep for every local PTY stop on Windows
- Ports UI kill path also tree-kills on Windows
- Keep POSIX agent-only descendant sweep; plain POSIX terminals unchanged

Closes stablyai#10150
Related: overlaps Windows agent path of stablyai#10004 / stablyai#10100
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
* fix(win): taskkill agent PTY descendant trees on stop

Windows agent teardown previously degraded to shell-only kill because
descendant snapshot is POSIX-only. Orphaned claude/codex/MCP children
kept worktree cwd handles open and blocked worktree remove.

Use taskkill /T /F on the PTY root in killWithDescendantSweep (daemon +
local agent shutdown) so the ConPTY tree is cleared before teardown.

Fixes stablyai#10004

* fix(win): bound taskkill with timeout and windowsHide

Prevent a wedged taskkill from delaying killRoot, and hide the console
flash during agent PTY tree teardown (CodeRabbit on stablyai#10100).
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…yai#10339)

* fix(win): taskkill plain-shell PTY trees on immediate teardown

Deleting a Windows worktree that still has a live process in a terminal
tab (a `pnpm i`, or any command that spawns a child tree) failed with
"Failed to physically stop every PTY for worktree" after the full 10s
teardown deadline.

Root cause: on Windows, closing a plain shell's ConPTY does not reap its
orphaned children — node-pty's `useConptyDll` skips the console-process
reap. A live `pnpm i`/`node` child survives the shell's exit, keeps the
ConPTY console non-empty (so the daemon still reports the session alive),
and holds the worktree cwd handle. The destructive-removal physical-stop
check then fails closed. stablyai#10100 fixed this for agent sessions
(killWithDescendantSweep -> taskkill /T /F) but plain shells were never
swept.

Fix: extend the Windows taskkill /T /F descendant tree-kill to non-agent
shells on the immediate/destructive teardown path, in both the daemon
(TerminalSessionTeardown) and local provider. Gated to win32 + immediate
with the same ownsRoot guard so a naturally-exited/recycled PID is never
signalled; POSIX shells keep reaching their child pgroup via forceKill
and are unchanged.

Reproduced and verified end to end via the Electron dev build: a worktree
with a live node child previously failed to delete after 10s; with the
fix the child tree is taskkilled, the directory is removed, and the
delete succeeds in ~1.8s.

* fix(win): claim plain-shell termination before the taskkill sweep

forceKillAndWaitForExit sets _isTerminating in its synchronous prologue.
Awaiting the Windows descendant sweep ahead of it left createOrAttach's
doomed-session guard open for the taskkill's duration, so a concurrent
attach could bind a pane to a session about to be tree-killed.
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.

2 participants