Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/runner/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
60 changes: 57 additions & 3 deletions src/runner/client.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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,
Expand Down Expand Up @@ -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 "<command>"`, 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) {
Expand All @@ -81,6 +133,7 @@ async function executeClient(

childProcess.on('close', (code) => {
clearTimeout(timeoutHandle);
untrack();
resolve({
exitCode: code || 0,
stdout,
Expand All @@ -91,6 +144,7 @@ async function executeClient(

childProcess.on('error', (error) => {
clearTimeout(timeoutHandle);
untrack();
resolve({
exitCode: -1,
stdout,
Expand Down