Skip to content

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

Merged
OrcaWin merged 2 commits into
mainfrom
OrcaWin/fix-worktree-delete-live
Jul 24, 2026
Merged

OrcaWin merged 2 commits into
mainfrom
OrcaWin/fix-worktree-delete-live

Conversation

@OrcaWin

@OrcaWin OrcaWin commented Jul 24, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

On Windows, deleting a worktree that still has a live process running in a terminal tab (a pnpm i, or any command that spawns a child tree) fails after the full ~10s teardown deadline with:

Failed to physically stop every PTY for worktree: …

Root cause

Closing a plain shell's ConPTY on Windows does not reap its orphaned children — node-pty's useConptyDll skips the console-process reap. So a live pnpm i/node child:

  1. survives the shell root's exit,
  2. keeps the ConPTY console non-empty (the daemon therefore still reports the PTY session alive), and
  3. holds the worktree cwd handle.

The destructive-removal physical-stop verification (killAllProcessesForWorktree with requirePhysicalStop) then can't prove the PTY exited and fails closed.

#10100 added a taskkill /T /F descendant tree-kill (killWithDescendantSweep) for agent sessions, but plain shells were never swept — so a terminal tab running an install still blocked deletion. This is a long-standing gap, not a regression in the current RC.

Fix

Extend the Windows taskkill /T /F descendant tree-kill to non-agent shells on the immediate/destructive teardown path, in both surfaces:

  • TerminalSessionTeardown.forceKillPlainShellSession (daemon — default on Windows)
  • LocalPtyProvider.shutdownTrackedPty (non-daemon / fallback config)

Gated to win32 + immediate, reusing killWithDescendantSweep so it inherits the same ownsRoot guard (never signals a naturally-exited / recycled PID) and the bounded, windowsHide taskkill. POSIX shells already reach their child pgroup on forceKill and are left unchanged.

Reproduced & verified (Electron dev build)

  • Before: worktree with a live node child → force-delete fails after 10.0s with Failed to physically stop every PTY; the child survives as an orphan holding the cwd.
  • After: the child tree is taskkilled, the directory is removed, and the delete succeeds in ~1.8s; no orphaned process remains.

Tests

  • terminal-session-teardown.test.ts (new): win32 immediate → sweep before force-kill; ownsRoot guard (dead / reassigned session → false); non-win32 immediate → no sweep; graceful → plain kill path.
  • local-pty-provider.test.ts: win32 immediate → tree kill; win32 graceful → no tree kill; non-win32 immediate → no tree kill.
  • terminal-host-session-reaping-leak.test.ts / terminal-host.test.ts: pinned to a POSIX platform for deterministic plain-shell teardown assertions across host OSes.

Typecheck + lint clean; affected suites green.

Behavior note

Because worktree sleep also stops PTYs via immediate: true (keepHistory preserves scrollback, not the process), Windows sleep now tree-kills descendants too. Previously a pnpm dev orphaned by a slept worktree kept running with no attached terminal while pinning the worktree cwd — that orphan is the reported bug. POSIX already killed the child pgroup here, so this aligns Windows with existing POSIX behavior rather than introducing a new policy. Sleep is user-initiated, so a Windows user who sleeps a worktree with a dev server running will now see it stop.

Review follow-up

Fixed an attach race introduced by the first commit: forceKillAndWaitForExit claims termination synchronously, so awaiting the sweep ahead of it left createOrAttach's doomed-session guard (terminal-host-session-create.ts:38) open for the taskkill's duration — a concurrent attach could bind a pane to a session about to be tree-killed. Plain shells never register in the teardown operations map, so isTerminating is the only fence. Now claims session.beginTermination() before the sweep, mirroring the agent path; covered by a regression test that fails without the claim.

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

coderabbitai Bot commented Jul 24, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Plain shell immediate teardown now performs a Windows descendant-process sweep with session ownership checks before force-killing. Local PTY shutdown applies equivalent handling for non-agent PTYs. New tests cover Windows, non-Windows, immediate, graceful, and ownership-change scenarios. Existing TerminalHost tests stabilize platform behavior and relocate overlapping force-kill coverage.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
Title check ✅ Passed The title clearly summarizes the main change: Windows immediate teardown now kills plain-shell PTY process trees.
Description check ✅ Passed The description covers the problem, fix, verification, tests, and follow-up, but it omits the template's explicit screenshots, AI review report, and security audit sections.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

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.

🧹 Nitpick comments (1)
src/main/daemon/terminal-session-teardown.ts (1)

47-55: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Shorten the new explanatory comments.

Keep only a one-line non-obvious rationale; the current comments narrate implementation details.

  • src/main/daemon/terminal-session-teardown.ts#L47-L55: reduce the block to the ConPTY-specific rationale.
  • src/main/daemon/terminal-session-teardown.test.ts#L41-L42: remove or condense the test setup narration.
  • src/main/providers/local-pty-provider.ts#L1132-L1135: reduce to the Windows orphan-process rationale.
  • src/main/providers/local-pty-provider.test.ts#L1520-L1521: remove or condense the behavior already expressed by the test name.
  • src/main/daemon/terminal-host-session-reaping-leak.test.ts#L61-L62: condense the platform-pinning rationale.
  • src/main/daemon/terminal-host-session-reaping-leak.test.ts#L134-L135: condense the POSIX teardown narration.
  • src/main/daemon/terminal-host.test.ts#L72-L73: condense the platform-pinning rationale.

As per coding guidelines, “Comments must be concise, non-obvious, and preferably one line; do not explain obvious code or narrate how it works.”

Source: Coding guidelines


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: ae8f74e4-55e4-4e35-81eb-c04329377e38

📥 Commits

Reviewing files that changed from the base of the PR and between 981653f and 21d9896.

📒 Files selected for processing (6)
  • src/main/daemon/terminal-host-session-reaping-leak.test.ts
  • src/main/daemon/terminal-host.test.ts
  • src/main/daemon/terminal-session-teardown.test.ts
  • src/main/daemon/terminal-session-teardown.ts
  • src/main/providers/local-pty-provider.test.ts
  • src/main/providers/local-pty-provider.ts

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.
@OrcaWin
OrcaWin merged commit 402b5a1 into main Jul 24, 2026
8 checks passed
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.

1 participant