Repository navigation
fix(win): taskkill plain-shell PTY trees on immediate teardown - #10339
Conversation
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.
📝 WalkthroughWalkthroughPlain 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)
✨ Finishing Touches📝 Generate docstrings
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/main/daemon/terminal-session-teardown.ts (1)
47-55: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueShorten 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
📒 Files selected for processing (6)
src/main/daemon/terminal-host-session-reaping-leak.test.tssrc/main/daemon/terminal-host.test.tssrc/main/daemon/terminal-session-teardown.test.tssrc/main/daemon/terminal-session-teardown.tssrc/main/providers/local-pty-provider.test.tssrc/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.
…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.
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:Root cause
Closing a plain shell's ConPTY on Windows does not reap its orphaned children — node-pty's
useConptyDllskips the console-process reap. So a livepnpm i/nodechild:The destructive-removal physical-stop verification (
killAllProcessesForWorktreewithrequirePhysicalStop) then can't prove the PTY exited and fails closed.#10100 added a
taskkill /T /Fdescendant 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 /Fdescendant 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
killWithDescendantSweepso it inherits the sameownsRootguard (never signals a naturally-exited / recycled PID) and the bounded,windowsHidetaskkill. POSIX shells already reach their child pgroup onforceKilland are left unchanged.Reproduced & verified (Electron dev build)
nodechild → force-delete fails after 10.0s withFailed to physically stop every PTY; the child survives as an orphan holding the cwd.Tests
terminal-session-teardown.test.ts(new): win32 immediate → sweep before force-kill;ownsRootguard (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(keepHistorypreserves scrollback, not the process), Windows sleep now tree-kills descendants too. Previously apnpm devorphaned 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:
forceKillAndWaitForExitclaims termination synchronously, so awaiting the sweep ahead of it leftcreateOrAttach'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 teardownoperationsmap, soisTerminatingis the only fence. Now claimssession.beginTermination()before the sweep, mirroring the agent path; covered by a regression test that fails without the claim.