Skip to content

The agent that grows with you

fix(desktop): do not classify a signal-killed ssh child as unreachable

brooklyn!09f4adc705f9
diff --git a/apps/desktop/electron/ssh-connection.test.ts b/apps/desktop/electron/ssh-connection.test.ts
index 3a05901045e..9decf5785d2 100644
--- a/apps/desktop/electron/ssh-connection.test.ts
+++ b/apps/desktop/electron/ssh-connection.test.ts
@@ -210,7 +210,8 @@ test('classifySshError detects unreachable', () => {
})
// A fake child process that emits a scripted result on next tick.
-function fakeChild({ code = 0, stdout = '', stderr = '', errorEvent = null, hang = false }: any = {}) {
+// Node's `close` is `(code, signal)`: a signal death has `code === null`.
+function fakeChild({ code = 0, signal = null, stdout = '', stderr = '', errorEvent = null, hang = false }: any = {}) {
const child: any = new EventEmitter()
child.stdout = new EventEmitter()
child.stderr = new EventEmitter()
@@ -238,12 +239,21 @@ function fakeChild({ code = 0, stdout = '', stderr = '', errorEvent = null, hang
child.stderr.emit('data', Buffer.from(stderr))
}
- child.emit('close', code)
+ child.emit('close', signal ? null : code, signal)
})
return child
}
+function assertSignalDeathNotUnreachable(err, signal) {
+ assert.notEqual(err.kind, SSH_ERROR.UNREACHABLE)
+ assert.equal(err.signal, signal)
+ assert.doesNotMatch(err.message, /Could not reach/)
+ assert.match(err.message, new RegExp(signal))
+
+ return true
+}
+
// Build a spawnFn that returns scripted children per ssh invocation, recording
// the args it was called with.
function scriptedSpawn(scripts) {
@@ -515,6 +525,110 @@ test('no-mux: open() classifies auth failure', async () => {
await assert.rejects(conn.open(), (err: any) => err.kind === 'auth-failed')
})
+test('runSsh keeps Node close signal on the result', async () => {
+ const spawnFn = () => fakeChild({ signal: 'SIGTERM', stderr: '' })
+ const result: any = await runSsh(['box'], { timeoutMs: 5000, spawnFn })
+
+ assert.equal(result.code, null)
+ assert.equal(result.signal, 'SIGTERM')
+ assert.equal(result.stderr, '')
+})
+
+test('no-mux open() does not classify a signal death with empty stderr as unreachable', async () => {
+ const spawnFn = scriptedSpawn([{ signal: 'SIGTERM', stderr: '' }])
+ const conn = new SshConnection({ host: 'box', user: 'me' }, { spawnFn, mux: false })
+
+ await assert.rejects(() => conn.open(), (err: any) => assertSignalDeathNotUnreachable(err, 'SIGTERM'))
+})
+
+test('mux open() does not classify a signal-killed master with empty stderr as unreachable', async () => {
+ const spawnFn = scriptedSpawn(args => {
+ if (args.includes('check')) {
+ return { code: 255, stderr: 'no control path' }
+ }
+
+ return { signal: 'SIGHUP', stderr: '' }
+ })
+ const conn = new SshConnection({ host: 'box', user: 'me' }, { spawnFn, controlDir: '/tmp/d' })
+
+ await assert.rejects(() => conn.open(), (err: any) => assertSignalDeathNotUnreachable(err, 'SIGHUP'))
+})
+
+test('exec() does not classify a signal death with empty stderr as unreachable', async () => {
+ const spawnFn = scriptedSpawn([{ signal: 'SIGKILL', stderr: '' }])
+ const conn = new SshConnection({ host: 'box', user: 'me' }, { spawnFn, controlDir: '/tmp/d' })
+
+ await assert.rejects(() => conn.exec('uname -s'), (err: any) => assertSignalDeathNotUnreachable(err, 'SIGKILL'))
+})
+
+test('forward() does not classify a signal death with empty stderr as unreachable', async () => {
+ const spawnFn = scriptedSpawn([{ signal: 'SIGPIPE', stderr: '' }])
+ const conn = new SshConnection({ host: 'box', user: 'me' }, { spawnFn, controlDir: '/tmp/d' })
+
+ await assert.rejects(() => conn.forward(5000, 6000), (err: any) => assertSignalDeathNotUnreachable(err, 'SIGPIPE'))
+})
+
+test('close() does not report a signal-killed -O exit with empty stderr as unreachable', async () => {
+ const logs: string[] = []
+ const spawnFn = scriptedSpawn(args => {
+ if (args.includes('check')) {
+ return { code: 255 }
+ }
+
+ if (args.includes('-M')) {
+ return { code: 0 }
+ }
+
+ return { signal: 'SIGINT', stderr: '' }
+ })
+ const conn = new SshConnection(
+ { host: 'box', user: 'me' },
+ { spawnFn, controlDir: '/tmp/d', rememberLog: line => logs.push(line) }
+ )
+
+ await conn.open()
+ await conn.close()
+
+ const logged = logs.join('\n')
+ assert.match(logged, /SIGINT/)
+ assert.doesNotMatch(logged, /Could not reach/)
+})
+
+test('no-mux open() still classifies a normal non-zero exit with empty stderr as unreachable', async () => {
+ const spawnFn = scriptedSpawn([{ code: 255, stderr: '' }])
+ const conn = new SshConnection({ host: 'box', user: 'me' }, { spawnFn, mux: false })
+
+ await assert.rejects(
+ () => conn.open(),
+ (err: any) => {
+ assert.equal(err.kind, SSH_ERROR.UNREACHABLE)
+ assert.equal(err.signal, undefined)
+
+ return true
+ }
+ )
+})
+
+test('a signal death that already printed an unreachable ssh error stays unreachable', async () => {
+ const spawnFn = scriptedSpawn([
+ {
+ signal: 'SIGTERM',
+ stderr: 'ssh: connect to host box port 22: Connection refused'
+ }
+ ])
+ const conn = new SshConnection({ host: 'box', user: 'me' }, { spawnFn, mux: false })
+
+ await assert.rejects(
+ () => conn.open(),
+ (err: any) => {
+ assert.equal(err.kind, SSH_ERROR.UNREACHABLE)
+ assert.equal(err.signal, 'SIGTERM')
+
+ return true
+ }
+ )
+})
+
test('no-mux: forward spawns a persistent -N -L child; cancel + close kill it', async () => {
// Real listener stands in for the tunnel's local end so waitForLocalPort sees it.
const net = await import('node:net')
diff --git a/apps/desktop/electron/ssh-connection.ts b/apps/desktop/electron/ssh-connection.ts
index 09880e7bf18..fd817707a75 100644
--- a/apps/desktop/electron/ssh-connection.ts
+++ b/apps/desktop/electron/ssh-connection.ts
@@ -443,8 +443,9 @@ function sshErrorMessage(kind, conn, stderr?) {
// Spawn helper — runs an ssh invocation, races it against a hard timeout
-// Resolves { code, stdout, stderr }. On timeout the child is SIGKILLed and the
-// promise rejects with err.kind = TIMEOUT. `spawnFn` is injectable for tests.
+// Resolves { code, signal, stdout, stderr }. `signal` is Node's close signal
+// (null on a normal exit). On timeout the child is SIGKILLed and the promise
+// rejects with err.kind = TIMEOUT. `spawnFn` is injectable for tests.
function runSsh(args, { timeoutMs, spawnFn = spawn, stdin = 'ignore', stdinData, signal }: any = {}) {
return new Promise((resolve, reject) => {
if (signal?.aborted) {
@@ -534,7 +535,7 @@ function runSsh(args, { timeoutMs, spawnFn = spawn, stdin = 'ignore', stdinData,
signal?.removeEventListener('abort', onAbort)
reject(error)
})
- child.on('close', code => {
+ child.on('close', (code, closeSignal) => {
if (settled) {
return
}
@@ -542,11 +543,37 @@ function runSsh(args, { timeoutMs, spawnFn = spawn, stdin = 'ignore', stdinData,
settled = true
clearTimeout(timer)
signal?.removeEventListener('abort', onAbort)
- resolve({ code, stdout, stderr })
+ resolve({ code, signal: closeSignal || null, stdout, stderr })
})
})
}
+function sshCloseSignal(value) {
+ if (!value || typeof value === 'string') {
+ return null
+ }
+
+ return typeof value.signal === 'string' && value.signal ? value.signal : null
+}
+
+function sshCloseStderr(value) {
+ if (typeof value === 'string') {
+ return value
+ }
+
+ if (value && typeof value.stderr === 'string') {
+ return value.stderr
+ }
+
+ return value?.message || ''
+}
+
+// A normal exit is code 0 and no close signal. A signal death is not success,
+// even when the caller would otherwise treat a null code as a plain failure.
+function sshCloseOk(result) {
+ return Boolean(result) && !sshCloseSignal(result) && result.code === 0
+}
+
function stopTunnelChild(child, timeoutMs = 5_000) {
if (!child || child.exitCode != null || child.signalCode != null) {
return Promise.resolve()
@@ -661,11 +688,29 @@ class SshConnection {
return err
}
- const stderr = typeof stderrOrErr === 'string' ? stderrOrErr : stderrOrErr?.message || ''
+ const closeSignal = sshCloseSignal(stderrOrErr)
+ const stderr = sshCloseStderr(stderrOrErr)
+
+ // A signal death with empty stderr is a local process death, not proof the
+ // host was unreachable. Callers that pass UNREACHABLE as the empty-stderr
+ // fallback must not win here.
+ if (closeSignal && !String(stderr).trim()) {
+ const detail = `ssh process exited from signal ${closeSignal}`
+ const err: any = new Error(sshErrorMessage(SSH_ERROR.UNKNOWN, this, detail))
+ err.kind = SSH_ERROR.UNKNOWN
+ err.signal = closeSignal
+
+ return err
+ }
+
const kind = stderr ? classifySshError(stderr) : fallbackKind
const err: any = new Error(sshErrorMessage(kind, this, stderr))
err.kind = kind
+ if (closeSignal) {
+ err.signal = closeSignal
+ }
+
return err
}
@@ -702,8 +747,8 @@ class SshConnection {
throw this._fail(error, SSH_ERROR.UNREACHABLE)
}
- if (result.code !== 0) {
- throw this._fail(result.stderr, SSH_ERROR.UNREACHABLE)
+ if (!sshCloseOk(result)) {
+ throw this._fail(result, SSH_ERROR.UNREACHABLE)
}
this._opened = true
@@ -750,8 +795,8 @@ class SshConnection {
throw this._fail(error, SSH_ERROR.UNREACHABLE)
}
- if (result.code !== 0) {
- throw this._fail(result.stderr, SSH_ERROR.UNREACHABLE)
+ if (!sshCloseOk(result)) {
+ throw this._fail(result, SSH_ERROR.UNREACHABLE)
}
this._opened = true
@@ -772,7 +817,7 @@ class SshConnection {
try {
const result: any = await runSsh(args, { timeoutMs: this._connectTimeoutMs, spawnFn: this._spawnFn, signal })
- return result.code === 0
+ return sshCloseOk(result)
} catch (error: any) {
if (error?.kind === 'superseded') {
throw error
@@ -792,7 +837,7 @@ class SshConnection {
signal
})
- return result.code === 0
+ return sshCloseOk(result)
} catch (error: any) {
if (error?.kind === 'superseded') {
throw error
@@ -841,8 +886,8 @@ class SshConnection {
throw this._fail(error)
}
- if (result.code !== 0) {
- throw this._fail(result.stderr)
+ if (!sshCloseOk(result)) {
+ throw this._fail(result)
}
return result.stdout
@@ -1035,8 +1080,8 @@ class SshConnection {
throw this._fail(error)
}
- if (result.code !== 0) {
- throw this._fail(result.stderr)
+ if (!sshCloseOk(result)) {
+ throw this._fail(result)
}
}
@@ -1107,8 +1152,8 @@ class SshConnection {
try {
const result: any = await runSsh(args, { timeoutMs: this._connectTimeoutMs, spawnFn: this._spawnFn })
- if (result.code !== 0) {
- throw this._fail(result.stderr)
+ if (!sshCloseOk(result)) {
+ throw this._fail(result)
}
this._logLine('control master closed')