diff --git a/src/runner/client.test.ts b/src/runner/client.test.ts index 8ce464ac..6d7d8e66 100644 --- a/src/runner/client.test.ts +++ b/src/runner/client.test.ts @@ -65,3 +65,16 @@ describe('runConformanceTest spec-version applicability', () => { expect(result.clientOutput?.stdout).toContain(LATEST_SPEC_VERSION); }, 30000); }); + +describe('runConformanceTest client timeout', () => { + test('stops a hung client started through a shell command list', async () => { + // The trailing `; true` stops the shell from exec-ing node directly, so + // the client is a grandchild of the spawned shell, as with `cd dir && ...` + // or `npx tsx client.ts`. + const hungClient = 'node -e "setInterval(() => {}, 1000)"; true'; + const started = Date.now(); + const result = await runConformanceTest(hungClient, 'initialize', 1000); + expect(result.clientOutput?.timedOut).toBe(true); + expect(Date.now() - started).toBeLessThan(10000); + }, 20000); +}); diff --git a/src/runner/client.ts b/src/runner/client.ts index af10657f..9b3755dc 100644 --- a/src/runner/client.ts +++ b/src/runner/client.ts @@ -1,4 +1,4 @@ -import { spawn } from 'child_process'; +import { spawn, type ChildProcess } from 'child_process'; import { promises as fs } from 'fs'; import path from 'path'; import { @@ -23,6 +23,43 @@ export interface ClientExecutionResult { timedOut: boolean; } +// Clients run in their own process group (see executeClient), so they no +// longer receive Ctrl+C with the runner. Track them and stop them on exit. +const liveClients = new Set<() => void>(); +let exitHandlersInstalled = false; + +function trackClient(kill: () => void): () => void { + if (!exitHandlersInstalled) { + exitHandlersInstalled = true; + const killAll = () => liveClients.forEach((k) => k()); + process.on('exit', killAll); + const exitCodes = { SIGINT: 130, SIGTERM: 143, SIGHUP: 129 } as const; + for (const signal of ['SIGINT', 'SIGTERM', 'SIGHUP'] as const) { + process.on(signal, () => { + killAll(); + process.exit(exitCodes[signal]); + }); + } + } + liveClients.add(kill); + return () => liveClients.delete(kill); +} + +function killProcessTree(child: ChildProcess, useProcessGroup: boolean): void { + if (child.pid === undefined) return; + try { + if (useProcessGroup) { + process.kill(-child.pid, 'SIGKILL'); + } else { + spawn('taskkill', ['/pid', String(child.pid), '/T', '/F'], { + stdio: 'ignore' + }).on('error', () => child.kill()); + } + } catch { + child.kill('SIGKILL'); + } +} + async function executeClient( command: string, scenarioName: string, @@ -56,15 +93,30 @@ async function executeClient( } return new Promise((resolve) => { + // Run the client in its own process group so a timeout can stop the whole + // tree. With `shell: true` the child is `sh -c ""`, and `kill()` + // only reaches that shell; the client it started would keep the output + // pipes open and `close` would never fire. + const useProcessGroup = process.platform !== 'win32'; const childProcess = spawn(executable, args, { shell: true, stdio: 'pipe', - env + env, + detached: useProcessGroup }); + const kill = () => killProcessTree(childProcess, useProcessGroup); + const untrack = trackClient(kill); + const timeoutHandle = setTimeout(() => { timedOut = true; - childProcess.kill(); + kill(); + // A descendant that left the process group can keep the pipes open; + // drop them so `close` still fires. + setTimeout(() => { + childProcess.stdout?.destroy(); + childProcess.stderr?.destroy(); + }, 2000).unref(); }, timeout); if (childProcess.stdout) { @@ -81,6 +133,7 @@ async function executeClient( childProcess.on('close', (code) => { clearTimeout(timeoutHandle); + untrack(); resolve({ exitCode: code || 0, stdout, @@ -91,6 +144,7 @@ async function executeClient( childProcess.on('error', (error) => { clearTimeout(timeoutHandle); + untrack(); resolve({ exitCode: -1, stdout,