fix: clean up production daemon shutdown listener (#3765)

This commit is contained in:
Neil
2026-05-30 09:22:01 -07:00
committed by GitHub
parent 824f1ec483
commit 6368562230
2 changed files with 70 additions and 7 deletions
@@ -125,6 +125,57 @@ describe('createProductionLauncher', () => {
expect(child.disconnect).toHaveBeenCalled()
expect(child.unref).toHaveBeenCalled()
})
it('removes shutdown exit listener when force-kill timeout settles first', async () => {
vi.useFakeTimers()
try {
const handlers: Record<string, ((arg?: unknown) => void)[]> = {
message: [],
error: [],
exit: []
}
const child = {
pid: 12345,
killed: false,
on: vi.fn((event: string, cb: (arg?: unknown) => void) => {
handlers[event]?.push(cb)
return child
}),
once: vi.fn((event: string, cb: (arg?: unknown) => void) => {
handlers[event]?.push(cb)
return child
}),
off: vi.fn((event: string, cb: (arg?: unknown) => void) => {
handlers[event] = handlers[event]?.filter((handler) => handler !== cb) ?? []
return child
}),
kill: vi.fn(),
disconnect: vi.fn(),
unref: vi.fn()
}
forkMock.mockReturnValueOnce(child)
const launcher = createProductionLauncher({
getDaemonEntryPath: () => join(dir, 'daemon-entry.js')
})
const launch = launcher(socketPathFor(dir), tokenPathFor(dir))
handlers.message[0]?.({ type: 'ready' })
const handle = await launch
const shutdown = handle.shutdown()
expect(handlers.exit).toHaveLength(1)
await vi.advanceTimersByTimeAsync(5000)
await shutdown
expect(child.kill).toHaveBeenNthCalledWith(1, 'SIGTERM')
expect(child.kill).toHaveBeenNthCalledWith(2, 'SIGKILL')
expect(handlers.exit).toHaveLength(0)
} finally {
vi.useRealTimers()
}
})
})
function socketPathFor(dir: string): string {
+19 -7
View File
@@ -89,16 +89,28 @@ function shutdownChild(child: ChildProcess): Promise<void> {
return
}
const timeout = setTimeout(() => {
child.kill('SIGKILL')
let settled = false
let timeout: ReturnType<typeof setTimeout>
function finish(): void {
if (settled) {
return
}
settled = true
clearTimeout(timeout)
child.off('exit', onExit)
resolve()
}
function onExit(): void {
finish()
}
timeout = setTimeout(() => {
child.kill('SIGKILL')
finish()
}, 5000)
child.once('exit', () => {
clearTimeout(timeout)
resolve()
})
child.once('exit', onExit)
child.kill('SIGTERM')
})
}