orca icon indicating copy to clipboard operation
orca copied to clipboard

[Bug]: Stale OMP sessions survive terminal close and keep eating memory (workers + Deno LSP)

Open AmethystLiang opened this issue 4 months ago • 4 comments

Operating system

Unknown (reporter: @beebyboss on X). Likely macOS given Hermes cleanup tooling, but not confirmed.

Orca version

Unknown — reported 2026-07-17 on X.

Details

Summary

User reported that stale OMP (omp / oh-my-pi) sessions started in Orca kept running after their terminals were gone, some for ~2 days / up to 38 hours, and consumed enough memory that they had to kill the tree with Hermes.

OMP author (@_can1357) noted that the foreground omp process itself was still alive — so this is not only “detached child workers surviving parent death.” Worker daemons, eval workers, and Deno LSPs are expected children of a live OMP process; the Orca-side bug is that the owned agent PTY / foreground omp was not cleaned up when the terminal closed.

Source report

  • Original: https://x.com/beebyboss/status/2078024006167384224
  • Reply (OMP author): https://x.com/_can1357/status/2078025967440630110

What Hermes reported as killed (from the user post)

Process class Count / notes
Stale omp sessions 11 — orphaned from Wednesday terminals; some running ~38h
Worker daemon brokers + eval workers 4 brokers + 35 eval workers — children spawned by those omp sessions
Deno LSP language servers 7 — one per dead terminal (expected under live omp)
Already self-terminated 1 ocuagent

Process tree attribution (reporter / Hermes):

  • Foreground processes were omp --extension orca-agent-status.ts (Orca-managed OMP extension path).
  • All traced back to oh-my-pi / OMP coding sessions whose terminals were never properly cleaned up when closed.
  • Hermes analysis: “Basically every omp invocation spawns a Deno LSP + worker pool, and none of them got cleaned up when the parent terminal closed.”
  • OMP author: “the lsp et al. is normal, since the foreground process was also left alive… I can add an idle timeout, but bandaid. I don't quite know what happens when you 'background' it in Orca.”

Expected behavior (Orca “background” semantics)

Background in Orca is UI presentation, not process detachment.

When a terminal is created without focus (presentation: 'background' / no --focus):

  1. Orca still spawns a normal owned PTY with the agent command.
  2. It does not steal UI focus / may skip forced tab reveal.
  3. The process is fully live and addressable via terminal handles.
  4. It is not shell bg / nohup / daemonize.

On terminal close / kill, Orca is supposed to:

  1. Shut down the PTY (SIGTERM, then process-group force kill after ~5s on POSIX).
  2. For agent sessions (including omp when launchAgent is set or the command is recognized), also run a descendant tree kill (ppid snapshot → SIGTERM → delayed SIGKILL) because agent CLIs spawn tool children in detached process groups that SIGHUP never reaches.

Relevant code:

  • Background create: src/main/runtime/orca-runtime.ts (createTerminal, resolveTerminalPresentation, shouldCreateInBackground)
  • CLI default background unless --focus: src/cli/handlers/terminal.ts
  • Agent descendant kill policy: src/main/providers/local-pty-provider.ts (ptyAgentSessionIds, shutdownTrackedPty)
  • Descendant sweep: src/main/pty-descendant-termination.ts
  • PTY TTY process-group force kill: src/main/pty/posix-pty-process-groups.ts
  • OMP is a first-class TUI agent: src/shared/tui-agent-config.ts (omp)

Actual behavior

  • Foreground omp processes (with Orca’s orca-agent-status extension) remained alive after terminals were gone.
  • Child worker pools + Deno LSPs stayed up under those parents and accumulated over days.
  • User had to clean up outside Orca (Hermes).

Impact

  • Unbounded memory growth from leaked agent sessions + LSP + worker pools.
  • Especially painful when multiple OMP terminals are opened over days and closed without successful teardown.
  • User-visible as “Orca is eating my RAM” even when no agent tab appears active.

