Skip to content

[Bug]: Worktree deletion fails with "Failed to physically stop every PTY" — orphaned claude.exe/node.exe/cmd.exe processes on Windows #9045

Closed

Description

@LeSoviet

Operating system

Windows

Orca version

1.4.143.0

Details

Environment

  • OS: Windows 10
  • Agent CLIs used in worktrees: Claude Code, Codex, Kilo Code

Bug
Deleting a worktree fails consistently with:

Error invoking remote method 'worktrees:remove': Error: Failed to physically stop every PTY for worktree: ::

or, on the "Force Delete" path:

Error invoking remote method 'worktrees:remove': Error: Timed out waiting for physical PTY teardown: ::

This has now happened on two separate worktrees ("snaggletooth" and "anhinga"), so it appears to be reproducible rather than a one-off.

Root cause (as far as I can tell)
Using Sysinternals handle.exe on the worktree folder shows dozens of orphaned processes still holding open file handles into the worktree directory — claude.exe, node.exe, and cmd.exe, well beyond what should be running for a single session (20+ handles across repeated cmd.exe/node.exe instances). It looks like Orca is not properly terminating child processes (PTYs spawned for agent CLIs) when a worktree/session is closed, so they linger and block folder deletion.

Steps to reproduce

  1. Run an agent (Claude Code / Codex / Kilo Code) inside an Orca worktree.
  2. Close the session / try to delete the worktree from the UI.
  3. Deletion fails with the PTY teardown error above.
  4. Inspecting the worktree folder with handle.exe shows many orphaned claude.exe/node.exe/cmd.exe processes still holding file handles.

Current workaround
Manually force-kill the relevant processes, then delete the folder:

Stop-Process -Name claude -Force -ErrorAction SilentlyContinue
Stop-Process -Name node -Force -ErrorAction SilentlyContinue
Stop-Process -Name cmd -Force -ErrorAction SilentlyContinue
Remove-Item -Recurse -Force ""

A full OS restart also works but obviously isn't a practical daily workflow.

Expected behavior
When a worktree is closed or deleted, Orca should reliably terminate all child processes/PTYs it spawned (including nested node/cmd processes from CLI agents) before attempting folder removal, without requiring manual process killing or a system restart.

