Repository navigation
fix(ssh): a Disconnect or Remove during a background connect sticks - #29918
Conversation
A background connect (managed tunnel, census, runtime context) that was waiting for the old transport to close resumed after the user's Disconnect or Remove host and registered a fresh connection. Re-check the attempt token after that await so the cancelled attempt never registers.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Priority: ➖ Normal Merge Risk: ⚪ Minimal · up to
|
ELI5
If you click Disconnect (or Remove host) on an SSH host at the exact moment Orca is quietly reconnecting it in the background, the background reconnect used to win: the host came back as Connected a moment later. Now your click wins.
What Changed
Before: a background connect (managed-server tunnel, host census, runtime context) that found a dropped connection first waited for the old transport to close. If the user clicked Disconnect or Remove host during that wait, the connect resumed afterwards and registered a brand-new connection. The host showed Connected again, and a removed host kept a live SSH session in the pool.
After: after that wait the connect checks whether it is still the current attempt. A Disconnect or Remove invalidates the attempt, so the connect stops with "Connection to was cancelled" and registers nothing.
Mechanism:
SshConnectionManager.connectalready had a per-attempt token thatdisconnect()deletes, but it only read the token infinally. It now re-checks the token right afterawait existing.disconnect(), the only await before a new connection is registered. Once registered, a connection is already reachable bydisconnect(), andSshConnectionitself refuses a late handshake after it is disposed, so no later check is needed.Why
The token is already the "user cancelled this attempt" signal, and Remove host also goes through
disconnect(), so a separate disconnect or removed generation counter would carry the same information. A newer connect started after the Disconnect also replaces the token, so the stale attempt backs off in that case too.Relation to STA-10472 ("Disconnect doesn't stick"): this closes the in-flight half, a Disconnect that lands while a background connect is mid-way. STA-10472 also describes a background status check that starts a fresh connect after the Disconnect has fully finished. That needs a user-disconnected record for managed SSH environments and is not changed here, so this PR does not close STA-10472.
Linked Issue
Found by a systemic audit (no GitHub issue). Partly addresses STA-10472, as described above.
Visual Proof
N/A: there is no UI change. The connection state is verified by the live run below.
Testing
New unit test
keeps a Disconnect that lands while a background connect closes the old transportinsrc/main/ssh/ssh-connection-manager.test.ts. It controls when the old transport finishes closing, so the timing is deterministic. It fails on main (the background connect resolves and registers a new connection) and passes with the fix.pnpm test src/main/ssh/ssh-connection* src/main/ssh/orcad-managed-tunnel.test.ts src/main/ipc/ssh* src/main/startup/headless-serve-ssh-registration.test.ts: 517 passed.Live on Linux (omarchy), isolated: I ran a private user-level sshd on a free port with its own host key and a throwaway authorized key, reached through my own
ssh -Ltunnel and a temporary HOME. A throwaway harness drove the realSshConnectionManagerandSshConnection. It connected, killed the remote session so the connection dropped, started a background connect, and clicked Disconnect in the same moment.{"outcome":"registered connected","pooled":"connected","sessions":1}. The host came back, with one live SSH session on omarchy.{"outcome":"rejected: Connection to omarchy private sshd was cancelled","pooled":null,"sessions":0}.Full static-analysis job steps run locally (typecheck through
pnpm tc).I manually tested these changes locally
Automated tests added/updated, or explained why not below
Review
Codex review (gpt-6.1-sol, high): no findings.
Agent skill upstream boundary
Notes
Desktop-only connection pool logic. No wire change, so mixed client and host versions are unaffected. Cross-platform: no platform-specific code.
Checklist
N/Awith reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (or CI will cover; local preferred)