Repository navigation
fix(ssh): reconnect persisted PTYs after shutdown races - #8403
Conversation
|
@cubic-dev-ai please review |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughWorkspace shutdown persistence derives active SSH connection IDs from durable remote PTY session IDs alongside observed connection states. Payload and patch builders reuse computed terminal session data, while startup reconnect excludes runtime-owned targets. Tests cover reconnecting, explicitly disconnected, and runtime-owned SSH sessions. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 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: 3374f43b-aa7d-46de-a0cb-22a0f56892ef
📒 Files selected for processing (2)
src/renderer/src/lib/workspace-session-liveness.test.tssrc/renderer/src/lib/workspace-session.ts
- exclude runtime-owned (ephemeral-VM) targets: a renderer-driven startup ssh.connect would dispose the runtime layer's live relay session - exclude explicitly disconnected / auth-failed targets so startup never dials a host the user chose to leave offline (sessions still restore on tab focus via the deferred flow) - avoid re-running buildTerminalSessionData via the default parameter when the remote-session map is legitimately empty (null sentinel) - recompute activeConnectionIdsAtShutdown alongside terminal-field patches so a crash between patches cannot strand a stale target - pin all of the above with tests, including the patch-writer path Co-authored-by: Orca <[email protected]>
Main's windowless-promotion path can persist runtime-owned ids into activeConnectionIdsAtShutdown; renderer must never dial those. Co-authored-by: Orca <[email protected]>
Give the runtime-owned fixture a transient status so the status gate alone cannot exclude it; verified by mutation (guard removal fails). Co-authored-by: Orca <[email protected]>
nwparker
left a comment
There was a problem hiding this comment.
Reviewed adversarially (two independent verification passes, mutation-tested the new guards). The root-cause analysis is correct and the fix direction is right: activeConnectionIdsAtShutdown only reflected the instantaneous connection-state map, so a shutdown captured mid-relay-drop persisted the surviving PTY id but never reconnected its host, stranding the session until manual tab focus.
I pushed three hardening commits on top (thanks for maintainer-edit access):
1. d956bca — derivation hardening. The original union had three issues found in review:
- Runtime-owned (ephemeral-VM) targets leaked into the eager-connect list.
ssh:runtime-ssh-*@@pty-Nsession ids parse to runtime-owned connection ids, which the old connected-states source could never contain (their state changes are never broadcast to the renderer). Worse than a wasted call:doConnectdoesn't guard runtime-owned ids and disposes any existing session, so a renderer-driven startup connect could tear down the runtime layer's live VM relay. Now excluded, pinned by a mutation-verified test. - Explicit user disconnects became auto-dials at next launch. An intentional Disconnect leaves
status: 'disconnected'plus a preservedlastKnownRelayPtyIdByTabIdentry — exactly the state shape of the new test — so the union couldn't distinguish user intent from a transient drop. Involuntary drops land inreconnecting/reconnection-failed/error, neverdisconnected, so the gate now requires a non-disconnected/auth-failedobserved status. Sessions on excluded hosts still restore on tab focus via the deferred flow; only eagerness is withheld. - CodeRabbit's double-evaluation finding — when the map is legitimately empty (
undefined), explicitly passing it re-triggered the default initializer's second fullbuildTerminalSessionDatascan in the shutdown-critical path. Fixed with anullsentinel.
Also: the patch writer only recomputed the list on sshConnectionStates changes, but the list now derives from terminal fields too — closing all tabs of a disconnected host then crashing left a stale target on disk that startup would dial. The terminal-fields patch branch now carries the derived list (reusing its already-computed terminalSessionData, so no extra scan).
2. aa8f78d — consumption-side filter. Main's windowless-promotion path (persistWindowlessPtyBindingsForDesktopAttach) can persist runtime-owned ids into this list independently of the renderer builder, so App.tsx startup also filters them at the consumption boundary.
3. cb0eb89 — test pinning. The runtime-owned test fixture originally passed via the status gate alone (no status entry); it now carries a transient status so only the runtime-owned check can exclude it — verified by mutation (removing the guard fails the test).
Verified clean on a merge with current main: 419 tests across the workspace-session/persistence/ssh-migration suites, web-renderer typecheck, oxfmt. Known trade-off (accepted): a target whose automatic reconnect ended in auth-failed at shutdown (e.g. ssh-agent briefly unavailable) loses the eager dial but keeps full restore-on-focus — preferable to stacking credential dialogs at startup.
workspace-session.ts crossed the max-lines budget; the derivation is a self-contained concern (session ids + live SSH state -> startup targets). Parameter is now required (null = none) since every caller has the terminal session data or must opt into the scan explicitly. Co-authored-by: Orca <[email protected]>
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: 7b40285e-0d96-448a-be9a-32a395498a70
📒 Files selected for processing (3)
src/renderer/src/lib/workspace-session-patch.tssrc/renderer/src/lib/workspace-session-reconnect-targets.tssrc/renderer/src/lib/workspace-session.ts
🚧 Files skipped from review as they are similar to previous changes (1)
- src/renderer/src/lib/workspace-session-patch.ts
…h too A pane-level optimistic write can stamp a runtime-owned connection state; the live 'connected' filter must apply the same exclusion as the session-id union (CodeRabbit review finding). Pinned in the test on both paths. Co-authored-by: Orca <[email protected]>
|
Field report: after v1.4.144 shipped (which includes this PR), I'm seeing a notable increase in unexpected app exits where Electron Utility subprocesses receive SIGTERM with I've opened #9191 with the full data. The key observations that may connect to this PR:
This may be unrelated to this PR's specific changes — but the timing is exact and the prior versions didn't show this crash frequency. Flagging here in case it helps narrow down the root cause. |
* fix(ssh): reconnect persisted PTYs after shutdown races * fix(ssh): harden shutdown reconnect-target derivation - exclude runtime-owned (ephemeral-VM) targets: a renderer-driven startup ssh.connect would dispose the runtime layer's live relay session - exclude explicitly disconnected / auth-failed targets so startup never dials a host the user chose to leave offline (sessions still restore on tab focus via the deferred flow) - avoid re-running buildTerminalSessionData via the default parameter when the remote-session map is legitimately empty (null sentinel) - recompute activeConnectionIdsAtShutdown alongside terminal-field patches so a crash between patches cannot strand a stale target - pin all of the above with tests, including the patch-writer path Co-authored-by: Orca <[email protected]> * fix(ssh): filter runtime-owned targets at startup reconnect consumption Main's windowless-promotion path can persist runtime-owned ids into activeConnectionIdsAtShutdown; renderer must never dial those. Co-authored-by: Orca <[email protected]> * test(ssh): pin runtime-owned exclusion independently of the status gate Give the runtime-owned fixture a transient status so the status gate alone cannot exclude it; verified by mutation (guard removal fails). Co-authored-by: Orca <[email protected]> * refactor(session): extract reconnect-target derivation to its own module workspace-session.ts crossed the max-lines budget; the derivation is a self-contained concern (session ids + live SSH state -> startup targets). Parameter is now required (null = none) since every caller has the terminal session data or must opt into the scan explicitly. Co-authored-by: Orca <[email protected]> * fix(ssh): exclude runtime-owned targets from the connected-states path too A pane-level optimistic write can stamp a runtime-owned connection state; the live 'connected' filter must apply the same exclusion as the session-id union (CodeRabbit review finding). Pinned in the test on both paths. Co-authored-by: Orca <[email protected]> --------- Co-authored-by: Neil <[email protected]> Co-authored-by: Orca <[email protected]>
Summary
Root cause
Orca already persisted the surviving remote PTY ID after an SSH transport drop, but
activeConnectionIdsAtShutdownonly reflected the instantaneous connection-state map. If shutdown captured after the target became disconnected, startup had the PTY ID but did not reconnect the host needed to attach it.Hardening added in review
ssh:runtime-ssh-*@@pty-Nparse to runtime-owned connection ids that the old connected-states source could never contain (their state is never broadcast to the renderer). A renderer-driven startupssh.connectto one would dispose the runtime layer's live VM relay session (doConnecttears down existing sessions). Excluded in the builder and filtered again at the App.tsx consumption boundary (main's windowless-promotion path can persist such ids independently).status: 'disconnected'+ a preserved relay session id — indistinguishable from the transient-drop shape by session data alone. Involuntary drops land inreconnecting/reconnection-failed/error, neverdisconnected, so the derivation requires a non-disconnected/auth-failedobserved status. Excluded hosts still restore on tab focus via the deferred flow; only eagerness is withheld.remoteSessionIdsByTabId— a crash between patches can no longer strand a stale target on disk.null= none), so the shutdown path never re-runs the repo/worktree scan (CodeRabbit's double-evaluation finding). Extracted toworkspace-session-reconnect-targets.tsto stay within the max-lines budget.Tests
workspace-session-liveness.test.ts: transient drop → included; explicit disconnect → excluded (session id still persisted); runtime-owned → excluded (mutation-verified: removing the guard fails the test)workspace-session-patch.test.ts: terminal-field patches carry the derived reconnect listmain: 419 tests across workspace-session/persistence/ssh-migration suites, web typecheck, oxlint, oxfmt, max-lines ratchet