Activity

  1. added theissue type on Jul 16, 2026
  2. changed the title [-][Bug] "Failed to physically stop every PTY" — worktree deletion fails, requires manually killing orphaned claude.exe/node.exe/cmd.exe processes[/-] [+][Bug]: Worktree deletion fails with "Failed to physically stop every PTY" — orphaned claude.exe/node.exe/cmd.exe processes on Windows[/+] on Jul 16, 2026
  3. taifunk commented on Jul 17, 2026

    @taifunk

    Confirming on Windows — and I think this is a deliberate win32 no-op, not a general teardown failure.

    Operating system: Windows 10
    Orca version: 1.4.144.0 (daemon protocol v22, carried over from 1.4.143)

    What I see
    15 claude.exe on a 16 GB box; 10 orphaned — parent shell dead, no pane, unreachable. Ages 12–29h, ~340–440 MB each (~3.9 GB). Commit charge had climbed to 89.5% (23.8/26.6 GB) and the box hard-crashed a session earlier the same day at ~88%; hand-killing the 10 dropped it to 68.1%. They re-accumulate at ~10 per 29h. Not resumed/duplicate sessions — the three --resume processes carried three distinct session IDs.

    Steps to reproduce

    1. On Windows, run agent CLIs (Claude Code) in Orca terminal panes across several worktrees.
    2. Close panes / let sessions end over a day or two (no worktree-remove needed — plain session teardown is enough).
    3. Get-CimInstance Win32_Process -Filter "Name='claude.exe'" — orphans whose ParentProcessId no longer resolves accumulate.

    Root cause (adds to the reporter's "as far as I can tell")
    This looks like the deliberate Windows exclusion in #8706 (fix(terminal): kill agent descendant processes on session teardown), whose own description says:

    "Windows remains a deliberate no-op in the descendant snapshot module; ConPTY keeps its prior foreground-tree behavior."

    The shipped v1.4.144 app.asar matches: the descendant-snapshot path returns null on win32, the POSIX process-group kill falls back, and there's no Job Object or taskkill /T. So teardown kills the foreground tree only; claude.exe is a grandchild of the shell, and Windows doesn't cascade — it survives indefinitely. This is why orphans persist even on builds that already contain #8706: the sweep doesn't run on Windows. It's also broader than the worktrees:remove surface in the title — ordinary pane/session close orphans too.

    Suggested fix: extend #8706's descendant sweep to Windows ConPTY — a per-session Job Object (kill-on-close), or a taskkill /T /F fallback on teardown.

    Workaround for others meanwhile: periodically kill claude.exe whose parent PID no longer resolves, with an age floor (e.g. >2h) so short-lived hook-spawned processes aren't caught. Happy to test a Windows build.

  4. OrcaWin commented on Jul 18, 2026

    @OrcaWin
    Collaborator

    Reproduced and root-caused (Windows). This is a missing process-tree kill in the PTY teardown path, not a Git/filesystem race.

    Where the errors come from. Both messages are thrown by killAllProcessesForWorktree:

    • Failed to physically stop every PTY for worktree: <uuid>::<path> — src/main/runtime/worktree-teardown.ts:137
    • Timed out waiting for physical PTY teardown: <uuid>::<path> (Force Delete) — src/main/runtime/worktree-teardown.ts:60

    Destructive removal (worktrees:remove, src/main/ipc/worktrees.ts:1419) calls it with requirePhysicalStop: true, so any PTY whose physical exit isn't proven before the deadline makes the whole delete reject.

    Why it fails on Windows — no tree kill. Teardown kills a PTY via provider.shutdown, which on Windows reduces to node-pty's proc.kill() (a ConPTY close of the direct shell). The two primitives that would reap the child tree are POSIX-only:

    • captureDescendantSnapshot hard-returns null on win32 (src/main/pty-descendant-termination.ts:202) — its own comment says Windows "degrade[s] to today's shell-only kill." So the claude.exe → pwsh.exe/node.exe/cmd.exe subtree is never enumerated or signalled.
    • forceKillPosixPtyProcessGroups is POSIX-only; on win32 it just does a single-root kill (src/main/pty/posix-pty-process-groups.ts:94).

    Windows needs taskkill /T /F or a kill-on-close Job Object to reap a tree — process.kill/SIGTERM and a ConPTY close do not. The orphaned grandchildren keep handles into the worktree dir (what handle.exe shows), so the shell's onExit never proves physical teardown and the delete errors out.

    Applies to daemon-hosted PTYs too (#9195 / #8275): with the daemon as provider, provider.shutdown crosses into pty-subprocess.ts, which still only does proc.kill() (src/main/daemon/pty-subprocess.ts:1199-1237), and forceKill() early-returns on Windows. Grandchildren living under orca-terminal-daemon are orphaned.

    Notably, the fix pieces already exist in-repo but aren't wired into terminal teardown: taskkill /T /F is used in 7+ other places, and readWindowsConptyProcessIds already enumerates the full ConPTY child PID set — but only for foreground attribution.

    Repro test (3 passing) pinning the gap:

    npx vitest run --config config/vitest.config.ts src/main/runtime/repro-9045-windows-pty-tree-kill-gap.test.ts

    Proves the Windows path never enumerates/kills the child tree while POSIX does. Live read-only Get-CimInstance on this box confirmed the tree shape (powershell → claude → pwsh, powershell → node → node, cmd.exe).

    Fix direction: after the ConPTY close, escalate on Windows to a tree kill of the PTY root (taskkill /pid <shellPid> /T /F, or spawn under a Job Object) in both local-pty-provider.ts and the daemon forceKill (pty-subprocess.ts); verify with readWindowsConptyProcessIds.

    Full trace in docs/bug-reproductions/9045-worktree-delete-pty-teardown.md.

    🔁 Reproduced via automated Windows bug-reproduction pass (read-only process inspection; nothing killed/deleted).

  5. added
    has_reproConclusive reproduction exists (test/script/CLI evidence). See docs/bug-reproductions/
    on Jul 18, 2026
  6. AmethystLiang commented on Jul 19, 2026

    @AmethystLiang
    Contributor

    Duplicate consolidation

    #9044 is the same report (same author, nearly identical body, filed ~3 minutes earlier) and is already closed. This issue (#9045) is the canonical tracker — it has the reproduction and root-cause investigation (missing process-tree kill on Windows PTY teardown).

    No further action needed on #9044.

  7. Tauri-EPO commented on Aug 17, 2026

    @Tauri-EPO
    Contributor

    Filed #14998 for the containment primitive behind this — on Windows the teardown path is cooperative (taskkill /T /F gated on an identity probe that skips unknown/foreign/absent), with no job object anywhere in the tree, so nothing reaps when the daemon dies abnormally. Not a duplicate of this report; this one stays the user-facing tracker for the failed worktree removal.

    Measurements from a Windows 11 box in that issue: agent → MCP chains 6–14 levels deep, ~4 processes per npx-launched MCP server, 28 MCP processes / ~1.4 GB across two Claude sessions and one Codex under a single orca-terminal-daemon.exe.

  8. 2 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinghas_reproConclusive reproduction exists (test/script/CLI evidence). See docs/bug-reproductions/os:Windows

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions