Repository navigation
fix(ssh): reap orphaned relay PTYs on reconnect before they exhaust the session cap - #9820
TrentFisher6 wants to merge 3 commits into
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📥 CommitsReviewing files that changed from the base of the PR and between bbe0d0db06606ad5c7791661313378a97a75597d and c8b5d701222b6467617deeb01fe587aaa97cf80d. 📒 Files selected for processing (7)
🚧 Files skipped from review as they are similar to previous changes (5)
📝 WalkthroughWalkthroughAdds pane-key and start-time metadata to PTY process records. 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 8a58a338-6106-46a5-9a60-50a049e1e1e0
📥 Commits
Reviewing files that changed from the base of the PR and between 6e6b7d8 and db4267789521cdf73c4ef8610b34acefe23acf30.
📒 Files selected for processing (6)
src/main/providers/types.tssrc/main/ssh/ssh-relay-session-orphan-pty-reap.test.tssrc/main/ssh/ssh-relay-session.test.tssrc/main/ssh/ssh-relay-session.tssrc/relay/pty-handler.test.tssrc/relay/pty-handler.ts
bbe0d0d to
c8b5d70
Compare
…he session cap
Nothing reconciled the relay's live PTY list with what the app still
tracks, so relay slots leaked until the 50-session cap blocked new
terminals ("Maximum number of PTY sessions reached (50)"):
- closing a terminal while the SSH connection is down tombstones the
lease locally but never kills the remote PTY; reconnect then cancels
the relay's grace window, so the orphan lives forever
- a transient pty.shutdown failure closes the pane with no retry
- worktree teardown only sweeps the local provider, never SSH relays
Fix: after reattachKnownPtys, list the relay's PTYs and shut down any
pane-bound entry (paneKey set) the app no longer owns or leases. Bare
relay shells (e.g. remote CLI) are never touched, and entries younger
than 30s are skipped so a spawn racing the sweep can't be reaped. The
relay now reports paneKey and startedAtMs in pty.listProcesses to
support this; the sweep is best-effort and never blocks session ready.
Co-Authored-By: Claude Fable 5 <[email protected]>
Review follow-ups: keep toRelaySshPtyId inside the per-session try/catch so one foreign/malformed entry can't abort the sweep or tear down the session, and report relay-host-computed ageMs instead of startedAtMs so local/remote clock skew can't defeat the young-spawn guard. Co-Authored-By: Claude Fable 5 <[email protected]>
c8b5d70 to
cc4861b
Compare
Greptile SummaryThis PR fixes a relay PTY slot leak on SSH remotes where terminals closed while disconnected, transient shutdown failures, or worktree teardowns were not reflected back to the relay daemon, eventually exhausting the 50-session cap and blocking new terminal spawns.
Confidence Score: 5/5Safe to merge — the sweep is entirely best-effort, isolated behind per-entry try/catch blocks, and cannot prevent the session from reaching The change is SSH-provider-scoped and touches no local, WSL, or renderer paths. Every guard against false-positive reaping is correctly implemented and independently tested. Files Needing Attention: No files require special attention.
|
| Filename | Overview |
|---|---|
| src/main/ssh/ssh-relay-session.ts | Adds reapOrphanedRelayPtys() called after reattachKnownPtys on both establish and reconnect; correct ID-format handling, isolated per-entry try/catch, and known-set-build failure guard are all present and correct. |
| src/relay/pty-handler.ts | Stamps startedAtMs via ??= in wireAndStore and adds paneKey/ageMs to listProcesses output; both fields are optional and additive. |
| src/main/providers/pty-process-info.ts | Adds optional paneKey and ageMs to the shared PtyProcessInfo type; purely additive. |
| src/main/ssh/ssh-relay-session-orphan-pty-reap.test.ts | New test file covering all guard paths and failure resiliency. |
| src/main/ssh/ssh-relay-session.test.ts | Adds listProcesses and shutdown mocks to keep pre-existing tests passing. |
| src/relay/pty-handler.test.ts | Verifies pty.listProcesses returns paneKey and ageMs correctly. |
Reviews (2): Last reviewed commit: "fix(ssh): skip orphan sweep entirely whe..." | Re-trigger Greptile
Review follow-up: toRelaySshPtyId in the known-set construction could throw on a corrupt ownership entry and escalate a harmless sweep into a full connection teardown. Worse than aborting, a partial known set could reap PTYs the app still owns — so any failure now skips the whole sweep. Co-Authored-By: Claude Fable 5 <[email protected]>
|
SSH still works: Orca connects to the host over SSH and routes workspace operations through the managed remote Orca server (orcad). These merges remove the legacy SSH relay, not SSH support. Closing as obsolete after the merged SSH relay retirement in #27214 and #27215. This PR concerns the SSH relay PTY attach, recovery, replay, or lease lifecycle. #27214 removed the desktop relay connection/fallback path; #27215 deleted the SSH relay server, deployment assets, and relay-only tests. SSH hosts now use the managed Orca server (orcad), and unsupported hosts fail explicitly, so this relay-specific work is no longer planned. This closes the proposed relay implementation; it does not claim that any independent local, WSL, or managed-server behavior was fixed. |
Fixes #9819
Summary
On SSH remotes, relay PTY slots leak until every new terminal spawn fails with "Maximum number of PTY sessions reached (50)", and closing terminals/worktrees doesn't recover them. The relay daemon survives app restarts (reconnects cancel its grace window), slots are only freed by an explicit
pty.shutdown, and nothing reconciled the relay's live PTY list with what the app still tracks. Terminals closed while the connection was down (lease tombstoned locally, remote PTY never killed), transient shutdown failures, and worktree teardown (which only sweeps the local provider) all leaked slots permanently. Full analysis in #9819.User-visible change: after this fix, connecting or reconnecting to an SSH remote garbage-collects orphaned relay terminals, so long-running setups no longer hit the 50-session cap. No workflow or UI changes.
How it works
After
reattachKnownPtyson bothestablish()andreconnect(), the session runs a best-effort orphan sweep:paneKeycaptured fromORCA_PANE_KEYat spawn) — bare relay shells such as remote-CLI sessions are never touchedageMs) is computed on the relay host's clock so local/remote clock skew can't weaken the guard, and the known set is built after listing so a spawn that lands mid-sweep counts as knownageMs(relay predating this change) — age unknown, never reapedreadyTo support this, the relay's
pty.listProcessesnow reportspaneKeyandageMs(start time stamped inwireAndStore, covering both spawn and revive). Both fields are optional and additive; version skew in either direction degrades to "no reaping" rather than misbehavior.Screenshots
Testing
pnpm lint— oxlint clean on every changed file; the full run fails only on a pre-existingswitch-exhaustivenesserror insrc/renderer/src/components/skills/skill-freshness-group.tsxthat reproduces identically on untouchedorigin/mainin my environmentpnpm typecheck— cleanpnpm test— every suite touching this change passes (src/main/ssh,src/main/providers,src/relay/pty-handler.test.ts); the full run has 48 failures that reproduce identically on untouchedorigin/mainin my environment (Node 26 vs the repo's required Node 24), all in files this PR does not touchpnpm build— cleanNew tests:
src/main/ssh/ssh-relay-session-orphan-pty-reap.test.ts: reaps orphans on establish and reconnect; spares bare shells, young spawns, unknown-age entries, and leased PTYs; a foreign-connection entry cannot abort the sweep or the session; alistProcessesfailure still reachesreadysrc/relay/pty-handler.test.ts:pty.listProcessesreportspaneKey/ageMsAI Review Report
Reviewed with Claude Code (Claude Fable 5), plus the CodeRabbit pass on this PR. Risks checked and outcomes:
Date.now()arithmetic and optional metadata fields; no paths, shortcuts, shell behavior, signals, or Electron platform differences are touched. The main-process sweep is platform-neutral. No renderer/UI changes.ageMs→ nothing reaped; new relay + old app → extra optional fields ignored.paneKey); agent panes in worktrees are pane-bound and correctly covered. The relay's per-instance socket means the sweep can only ever see PTYs created by this app install.ageMs.toRelaySshPtyIdcould throw outside the try/catch and tear down the session on a malformed entry — fixed by moving it inside the per-session try/catch, with a regression test.pty.listProcessesround-trip per connect/reconnect (not a hot path); shutdowns are sequential and bounded by the 50-session cap.Security Audit
session.id,paneKey, andageMscome from the remote relay and are treated as untrusted — ids pass through the existingtoRelaySshPtyIdvalidation (throws on foreign connection ids, now safely contained per-entry),ageMsis type-checked, andpaneKeyis only used as an opaque presence marker and in a localconsole.warn.pty.shutdownRPCs back to the same relay the data came from.pty.listProcessesgains optional response fields only.markSshRemotePtyLeaseno-ops for unknown leases, so a hostile relay can't use the sweep to corrupt lease state beyond marking its own PTYs terminated.Notes
ageMsare never reaped from — the fix fully activates once the relay redeploys (which happens automatically on version match failure).kill()'s close-while-disconnected path record a pending kill instead of tombstoning the lease, so orphans are prevented at the source rather than only GC'd.🤖 Generated with Claude Code
ELI5
SSH relay PTY slots leaked across reconnects until new terminals hit the 50-session cap. On reconnect Orca reaps orphaned remote PTYs that the app no longer tracks so slots free up again.