Skip to content

fix(ssh): a Disconnect or Remove during a background connect sticks - #29918

Merged
nwparker merged 1 commit into
mainfrom
nwparker/ssh-wsl-lifecycle-races
Oct 17, 2026
Merged

nwparker merged 1 commit into
mainfrom
nwparker/ssh-wsl-lifecycle-races

Conversation

@nwparker

@nwparker nwparker commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​24 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​24
Prod 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​5 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​5

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.connect already had a per-attempt token that disconnect() deletes, but it only read the token in finally. It now re-checks the token right after await existing.disconnect(), the only await before a new connection is registered. Once registered, a connection is already reachable by disconnect(), and SshConnection itself 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 transport in src/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 -L tunnel and a temporary HOME. A throwaway harness drove the real SshConnectionManager and SshConnection. It connected, killed the remote session so the connection dropped, started a background connect, and clicked Disconnect in the same moment.

    • main: {"outcome":"registered connected","pooled":"connected","sessions":1}. The host came back, with one live SSH session on omarchy.
    • this branch: {"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

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

Desktop-only connection pool logic. No wire change, so mixed client and host versions are unaffected. Cross-platform: no platform-specific code.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

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.
@coderabbitai

coderabbitai Bot commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: d0bd0857-205d-4aed-9434-2f42852efa45
📥 Commits

Reviewing files that changed from the base of the PR and between 6bbc3cb and ef5e990.

📒 Files selected for processing: 2
  • src/main/ssh/ssh-connection-manager.test.ts
  • src/main/ssh/ssh-connection-manager.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 3 remain after this review.

📝 Walkthrough

After disconnecting an existing connection, SshConnectionManager.connect checks whether its attempt is still registered. If a concurrent disconnect invalidated the attempt, connect throws a cancellation error before registering a replacement. A regression test verifies this behavior while the existing connection’s close is pending.

Priority: ➖ Normal

Merge Risk: ⚪ Minimal · up to ef5e9

The in-flight reconnect cancellation change has no identified merge-blocking issue. The separately noted case of a new background connect after Disconnect completes remains outside this change.

Pre-merge checks: Passed 5
Check name Status Explanation
Title check Passed The title is concise, specific, and accurately describes the fix for Disconnect or Remove during a background SSH connect.
Description check Passed The description is detailed and covers the user impact, implementation, rationale, testing, scope, and checklist. The Linked Issue section does not provide an actual issue link, but the rest of the re…
Docstring Coverage Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Description check

Explanation

The description is detailed and covers the user impact, implementation, rationale, testing, scope, and checklist. The Linked Issue section does not provide an actual issue link, but the rest of the required content is substantially complete.

Full details: Docstring Coverage

Explanation

No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2 files.

✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@nwparker
nwparker merged commit 03be5bb into main Oct 17, 2026
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant