Skip to content

The agent that grows with you

fix(desktop): keep SSH forwards alive across mux idle

Niklas Kietaibl64b2ad794d60
diff --git a/apps/desktop/electron/ssh-connection.test.ts b/apps/desktop/electron/ssh-connection.test.ts
index 74e1853f05d..49b7d032142 100644
--- a/apps/desktop/electron/ssh-connection.test.ts
+++ b/apps/desktop/electron/ssh-connection.test.ts
@@ -6,7 +6,7 @@ import os from 'node:os'
import path from 'node:path'
import { promisify } from 'node:util'
-import { test } from 'vitest'
+import { test, vi } from 'vitest'
import {
baseSshOptions,
@@ -509,6 +509,37 @@ test('forward() issues -O forward with a loopback-bound -L spec', async () => {
assert.equal(args[0], '-O')
assert.equal(args[1], 'forward')
assert.ok(args.includes('127.0.0.1:5000:127.0.0.1:6000'))
+ await conn.cancelForward(5000, 6000)
+})
+
+test('mux forward keeps the ControlPersist master alive until the final forward is cancelled', async () => {
+ vi.useFakeTimers()
+
+ try {
+ const spawnFn = scriptedSpawn({ code: 0 })
+
+ const conn = new SshConnection(
+ { host: 'box', user: 'me' },
+ { spawnFn, controlDir: '/tmp/d', controlKeepaliveMs: 1_000 }
+ )
+
+ await conn.forward(5000, 6000)
+ await conn.forward(5001, 6001)
+ await vi.advanceTimersByTimeAsync(1_000)
+
+ const checks = () => spawnFn.calls.filter(args => args[0] === '-O' && args[1] === 'check').length
+ assert.equal(checks(), 1, 'a tracked forward refreshes the ControlPersist timer')
+
+ await conn.cancelForward(5000, 6000)
+ await vi.advanceTimersByTimeAsync(1_000)
+ assert.equal(checks(), 2, 'cancelling one of several forwards keeps the refresh active')
+
+ await conn.cancelForward(5001, 6001)
+ await vi.advanceTimersByTimeAsync(2_000)
+ assert.equal(checks(), 2, 'cancelling the final forward stops the refresh timer')
+ } finally {
+ vi.useRealTimers()
+ }
})
test('lifecycle logging passes through redaction', async () => {
diff --git a/apps/desktop/electron/ssh-connection.ts b/apps/desktop/electron/ssh-connection.ts
index c52ea0b01ec..935749ac0e1 100644
--- a/apps/desktop/electron/ssh-connection.ts
+++ b/apps/desktop/electron/ssh-connection.ts
@@ -58,6 +58,7 @@ const REMOTE_PROBE_TIMEOUT_SECS = 15
const DEFAULT_TUNNEL_RESTART_LIMIT = 5
const DEFAULT_TUNNEL_RESTART_DELAY_MS = 1_000
const CONTROL_PERSIST_SECONDS = 300
+const DEFAULT_CONTROL_KEEPALIVE_MS = 60_000
// eslint-disable-next-line no-control-regex -- deliberately reject control chars in ssh targets
const _CONTROL_CHAR_RE = /[\x00-\x1f\x7f]/
@@ -652,6 +653,9 @@ class SshConnection {
_mux: boolean
_tunnels: Map<string, any>
_controlMasters: ControlMasterHolders
+ _forwardedSpecs: Set<string>
+ _controlKeepaliveMs: number
+ _controlKeepaliveTimer: ReturnType<typeof setInterval> | null
constructor(cfg, opts: any = {}) {
if (!cfg || !cfg.host) {
@@ -684,6 +688,7 @@ class SshConnection {
: ''
this._tunnels = new Map()
this._controlMasters = opts.controlMasterHolders || sharedControlMasterHolders
+ this._forwardedSpecs = new Set()
this._spawnFn = opts.spawnFn || spawn
@@ -693,6 +698,8 @@ class SshConnection {
this._forwardTimeoutMs = opts.forwardTimeoutMs ?? DEFAULT_FORWARD_TIMEOUT_MS
this._tunnelRestartLimit = opts.tunnelRestartLimit ?? DEFAULT_TUNNEL_RESTART_LIMIT
this._tunnelRestartDelayMs = opts.tunnelRestartDelayMs ?? DEFAULT_TUNNEL_RESTART_DELAY_MS
+ this._controlKeepaliveMs = opts.controlKeepaliveMs ?? DEFAULT_CONTROL_KEEPALIVE_MS
+ this._controlKeepaliveTimer = null
this._opened = false
}
@@ -883,6 +890,30 @@ class SshConnection {
}
}
+ // `ssh -O forward` does not keep a ControlPersist master busy by itself.
+ // Refresh the mux while Desktop owns local forwards so the 300s idle timer
+ // cannot silently remove their listener sockets. The finite persist timeout
+ // still cleans up an orphaned master if Desktop crashes.
+ _startControlKeepalive() {
+ if (!this._mux || this._controlKeepaliveTimer || this._forwardedSpecs.size === 0) {
+ return
+ }
+
+ this._controlKeepaliveTimer = setInterval(() => {
+ void this.isAlive()
+ }, this._controlKeepaliveMs)
+ this._controlKeepaliveTimer.unref?.()
+ }
+
+ _stopControlKeepalive() {
+ if (!this._controlKeepaliveTimer) {
+ return
+ }
+
+ clearInterval(this._controlKeepaliveTimer)
+ this._controlKeepaliveTimer = null
+ }
+
// A real exec through the master (`exit 0` works under POSIX shells and
// cmd.exe); a wedged mux hangs to the timeout.
async _verifyMuxChannel({ signal }: any = {}) {
@@ -1139,6 +1170,9 @@ class SshConnection {
if (!sshCloseOk(result)) {
throw this._fail(result)
}
+
+ this._forwardedSpecs.add(spec)
+ this._startControlKeepalive()
}
// Cancel a previously-established forward. Best-effort: a failure here is
@@ -1173,6 +1207,12 @@ class SshConnection {
this._logLine(`cancelled forward 127.0.0.1:${localPort}`)
} catch (error: any) {
this._logLine(`cancelForward failed (ignored): ${error.message}`)
+ } finally {
+ this._forwardedSpecs.delete(spec)
+
+ if (this._forwardedSpecs.size === 0) {
+ this._stopControlKeepalive()
+ }
}
}
@@ -1181,6 +1221,8 @@ class SshConnection {
// this connection's claim (#97264). No-mux: kill the tunnel children.
// Best-effort; never throws.
async close() {
+ this._stopControlKeepalive()
+ this._forwardedSpecs.clear()
const action = this._mux ? this._controlMasters.release(this.controlPath, this) : 'exit-master'
if (!this._opened) {