Skip to content

The agent that grows with you

fix(desktop): a timed-out gateway client that exits on SIGTERM resolves exit 7 again

Teknium4e3f5bbc5cb2
diff --git a/apps/desktop/electron/local-gateway-deadline.test.ts b/apps/desktop/electron/local-gateway-deadline.test.ts
index 32f481c3bc6..a3ce82dfa6a 100644
--- a/apps/desktop/electron/local-gateway-deadline.test.ts
+++ b/apps/desktop/electron/local-gateway-deadline.test.ts
@@ -21,3 +21,45 @@ test('an ensure client that ignores termination is killed and its promise reject
await rejected
expect(child.kill.mock.calls).toEqual([[], ['SIGKILL']])
})
+
+// restartLocalGatewayOwner (main.ts) logs a non-zero restart exit and lets the re-ensure report
+// what answers. A restart client that times out but exits on SIGTERM must keep that contract:
+// resolve exit 7 naming the deadline, then re-ensure and attach — not throw past the restart.
+test('a stale-owner restart client that times out but obeys SIGTERM is logged and the re-ensure attaches', async () => {
+ vi.useFakeTimers()
+
+ const child = Object.assign(new EventEmitter(), {
+ stdout: new EventEmitter(), stderr: new EventEmitter(),
+ kill: vi.fn((signal?: string) => {
+ if (!signal) { queueMicrotask(() => child.emit('close', null)) }
+
+ return true
+ })
+ })
+
+ vi.doMock('node:child_process', () => ({ spawn: () => child }))
+ const { createStaleGatewayRestarter, ensureLocalGateway, runGatewayEnsure } = await import('./local-gateway')
+ const logs: string[] = []
+
+ const restartOwner = async (owner: string) => {
+ const result = await runGatewayEnsure({ command: 'owned-client', args: [], env: {}, shell: false }, '.', owner, {}, { timeoutMs: 10, label: 'hermes gateway restart' })
+ const reason = `${result.stdout}\n${result.stderr}`.trim().split(/\r?\n/).filter(Boolean).pop()
+ logs.push(`gateway restart exited ${result.code}${reason ? `: ${reason}` : ''}`)
+ }
+
+ const endpoint = { profile_id: '/h', instance_id: 'old', authority_epoch: 1, runtime_protocol: 1, api_origin: 'http://127.0.0.1:1234', capabilities: ['session-authority-v1'], supervisor: 'none' }
+ const answers = [{ ...endpoint, code_sha: 'old' }, { ...endpoint, instance_id: 'new', code_sha: 'new' }]
+ let ensures = 0
+
+ const attached = ensureLocalGateway(async () => {
+ ensures += 1
+
+ return { code: 0, stdout: JSON.stringify({ state: 'ready', endpoint: answers.shift(), client_code_sha: 'new' }) }
+ }, undefined, createStaleGatewayRestarter(restartOwner, () => undefined))
+
+ await vi.advanceTimersByTimeAsync(10)
+ await expect(attached).resolves.toMatchObject({ gatewayEndpoint: { instance_id: 'new' } })
+ expect(child.kill.mock.calls).toEqual([[]])
+ expect(ensures).toBe(2)
+ expect(logs).toEqual(['gateway restart exited 7: hermes gateway restart timed out'])
+})
diff --git a/apps/desktop/electron/local-gateway.ts b/apps/desktop/electron/local-gateway.ts
index 9ab512dafa7..7adf3845f61 100644
--- a/apps/desktop/electron/local-gateway.ts
+++ b/apps/desktop/electron/local-gateway.ts
@@ -33,11 +33,14 @@ export function runGatewayEnsure(
// profile or an import crash explain themselves here while stdout stays empty.
child.stderr.on('data', data => { stderr = (stderr + data.toString()).slice(-4096) })
child.on('error', () => { clearTimeout(timer); clearTimeout(forceTimer); reject(new Error(`Could not run ${label}`)) })
+ // A client that exits on SIGTERM still resolves (callers such as restartLocalGatewayOwner log a
+ // non-zero exit and re-ensure); only one that outlives the force-kill deadline rejects. An
+ // expired run never reads as success, and its last diagnostic line names the deadline.
child.on('close', code => {
clearTimeout(timer)
clearTimeout(forceTimer)
- if (expired) { reject(expired) }
+ if (expired) { resolve({ code: code || 7, stdout, stderr: `${stderr}\n${expired.message}`.trim() }) }
else { resolve({ code: code ?? 7, stdout, stderr }) }
})
})