Skip to content

fix(ssh): an SSH host comes back after restarting Orca instead of staying offline (STA-10476) - #28398

Merged
OrcaWin merged 1 commit into
mainfrom
nwparker/ssh-forward-quit-orphans
Oct 13, 2026
Merged

OrcaWin merged 1 commit into
mainfrom
nwparker/ssh-forward-quit-orphans

Conversation

@OrcaWin

@OrcaWin OrcaWin commented Oct 13, 2026 •

Copy link
Copy Markdown
Collaborator
Files Added Deleted Net
Test 5 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​340 $\color{#cf222e}{\Huge{\mathbf{−}}}$​7 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​333
Prod 9 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​250 $\color{#cf222e}{\Huge{\mathbf{−}}}$​12 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​238

ELI5

For SSH hosts reached through a jump host or proxy, Orca opens a separate ssh process to tunnel to the host's Orca server, on a fixed port on this computer. Quitting Orca did not stop that process. On the next launch the port was still taken, so Orca marked the host offline, and Reconnect could not fix it. Now Orca stops these processes when it quits. If one was left behind anyway (crash, force-quit, or quitting an older Orca during an update), the next launch finds that leftover and stops it.

What Changed

Before:

  • After quitting and reopening Orca (including every update), a ProxyCommand/ProxyJump host showed " is offline" and stayed offline.
  • Its workspaces fell out of their project.
  • Session History showed Local port 127.0.0.1:<port> is not available: EADDRINUSE.

After: the host reconnects on relaunch.

Mechanism:

  1. Quit. The SSH shutdown step now also closes every SSH port forward and every managed-server tunnel. It waits until each ssh -L process has exited, within the existing 6 s SSH quit budget; stopping already escalates to SIGKILL after 2 s.

    • Forwards are removed from their managers before they stop. The user's saved port forwards are therefore not marked closed, and they restore on the next connect as before.
    • The tunnel manager's new shutdown() also supersedes any tunnel build still in flight.
  2. Launch. When binding a fixed forward port fails with EADDRINUSE, Orca reads the process table once. It looks for a leftover Orca forward on that port, stops it (SIGTERM), and tries the port once more. A process qualifies only if:

    • its whole argv after the ssh executable is exactly what Orca would spawn for this same forward. That means the same options, the same -L 127.0.0.1:<port>:<remote>, and the same destination; only ExitOnForwardFailure=yes|no may differ, so a forward from an older version also matches. Any extra option means it is someone else's ssh.
    • and its parent is gone. On macOS/Linux that means reparented to init/launchd or a systemd --user subreaper. On Windows, the parent pid is missing or now names a younger process.

    Orca escalates to SIGKILL only if the same pid still matches and its start time is readable and unchanged. A pid reused after SIGTERM is never signalled again.

    As a result, a user's own ssh and a tunnel owned by another running Orca are never touched. If nothing qualifies, the original error stands.

Why

  • Quit alone is not enough. A crash or force-quit still leaves the process. So does the update itself: the quit before an update is done by the old version, which has no fix and no record of what it spawned. That is why recovery matches the process itself rather than relying on a pid file.
  • Moving to a dynamic local port would be the deeper fix, but it is too large for this release. The port is part of the stored pairing endpoint (ws://127.0.0.1:<port>), and the environment store checks that the tunnel and the pairing agree. Changing it means rewriting stored pairings on every connect.

Linked Issue

STA-10476

Visual Proof

N/A — no UI change; the host simply stays online across restarts.

Testing

  • Real host, on macOS against a Linux host behind ProxyJump:
    • Started a forward with this branch's code, then SIGKILLed its parent. The ssh -N -L 127.0.0.1:47120 process was reparented to pid 1 and kept the port.
    • reserveLocalForwardPort(47120) logged Stopping a forward a previous Orca left on 127.0.0.1:47120, then bound the port. A user ssh -N -L 127.0.0.1:47121 running next to it was untouched.
    • With the parent still alive, the same call left the forward alone and returned the original EADDRINUSE error.
    • A forward from an older version (ExitOnForwardFailure=yes) was also recovered.
    • An orphaned ssh to a different host alias on the same port was left alone, and so was one with one extra option.
  • Unit tests:
    • Quit waits for forward and tunnel processes to stop.
    • SshPortForwardManager.closeAll crosses connections and does not fire the closed callback.
    • Tunnel shutdown supersedes a late build.
    • Orphan matching on Linux subreaper, macOS, and Windows (missing or recycled parent).
    • Never matches a user ssh, another port with the same prefix, or a forward whose Orca is still live.
    • Recover-and-retry on EADDRINUSE.
  • pnpm tc:node, focused and src/main/{ipc,ssh,startup} tests (8515 passed), oxlint, pnpm run check:code-quality:changed, pnpm lint

Review

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

  • Cross-platform: the process table comes from the shared ps reader on macOS/Linux and the native Windows table, never from PowerShell.
  • SSH: the recovery only acts on processes on this computer, because the tunnel's listener is local.
  • No wire or persistence change.

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)

@coderabbitai

coderabbitai Bot commented Oct 13, 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: 3fc65d4d-35e3-442c-8a47-4af000d2dd12
📥 Commits

Reviewing files that changed from the base of the PR and between 72661531f8892fba1bd3b4445cf2629d9ae5c50b and 95c9898.

📒 Files selected for processing: 4
  • src/main/ssh/system-ssh-forward-process.test.ts
  • src/main/ssh/system-ssh-forward-process.ts
  • src/main/ssh/system-ssh-orphaned-forward.test.ts
  • src/main/ssh/system-ssh-orphaned-forward.ts

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

📝 Walkthrough

The changes add detection and cleanup for orphaned Orca system SSH forwards. Fixed-port binding retries when an address is in use and orphan cleanup succeeds. SSH shutdown now closes active port forwards and managed tunnels, and waits for their closure.

Priority: ➖ Normal

Merge Risk: 🟡 Moderate · up to 95c98

SSH forwards or managed tunnels can remain active after shutdown begins. Close these lifecycle gaps before merging unless the shutdown risk is explicitly accepted.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 34.78% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 23 functions across 14 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
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.
Title check Passed The title clearly identifies the SSH restart fix and matches the primary change.
Description check Passed The description is complete and covers the user impact, mechanism, rationale, testing, cross-platform considerations, and checklist. The linked issue is identified as STA-10476, but it is not provided…
  • Fix all pre-merge checks with AI
✨ Finishing Touches: 💡 1
📝 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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments: 1
src/main/ssh/system-ssh-orphaned-forward.ts: 1

136-145: 🎯 Functional Correctness | 🔵 Trivial | 💤 Low value

Return false when the orphan does not exit.

stopOrphanedOrcaSshForward returns true even when the orphan survives both waits. It also returns true when kill throws EPERM. In both cases the caller retries the bind once and fails with EADDRINUSE anyway. The function comment says true means the orphan was signalled, so this is not a correctness failure. However, the caller's retry depends on that boolean. Use the result of waitUntilGone so that true means the port was actually freed.

Proposed fix
   try {
     deps.kill(orphan.pid, 'SIGTERM')
-    if (!(await waitUntilGone(orphan.pid, deps.kill, TERM_WAIT_MS))) {
-      deps.kill(orphan.pid, 'SIGKILL')
-      await waitUntilGone(orphan.pid, deps.kill, KILL_WAIT_MS)
-    }
-  } catch {
-    // Exited between the table read and the signal; the caller's retry decides.
+    if (await waitUntilGone(orphan.pid, deps.kill, TERM_WAIT_MS)) {
+      return true
+    }
+    deps.kill(orphan.pid, 'SIGKILL')
+    return await waitUntilGone(orphan.pid, deps.kill, KILL_WAIT_MS)
+  } catch {
+    // Exited between the table read and the signal, or the signal was refused.
+    return isGone(orphan.pid, deps.kill)
   }
-  return true
 }

ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: d66f0cbd-1c88-4207-a5be-b61850d87065
📥 Commits

Reviewing files that changed from the base of the PR and between b3ad227 and 72661531f8892fba1bd3b4445cf2629d9ae5c50b.

📒 Files selected for processing: 14
  • src/main/ipc/ssh-app-shutdown.test.ts
  • src/main/ipc/ssh-ipc-mock-shapes.ts
  • src/main/ipc/ssh-ipc-module-mocks.ts
  • src/main/ipc/ssh-ipc-test-harness.ts
  • src/main/ipc/ssh-shutdown-drain.ts
  • src/main/ssh/orcad-managed-tunnel-manager.ts
  • src/main/ssh/orcad-managed-tunnel.test.ts
  • src/main/ssh/orcad-managed-tunnel.ts
  • src/main/ssh/ssh-port-forward.test.ts
  • src/main/ssh/ssh-port-forward.ts
  • src/main/ssh/system-ssh-forward-process.test.ts
  • src/main/ssh/system-ssh-forward-process.ts
  • src/main/ssh/system-ssh-orphaned-forward.test.ts
  • src/main/ssh/system-ssh-orphaned-forward.ts

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

Comment on lines +187 to +189
this.managerGeneration += 1
this.active.clear()
await this.forwards.closeAll()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift

Prevent managed tunnels from restarting during shutdown.

If a joined build fails as superseded, ensure retries it with the new managerGeneration. That retry can open a forward after closeAll has taken its snapshot. A rebuild waiting on an in-flight build can also call ensure after shutdown. Mark the manager as shutting down and reject new builds, in addition to superseding existing ones.

@@ -191,6 +191,11 @@ export class SshPortForwardManager {
await Promise.all(stale.map((id) => this.removeForwardAsync(id)))
}

/** Stops every forward and waits until each released its port; quit uses it. */
async closeAll(): Promise<void> {
await Promise.all([...this.forwards.keys()].map((id) => this.removeForwardAsync(id)))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift

Include pending forward starts in closeAll.

If provider.start is still pending, addForwardWithId has not added that forward to this.forwards. closeAll can finish before the start resolves. The start then registers a live forward after shutdown, so its SSH process can outlive Orca. Track pending starts and close their forwards when they complete, or prevent registration after shutdown.

@OrcaWin
OrcaWin force-pushed the nwparker/ssh-forward-quit-orphans branch from 7266153 to 95c9898 Compare October 13, 2026 20:26
@OrcaWin
OrcaWin merged commit 335cc32 into main Oct 13, 2026
66 checks passed
registered2nd pushed a commit to registered2nd/orca that referenced this pull request Oct 14, 2026
…ying offline (STA-10476) (stablyai#28398)

Co-authored-by: m4air <[email protected]>
(cherry picked from commit 335cc32)
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