Skip to content

[Bug]: Terminal pty leaks — stale daemons survive upgrade, ptys survive tab close, orphaned flag unreliable #11342

Open

Description

@Cyb3rN8

Short summary

Orca leaks terminal pty sessions in three distinct ways. On this machine a single cleanup pass reclaimed 4 stale daemons, 64 descendant processes, and 33 orphaned pty sessions. The daemon-side orphaned flag only catches one of the three classes, so neither the UI nor orca terminal list reveals the bulk of the leak.


Class 1 — stale daemons survive app upgrade

After upgrades, previous-generation daemon processes are never reaped. Four generations were alive simultaneously:

daemon started descendant processes
daemon-v23 (pid 978) Jul 21 0
daemon-v25 (pid 50007) Jul 23 44
daemon-v26 (pid 80383) Jul 25 18
daemon-v28 (pid 79714) Jul 26 317 (current, in use)

v25 and v26 each still held login -flpq → zsh → claude chains (7 agent sessions total, burning CPU and RAM). These are invisible to the CLI, since orca terminal list only talks to the current runtime (v28). A SIGTERM to the four stale daemons cleanly reclaimed all 64 descendants, and the running Orca was unaffected — confirming nothing needed them.

The daemon/ directory corroborates the leak: daemon-v23.pid and daemon-v23.token were left behind with no matching .sock.


Class 2 — global floating terminal: tab destroyed, pty survives, orphaned stays false

After closing the global floating terminal, 28 global-floating-terminal@@* ptys remained registered across 24 tabIds. Every single one reported:

"orphaned": false,
"connected": true,
"writable": true

But orca terminal close --terminal <handle> --tab on them returns:

{"ok": false, "error": {"code": "runtime_error", "message": "tab_not_found"}}

The tab is genuinely gone from the UI, so orphaned: false is simply wrong here. orca terminal close without --tab reclaims them and reports "ptyKilled": true, confirming the pty was live and unreferenced. 27 of the 28 were reclaimed this way (the 28th was the session issuing the commands).


Class 3 — main window: pty with no pane binding

Six further ptys carried synthesized identifiers, where tabId == leafId == "pty:" + ptyId:

"tabId":  "pty:<worktreeId>::<worktreePath>@@b4bf46ff",
"leafId": "pty:<worktreeId>::<worktreePath>@@b4bf46ff",
"ptyId":  "<worktreeId>::<worktreePath>@@b4bf46ff",
"orphaned": true,
"title": null,
"lastOutputAt": null

This class is correctly flagged orphaned: true, and the synthesized pty: prefix makes it detectable read-only. But nothing reclaims them automatically and they surface nowhere in the UI. These appear to be registry residue rather than live processes — killing all six changed the descendant process count not at all.


Why this is painful to clean up from the CLI

  • No selector reaps by orphan state. terminal stop --worktree id:global-floating-terminal would take the caller's own session down along with the leaked ones.
  • Every read-only field is identical between healthy and leaked class-2 sessions: orphaned, connected, writable, paneRuntimeId (always 1), rendererGraphEpoch (always 0). The only discriminator is a destructive call — close --tab returning tab_not_found — which closes the tab when one does exist, so it cannot serve as a probe. terminal switch appears usable as a non-destructive stand-in, but it perturbs UI focus.
  • terminal list handles expire almost immediately. In a loop issuing only terminal show, handles went stale by the 5th call (terminal_handle_stale). Every mutation must re-list to re-resolve. tabId is stable; handle is not. This makes any batch cleanup script fragile.
  • orca-data.json on disk is not written live. workspaceSession.tabsByWorktree, unifiedTabs, and terminalLayoutsByTabId looked like the right reconciliation source, but the file was 7 days stale and did not contain the current session's tabId at all.

Reproduction

  1. Use Orca across at least one app upgrade, so a new daemon-vN generation starts.
  2. ps -ef | grep daemon-entry.js — previous-generation daemons are still running, with live login/zsh/agent descendants.
  3. Open several global floating terminal sessions, then close the global terminal.
  4. orca terminal list --json — the global-floating-terminal@@* ptys are still listed, as orphaned: false, connected: true.
  5. orca terminal close --terminal <handle> --tab → tab_not_found, proving the tab is gone while the pty remains registered and alive.

Suggested fixes

  1. On daemon startup, signal previous-generation daemons to drain and exit — the daemon-vN.pid records needed to find them are already on disk.
  2. Reclaim ptys when their owning tab is destroyed, particularly on global-floating-terminal teardown.
  3. Fix orphaned for class 2, or expose a read-only "has live tab binding" field, so cleanup does not require a destructive probe.
  4. Provide a non-destructive reap path, e.g. orca terminal list --orphaned plus orca terminal reap.
  5. Consider lengthening terminal handle lifetime, or accepting tabId directly on mutating commands.

Environment

  • Orca 1.4.159
  • macOS 26.5.2, arm64

(Paths in the excerpts above are redacted placeholders; identifiers are otherwise verbatim.)

Activity

  1. Cyb3rN8 commented on Jul 29, 2026

    @Cyb3rN8
    Author

    Follow-up: the runtime already has everything needed to detect this read-only — it's just not reachable from the CLI.

    The daemon RPC surface (JSON-RPC over the unix socket in orca-runtime.json) exposes terminal.resolvePane, which the renderer itself uses to confirm a pty is still bound to a pane:

    terminal.resolvePane({ paneKey: `${tabId}:${leafId}`, worktreeId })
    
    • pane still bound → ok, returns the terminal (the renderer then verifies handle/tabId/leafId/worktreeId all match)
    • pane gone → {"code": "runtime_error", "message": "terminal_not_found"}

    That cleanly separates the class-2 leaks from healthy sessions without touching UI state, which none of the fields returned by terminal list can do. terminal close --tab can also detect it, but destroys a live tab when one exists, so it can't be used as a probe — and terminal switch works but yanks UI focus on every check, which makes sweeping a few dozen tabs unusable in practice.

    So suggested fixes #3/#4 above are probably cheap: surfacing resolvePane (or just an --orphaned filter on terminal list computed from it) would be enough for users to clean up without a destructive probe.

    Also worth noting, both already present in the bundle:

    • terminal.adoptOrphans — there's apparently already an orphan-adoption path
    • a [terminal-retirement] code path that closes handles in bulk by tabId, and warns skipped unroutable runtime handles

    Whatever leaves these ptys behind seems to be a gap in those existing mechanisms rather than a missing one.

    One more CLI-level papercut for anyone scripting a cleanup: handles from terminal list go stale within a handful of calls (terminal_handle_stale), so every mutation needs a fresh list to re-resolve. tabId and ptyId are stable; handle is not.

  2. Regis-RCR commented on Aug 24, 2026

    @Regis-RCR
    Contributor

    A counter-example to "confirming nothing needed them", from three macOS hosts on 1.4.187 and 1.4.188.

    The class-1 report here ends with a SIGTERM to four stale daemons cleanly reclaiming 64 descendants, with the running Orca unaffected. On our machines that same reap would not have been safe, and we only found that out because we inventoried before running it.

    Two generations stamped --app-version=1.4.185 were alive alongside newer ones. Under them, at the moment of collection:

    Host B / daemon-v34
      ttys093  caffeinate -i -t 300           age 00:39
      ttys077  memtrace MCP server
      ttys008  bun server.ts (telegram MCP)
    
    Host C / daemon-v34
      ttys002  cswap watch
      ttys003  cswap tui
      ttys005  bun .../mcp-server.cjs
    

    A process 39 seconds old means something was driving that terminal at that moment. The remediation we had already approved would have terminated five generations holding 34 terminals, including those. We cancelled it.

    The limit of the claim, since it matters more than the headline: we did not establish that all 28 inventoried terminals were active. A live MCP server process is not proof of a connected client, and a recent caffeinate proves recent creation rather than continuous activity. Strictly, the 39 seconds is the process's age, not the terminal's. The narrow claim is enough for the decision anyway: at least some carried current work, so a blanket reap is unsafe.

    So the unreliable orphaned flag cuts in both directions, and this thread only documents one of them. It under-reports the leak, which is your finding. It also means absence from the current runtime graph is not evidence that a survivor is disposable, which is exactly the safety requirement #9229 asks for. This is a concrete instance of the second.

    One asymmetry worth adding here, because suggested fix 1 above assumes the records are on disk. On our 28-day host, daemon-v30 and daemon-v31 are running with no .pid, .sock or .token file at all, while the other two hosts carry six .sock files with no process behind them. A discovery pass driven by the record files walks past exactly the generations worth preserving. Both directions are needed: live daemon-entry.js processes whose --socket names another generation, and record files with no process.

    Full data across the three hosts, with the evidential status of each claim, in #16278.

  3. farrrr commented on Nov 3, 2026

    @farrrr

    Related new report: #37843 — on v1.4.247 (Linux, local host), a terminal started with orca agent launch is orphaned: true right after launch (no restart). terminal close --tab then returns terminal_handle_stale and leaves the PTY running; plain close stops it but still returns tab_not_found. Repro steps and code pointers are in that issue.

  4. jk1420 commented on Nov 20, 2026

    @jk1420

    Windows-specific confirmation on Orca 1.4.265 (Desktop only).

    After an upgrade chain from 1.4.245/1.4.248/1.4.257/1.4.262 to 1.4.265, previous-generation terminal daemons remained alive after the parent Orca application had exited. The new protocol-v60 daemon started successfully, but older daemons still accepted client handshakes shortly afterward. Repeating updates leaves multiple versioned daemon generations resident.

    Observed

    • Older daemon processes remained alongside the current daemon after the update and continued to own terminal/session state.
    • The corresponding versioned daemon-v*.pid and daemon-v*.token records remained after the old processes were no longer needed.
    • The old daemon records reported cgroupUnit: null and processCeiling.state: off with reason disabled, so there was no OS-level lifetime binding or process ceiling to reclaim them when the parent app exited.
    • Manually stopping the old daemons removed the redundant processes without affecting the current 1.4.265 daemon; the Orca runtime then reported ready and reachable.

    All paths, usernames, PIDs, socket names, tokens, worktree names, and other machine-specific identifiers are intentionally omitted.

    Reproduction

    1. Run Orca on Windows with one or more terminal sessions.
    2. Allow Orca to update across versions while a terminal/session is still present.
    3. Relaunch the updated Orca application.
    4. Inspect the terminal-host processes and versioned daemon records.
    5. Close or migrate the old terminal sessions and repeat another update.

    Previous-generation daemons remain resident instead of draining and exiting, so the number of daemon generations grows over time.

    Expected

    After the new daemon has migrated or reattached all live terminals, an older daemon with no active terminal ownership should drain and exit. Its PID/token metadata should then be removed. If an older daemon is still required, Orca should expose that ownership clearly and perform the cleanup when the final terminal exits.

    Impact

    This creates redundant long-lived processes and stale metadata, and may contribute to session/profile state conflicts during update and reattach. It also makes manual cleanup necessary after upgrades.

    Related: #37909 (older daemons and duplicate/resumed sessions), #41931 (old daemon surviving auto-update).

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