Skip to content

The agent that grows with you

refactor(desktop): tie mux keepalive to persist timeout

Niklas Kietaibl75634d5cc2aa
diff --git a/apps/desktop/electron/ssh-connection.test.ts b/apps/desktop/electron/ssh-connection.test.ts
index 49b7d032142..f0e33cae236 100644
--- a/apps/desktop/electron/ssh-connection.test.ts
+++ b/apps/desktop/electron/ssh-connection.test.ts
@@ -15,6 +15,8 @@ import {
buildInteractiveSshArgs,
buildMasterArgs,
classifySshError,
+ CONTROL_FORWARD_KEEPALIVE_MS,
+ CONTROL_PERSIST_SECONDS,
controlSocketPath,
createSshProbeConnection,
forwardSpec,
@@ -520,22 +522,23 @@ test('mux forward keeps the ControlPersist master alive until the final forward
const conn = new SshConnection(
{ host: 'box', user: 'me' },
- { spawnFn, controlDir: '/tmp/d', controlKeepaliveMs: 1_000 }
+ { spawnFn, controlDir: '/tmp/d' }
)
await conn.forward(5000, 6000)
await conn.forward(5001, 6001)
- await vi.advanceTimersByTimeAsync(1_000)
+ assert.ok(CONTROL_FORWARD_KEEPALIVE_MS < CONTROL_PERSIST_SECONDS * 1_000)
+ await vi.advanceTimersByTimeAsync(CONTROL_FORWARD_KEEPALIVE_MS)
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)
+ await vi.advanceTimersByTimeAsync(CONTROL_FORWARD_KEEPALIVE_MS)
assert.equal(checks(), 2, 'cancelling one of several forwards keeps the refresh active')
await conn.cancelForward(5001, 6001)
- await vi.advanceTimersByTimeAsync(2_000)
+ await vi.advanceTimersByTimeAsync(CONTROL_FORWARD_KEEPALIVE_MS * 2)
assert.equal(checks(), 2, 'cancelling the final forward stops the refresh timer')
} finally {
vi.useRealTimers()
diff --git a/apps/desktop/electron/ssh-connection.ts b/apps/desktop/electron/ssh-connection.ts
index 935749ac0e1..f2e3ab364d1 100644
--- a/apps/desktop/electron/ssh-connection.ts
+++ b/apps/desktop/electron/ssh-connection.ts
@@ -58,7 +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
+const CONTROL_FORWARD_KEEPALIVE_MS = Math.min(60_000, Math.floor((CONTROL_PERSIST_SECONDS * 1_000) / 2))
// eslint-disable-next-line no-control-regex -- deliberately reject control chars in ssh targets
const _CONTROL_CHAR_RE = /[\x00-\x1f\x7f]/
@@ -654,7 +654,6 @@ class SshConnection {
_tunnels: Map<string, any>
_controlMasters: ControlMasterHolders
_forwardedSpecs: Set<string>
- _controlKeepaliveMs: number
_controlKeepaliveTimer: ReturnType<typeof setInterval> | null
constructor(cfg, opts: any = {}) {
@@ -698,7 +697,6 @@ 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
}
@@ -900,8 +898,10 @@ class SshConnection {
}
this._controlKeepaliveTimer = setInterval(() => {
+ // Remote liveness owns reconnect/teardown. Keep refreshing through a
+ // transient failed check rather than turning one timeout into expiry.
void this.isAlive()
- }, this._controlKeepaliveMs)
+ }, CONTROL_FORWARD_KEEPALIVE_MS)
this._controlKeepaliveTimer.unref?.()
}
@@ -1324,6 +1324,7 @@ export {
buildInteractiveSshArgs,
buildMasterArgs,
classifySshError,
+ CONTROL_FORWARD_KEEPALIVE_MS,
CONTROL_PERSIST_SECONDS,
controlSocketPath,
createSshProbeConnection,