Skip to content

[Bug][Windows] Closed PTYs remain as persistent orphan handles and misattribute descendant memory #10897

Closed

Description

@huxiaodong07

Summary

On Windows, Orca can retain terminal records after their PTYs have already been stopped. They remain reported as orphan terminals indefinitely, including after restarting Orca.

The Resource Manager can then use the dead PTY root PID as the label for live descendant processes. On Windows, a child can retain the exited parent's PID, so this can make real agent processes appear under a PID that no longer exists and makes safe cleanup confusing.

Environment

  • Orca 1.4.158 (packaged Windows build)
  • Windows 11
  • Local worktrees and Codex / Pi agents

Reproduction / observed behavior

  1. Start several agent terminals across worktrees.
  2. Close the terminal or let its PTY exit.
  3. Run orca terminal list --json.

I observed eight entries with all of the following:

  • orphaned: true
  • empty title and preview
  • no active pane
  • orca terminal close --terminal <handle> --json returns ptyKilled: true
  • orca terminal show --terminal <handle> --json reports paneRuntimeId: -1

Despite that, all eight entries remain in orca terminal list --json and Resource Manager continues to report “8 orphan terminals” after an app restart.

Before cleanup, Resource Manager also displayed memory under stale root PIDs. The displayed root PIDs were absent from the Windows process table, but live Codex/Pi descendants still had those exited PIDs in their ParentProcessId chain. This made the UI attribute their memory to a non-existent PTY root.

Expected behavior

  • Once a terminal close reports ptyKilled: true and the handle has paneRuntimeId: -1, the terminal should be removed from the runtime terminal inventory (or clearly separated as an inert historical record that does not contribute to orphan counts).
  • Resource Manager should not recurse from a PTY root PID unless that root PID exists in the current process snapshot.
  • Orphan cleanup should distinguish:
    1. inert handle records with no process,
    2. dead-root sessions with live detached descendants, and
    3. live / ambiguous sessions.

Impact

  • Users see a persistent, misleading orphan-terminal count.
  • Restarting does not clear it.
  • A dead PID can be shown as if it owns real memory, while the actual child processes require separate, careful cleanup.
  • This encourages unsafe manual process killing because the Resource Manager's labels do not map directly to live Windows processes.

Suggested acceptance criteria

  • Close/reap removes or tombstones the terminal record once its runtime is gone.
  • orca terminal list --json excludes inert records by default, or exposes an explicit state/filter for them.
  • Resource accounting requires the PTY root PID to exist before traversing descendants.
  • Regression test: exited parent PID with still-running descendants on Windows must not be shown as an active PTY root.
  • Regression test: ptyKilled: true + paneRuntimeId: -1 does not leave a persistent orphan count after restart.

Related: #9530 and #9840 cover adjacent stale-session / PTY-health problems; this report is specifically about persistent inert handles plus Windows process-tree attribution.

Activity

  1. nwparker commented on Aug 22, 2026

    @nwparker
    Contributor

    Fixed by #15755 (2b1254d68), now on main.

    Closed PTY trees are now owned by a job object rather than reconstructed from a process-table scrape, so teardown reaps descendants that reparented or detached instead of leaving handles behind.

    Verified on Windows 11 with a detached grandchild — invisible to both a parent-pid walk and GetConsoleProcessList — reaped by a single TerminateJobObject.

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions