Skip to content

The agent that grows with you

fix(desktop): bound unresponsive gateway ensure processes

David Dudok de Wit38238c8fa21a
diff --git a/apps/desktop/electron/local-gateway-deadline.test.ts b/apps/desktop/electron/local-gateway-deadline.test.ts
new file mode 100644
index 00000000000..32f481c3bc6
--- /dev/null
+++ b/apps/desktop/electron/local-gateway-deadline.test.ts
@@ -0,0 +1,23 @@
+import { EventEmitter } from 'node:events'
+
+import { afterEach, expect, test, vi } from 'vitest'
+
+afterEach(() => { vi.useRealTimers(); vi.doUnmock('node:child_process'); vi.resetModules() })
+
+test('an ensure client that ignores termination is killed and its promise rejects within a second deadline', async () => {
+ vi.useFakeTimers()
+
+ const child = Object.assign(new EventEmitter(), {
+ stdout: new EventEmitter(), stderr: new EventEmitter(), kill: vi.fn(() => true)
+ })
+
+ vi.doMock('node:child_process', () => ({ spawn: () => child }))
+ const { runGatewayEnsure } = await import('./local-gateway')
+ const result = runGatewayEnsure({ command: 'owned-client', args: [], env: {}, shell: false }, '.', 'profile', {}, { timeoutMs: 10 })
+ const rejected = expect(result).rejects.toThrow('timed out')
+ await vi.advanceTimersByTimeAsync(10)
+ expect(child.kill.mock.calls).toEqual([[]])
+ await vi.advanceTimersByTimeAsync(1000)
+ await rejected
+ expect(child.kill.mock.calls).toEqual([[], ['SIGKILL']])
+})
diff --git a/apps/desktop/electron/local-gateway.ts b/apps/desktop/electron/local-gateway.ts
index 9683a327390..a72f02fa84d 100644
--- a/apps/desktop/electron/local-gateway.ts
+++ b/apps/desktop/electron/local-gateway.ts
@@ -14,16 +14,32 @@ export function runGatewayEnsure(
const child = spawn(windowsShellCommand(backend.command, Boolean(backend.shell)), backend.args, hiddenWindowsChildOptions({ cwd, env: { ...parentEnv, HERMES_HOME: home, ...backend.env }, shell: backend.shell, stdio: ['ignore', 'pipe', 'pipe'] }))
let stdout = ''
let stderr = ''
+ let expired: Error | undefined
+ let forceTimer: ReturnType<typeof setTimeout> | undefined
+
+ const expire = (message: string) => {
+ if (expired) { return }
+ expired = new Error(message)
+ child.kill()
+ forceTimer = setTimeout(() => { child.kill('SIGKILL'); reject(expired) }, 1000)
+ }
+
// Only the bounded CLI client is ours. Never retain/kill its detached owner.
- const timer = setTimeout(() => child.kill(), timeoutMs)
+ const timer = setTimeout(() => expire(`${label} timed out`), timeoutMs)
child.stdout.on('data', data => { stdout += data.toString();
- if (stdout.length > 65536) {child.kill()} })
+ if (stdout.length > 65536) { stdout = stdout.slice(0, 65536); expire(`${label} output exceeded limit`) } })
// Diagnostics only (never protocol): an older `hermes` without the subcommand, a missing
// 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); reject(new Error(`Could not run ${label}`)) })
- child.on('close', code => { clearTimeout(timer); resolve({ code: code ?? 7, stdout, stderr }) })
+ child.on('error', () => { clearTimeout(timer); clearTimeout(forceTimer); reject(new Error(`Could not run ${label}`)) })
+ child.on('close', code => {
+ clearTimeout(timer)
+ clearTimeout(forceTimer)
+
+ if (expired) { reject(expired) }
+ else { resolve({ code: code ?? 7, stdout, stderr }) }
+ })
})
}