Skip to content

Remote mirror pty-exit can kill live host terminal sessions #8871

Closed

Description

@bbingz

Summary

A paired desktop client can terminate a live terminal on the host when its local remote-terminal mirror reports pty-exit (for example while retiring a stale remote PTY handle during reconnect/restore).

This destroys long-running terminal workloads such as Claude Code sessions. The affected workload does not need to be a native provider integration; the observed sessions were Claude Code processes configured to use a Grok API endpoint.

Observed behavior

  • Agent sleep was disabled on the host.
  • A paired client's renderer bootstrapped at 18:36:08.431 local time.
  • The host daemon logged an explicit session-killed for an existing terminal at 18:36:11.779, only 3.348 seconds later.
  • Additional mirrored terminal sessions were explicitly killed during the same paired-client activity window.
  • These were session-killed events, not natural session-exited events.

Root cause

The renderer exit path is:

  1. The remote PTY transport retires/disconnects a mirrored PTY and invokes onPtyExit.
  2. Terminal.tsx calls closeTerminalTab(tabId, { reason: 'pty-exit' }).
  3. For a remote-owned worktree, closeTerminalTab prunes the local mirror and unconditionally calls closeWebRuntimeSessionTab.
  4. The host receives session.tabs.close and kills the authoritative PTY.

A mirror-local pty-exit is reconciliation evidence, not user intent to close the host-owned tab. Propagating it back to the host turns stale mirror state into a destructive host action.

Relevant code:

  • src/renderer/src/components/terminal/terminal-tab-actions.ts
  • src/renderer/src/components/terminal/Terminal.tsx
  • src/renderer/src/runtime/remote-runtime-pty-transport.ts

This appears related to the remote close propagation added around #8628: explicit user/cleanup closes should reach the host, but natural/mirror pty-exit must not.

Expected behavior

When a remote mirror reports pty-exit, Orca should reconcile/prune only the local mirror and wait for the authoritative host snapshot. It must not send session.tabs.close to the host.

Explicit user close and cleanup actions should continue to propagate to the host.

Regression test

A focused unit test currently fails because closeWebRuntimeSessionTab is called once for:

closeTerminalTab('local-tab-1', { reason: 'pty-exit' })

The fix should make that call local-only while preserving the existing explicit-close behavior.