Hypotheses to investigate (ordered)

  1. Close path never ran pty.kill / provider shutdown for those tabs

    • UI tab close without kill ownership
    • Headless / background-surface tabs that drop from graph without kill
    • Runtime/CLI close targeting a handle that no longer maps to the live PTY
  2. Daemon-owned PTYs outlived app/runtime tracking

    • Related class: #7783 (Orca Helper / daemon keeps agent trees after UI/runtime reports not running)
    • Once runtime is gone, normal orca terminal * cannot clean them up (runtime_unavailable)
  3. Kill ran but failed to land on the shell/omp

    • Less likely for a still-attached foreground process given process-group SIGKILL, but possible if PID ownership was wrong, provider routing failed (pty:kill / daemon startup race — see comments around #7742 in src/main/ipc/pty.ts), or the process was reparented off the PTY TTY
  4. Session not agent-marked

    • Only agent-marked PTYs get descendant sweep
    • If user typed omp in a plain shell tab without recognition, children can survive classic shell kill — still should not leave an attached foreground omp if the PTY root was force-killed
  5. OMP-side reparenting of workers (secondary)

    • Even with correct parent kill, double-forked / reparented workers can survive if they leave the ppid tree before snapshot
    • OMP author already suggested idle timeout as a band-aid for that class
    • Does not explain live foreground omp after terminal close

Related issues

Issue Relationship
#7783 Same lifecycle class: managed agent trees survive after Orca UI/runtime is gone (macOS daemon Helper). This report is specifically OMP + memory + terminal-close / stale-for-days.
#9414 Tracking for recognition-independent lifecycle safety; may cover unrecognized agents, but OMP is recognized.
#8459 Resource Manager orphan false-positives — inverse problem (killing live sessions). Don’t conflate.
#9193 Close/tab handle resolution gaps for floating PTYs.

Reproduction checklist (needed)

We do not yet have a conclusive in-repo repro. Please capture:

  • [ ] Orca version + OS
  • [ ] How OMP was started: Agent button / orca terminal create --command omp / typed in shell / worktree startup
  • [ ] Focused vs background (--focus or not)
  • [ ] Daemon on or off
  • [ ] Exact close path: tab X, worktree remove, app quit, mobile close, etc.
  • [ ] Whether Orca still listed the terminal before cleanup (orca terminal list --json if runtime up)
  • [ ] Sample process tree while leaked (ps / Activity Monitor / Hermes dump), including PIDs of shell, omp, brokers, Deno LSP
  • [ ] Whether foreground omp was still parented under Orca’s login/zsh/PTY or had reparented to init

Suggested repro probes

# After starting OMP in Orca and closing the terminal tab:
pgrep -fl 'omp|deno|orca-agent-status'
orca status --json
orca terminal list --json   # if runtime reachable
  1. Start OMP via Orca agent launch (so orca-agent-status extension is present).
  2. Close the terminal tab normally.
  3. Immediately check whether omp / Deno LSP / worker processes remain.
  4. Repeat with CLI background create (orca terminal create --command 'omp …' without --focus) and with app quit.

Acceptance criteria

  • Closing an Orca terminal that owns an OMP session terminates:
    • the PTY shell
    • the foreground omp process
    • descendants still in the agent tree (workers / LSP) that are reachable via process-group or descendant snapshot
  • After app quit (or with daemon keep-alive policy explicitly defined): either full tree is killed, or leftovers are visible and stoppable without manual process hunting.
  • No multi-day accumulation of Orca-started omp sessions after their terminals are closed.

Notes for OMP coordination

  • Foreground leak = Orca responsibility.
  • Idle timeout / self-cleanup for reparented workers + LSP when parent dies or TTY vanishes would still be a useful safety net on the OMP side (as @_can1357 suggested).

AmethystLiang avatar Jul 20 '26 07:07 AmethystLiang

Same lifecycle class on Windows with Codex/Grok (Orca 1.4.188), not only OMP:

  • 9 live Codex sessions → 27 tavily-mcp node processes and 26 AnySearch/askecho python processes (one MCP interpreter tree per agent)
  • After closing tabs, copies remain (cmd.exe /c tavily-mcp → node, plus uv/python askecho). codex.exe count drops; MCP often does not.

Acceptance from this issue (PTY + foreground agent + descendants) would cover that Windows MCP path too. Measurements: https://github.com/stablyai/orca/issues/16211

sanshengai avatar Aug 24 '26 02:08 sanshengai

Lifecycle work now covers the two distinct paths:

  • #20642 (merged) stops detached OMP tools on immediate local terminal close.
  • #20645 (merged) covers the relay close path.
  • #21629 covers app-quit cleanup for local fallback agent PTYs while preserving daemon-backed OMP sessions across a normal Orca quit. Explicit tab close remains the intentional teardown path.

I exercised the #21629 focused shutdown tests (76 passed), node typecheck, changed-code quality, and package builds. Its required CI is currently blocked by unrelated current-main failures: the anti-slop shapes naming gate and pane-title-update-global-scan-budget assertion. I am leaving this issue open until #21629 is merged and the full lifecycle gate is green.

nwparker avatar Sep 19 '26 09:09 nwparker

Current-main fix is prepared as #21668. It targets local POSIX OMP PTY process-group cleanup during Orca app quit. Daemon-backed sessions remain alive across app quit; explicit tab/worktree closure still terminates the session. Focused lifecycle tests, node typecheck, and changed-file quality checks pass; required CI is running.

nwparker avatar Sep 19 '26 12:09 nwparker

PR #21668 is merged (2e278426efa1). It force-cleans local POSIX OMP PTY process groups during app quit while preserving daemon-backed session survival. Focused lifecycle tests, node typecheck, full CI, packaging, and verify passed.

nwparker avatar Sep 19 '26 12:09 nwparker

Fixed by #21668.

nwparker avatar Sep 19 '26 12:09 nwparker