Activity

  1. bbingz commented on Jul 15, 2026

    @bbingz
    ContributorAuthor

    Additional forensics from a full incident analysis tonight (multi-agent code + log investigation; happy to share more detail):

    1. The kill path is reachable without any remote client. The host's own daemon log shows three force-only session-killed events before the remote client ever connected (09:17:57, 18:00:07, 18:03:33 local that day), each preceded a few minutes earlier by creation of a replacement terminal in the same project and bracketed by host-local sidebar activity — i.e. the host renderer itself can retire a stale handle and echo the fatal close. This is a core session-lifecycle defect, not just a remote-mirror scenario.

    2. Direct trigger evidence for the connect-time kill. The first post-connect kill landed 3.35s after the client's renderer_bootstrap_started, and the client's store snapshot diff shows exactly the killed tabs being dropped client-side. This matches restored mirror panes attaching with persisted handles: attach() subscribes the saved handle without re-deriving it from the current session snapshot (remote-runtime-pty-transport.ts:690-728), host handles are process-lifetime and cleared on renderer reload (orca-runtime.ts:19447-19457), and desktop clients get no waitForLeafPtyId grace (rpc/methods/terminal.ts:1987-2001) — so a stale persisted handle deterministically yields no_connected_pty, which the transport converts into a synthesized pty-exit.

    3. Scope note for reviewers of #8872. The PR deliberately guards only the reason:'pty-exit' echo in closeTerminalTab. Known residual paths, which I'd suggest treating as follow-ups rather than blockers: the store closeTab retirement path, reasonless automated closeTerminalTab callers, attach()'s persisted-handle subscribe (trigger-side), and host-side hardening (closeMobileSessionTab could verify tab generation/expected pty before killing the live one).

    4. Attribution gap. None of the incident kills could be attributed to a client because the RPC context's identity fields (ctx.clientId / clientKind / connectionId — populated at runtime-rpc.ts:1150-1155, defined at rpc/core.ts:66-77) are dropped in the session.tabs.close handler, and the daemon's session-killed log omits the requester. I'm filing a tiny PR adding both log fields — it would have turned this investigation from inference into a one-line lookup.

    5. Related feedback loop. pty-exit-reason closes never retire sleepingAgentSessionsByPaneKey resume records, so each mirror-echo kill converts a live session into a pending duplicate resume on the next connect — filed separately with full analysis.

  2. bbingz commented on Jul 15, 2026

    @bbingz
    ContributorAuthor

    Correction to point 1 of my comment above (after an independent second review of the evidence):

    I'm downgrading the claim that "the kill path is reachable without any remote client." It doesn't hold up:

    • A host-local closeTerminalTab(reason:'pty-exit') never takes the runtime session.tabs.close branch (terminal-tab-actions.ts:140 — that branch requires an active web-runtime session), and the store closeTab path performs no PTY retirement/kill at all (terminals.ts:1218). Local pty-exit handling therefore cannot produce the mirror-echo kill.
    • The three pre-connect kills (09:17:57, 18:00:07, 18:03:33 local) are single force-only requests (immediate:true) — a different request shape from the graceful+force pairs (immediate false → true, milliseconds apart) that the closeMobileSessionTab echo path produced at 19:08–19:10.

    Those early kills should be read as unattributed (plausibly ordinary user-initiated closes shortly before leaving the desk), not as evidence of a host-local echo path.

    One honest nuance this surfaces for reviewers: the first post-connect kill (18:36:11, 3.35s after the client renderer bootstrapped, matching the client store dropping exactly that tab) is also a single force-only request rather than a graceful+force pair — so while the timing and client-store evidence still tie it to the client reconnect, the exact RPC family it traveled through is not pinned down by the current logs. That is precisely what the attribution-logging PR is for.

    Points 2–5 of my earlier comment stand as written.

  3. contextor2 commented on Jul 15, 2026

    @contextor2

    We hit what looks like exactly this bug three times in a single session and captured clean forensics that may help — including the attribution gap noted in #8888.

    Environment: Orca v1.4.141, macOS 26.2 (arm64, M4 Mac mini). Host desktop app running locally; paired remote clients (mobile scope + a second "runtime"-scope device) connecting through serve.

    Incidents (local time KST = UTC+9, 2026-07-16): mass session-killed bursts at 02:32:24, 02:42:33, and 03:02:40–41. The bursts killed the live worktree PTY sessions — 4, 7, and 7 distinct worktrees respectively (plus the serve session itself in the third burst; 10 distinct worktrees across the three bursts: Claude Code coordinator/workers, Codex, OpenCode/GLM) — predominantly with immediate:true in daemon.log.

    Key correlation: each burst coincides — within ~1 second — with a new remote-client renderer process spawning on the host (Orca Helper (Renderer) with a fresh --renderer-client-id, accompanied by an agent-browser sidecar spawn). Process start times from ps lstart:

    Renderer spawn (KST) Mass-kill burst (UTC, daemon.log)
    02:32:24 17:32:24.847–25.23x
    02:42:33 17:42:33.435–33.833
    03:02:40 18:02:40.992–41.277

    This matches the 3.3-second bootstrap→kill correlation in the original report.

    Third incident timeline (UTC):

    • 17:58:33 — serve session serve-7dff501c created (remote client connects)
    • 18:02:40 — new renderer + agent-browser spawn on host (client reconnect/bootstrap)
    • 18:02:40.992–41.277 — 10+ sessions killed with immediate:true, including the serve session itself
    • 18:02:59 — paired runtime-scope device lastSeenAt updates; killed sessions start getting recreated (with restored scrollback) immediately after

    Ruled out: host app and daemon PIDs were stable across all three incidents (no crash/restart), no system sleep (pmset log, AC power), no memory pressure (renderer heap ~133MB of 3.5GB limit). These were explicit kill requests handled by the daemon, not session-exited.

    Code-level observation (v1.4.141 bundle): in closeTerminalTab, options.reason === "pty-exit" is only consulted to skip the pinned-tab confirmation; the remote-worktree branch then forwards every close to the host via session.tabs.close, whose params carry no reason/intent field — so closeMobileSessionTab on the host cannot distinguish a stale-mirror pty-exit reconciliation from a deliberate user close. Consistent with #8872's analysis.

    Until #8872 ships we're running a local host-side mitigation (a guard in closeMobileSessionTab that refuses session.tabs.close for terminals currently running an agent). Happy to share raw daemon.log excerpts or the full timeline if useful.

  4. AmethystLiang commented on Jul 17, 2026

    @AmethystLiang
    Contributor

    will resolve this issue today

  5. nwparker commented on Aug 3, 2026

    @nwparker
    Contributor

    Closing as fixed

    Verified on current tree: mirror-local pty-exit no longer propagates a host tab close.

    In closeTerminalTab (src/renderer/src/components/terminal/terminal-tab-actions.ts), when a web/runtime session is active:

    if (options?.reason === 'pty-exit') {
      // Why: stream exit is not host-tab closure; the HUB snapshot decides whether reconnect restores or removes this tab.
      return
    }

    Unit coverage asserts closeWebRuntimeSessionTab is not called for reason: 'pty-exit' (terminal-tab-actions.test.ts — “lets the HUB snapshot adjudicate a stream exit”).

    Fix PR: #9994
    (41751dd90d — introduced the reason === 'pty-exit' early-return)

    If anyone still sees host session-killed from a remote mirror stream-exit after a build that includes #9994, please reopen with logs.

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 workingorca-remote-serverRemote Orca Server / orca serve / paired remote runtime

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions