Skip to content

Commit b32a330

Browse files
mchwangclaude
andcommitted
Escalate to SIGKILL when gh ignores SIGTERM, so a timeout or abort always settles
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent fc5887b commit b32a330

3 files changed

Lines changed: 18 additions & 4 deletions

File tree

‎docs/implementation/pull-request-opening.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ Each opening or refresh owns the task state version at the moment it passed step
6969

7070
The adapter refuses any answer for a PR that is not open. A PR closed between the lookup and the update is never recorded as the task's review PR.
7171

72-
**Transport.** The PR title and description go to `gh api --input -` as a JSON body on stdin (`github/run-with-input.ts`), never as arguments: Linux limits one argument to 128 KiB, and a 60,000-character description of multibyte text is larger. The runner settles only after `gh` has exited, including on a timeout or abort. The already-fixed check also waits for both of its reads to settle before it returns.
72+
**Transport.** The PR title and description go to `gh api --input -` as a JSON body on stdin (`github/run-with-input.ts`), never as arguments: Linux limits one argument to 128 KiB, and a 60,000-character description of multibyte text is larger. The runner settles only after `gh` has exited, including on a timeout or abort: it sends SIGTERM, then SIGKILL after 5 seconds if `gh` is still running. The already-fixed check also waits for both of its reads to settle before it returns.
7373

7474
**Environment.** Each `gh` process gets only an allowlist of variables: the path, home and locale; GitHub tokens, host and configuration directories; the D-Bus session bus that Linux keyring sign-in uses; and proxy and CA settings (`github/gh-env.ts`). Prompts, the pager and update checks are turned off.
7575

‎github/run-with-input.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,23 @@ import { spawn } from 'node:child_process';
33
/**
44
* Runs a command with literal argv and writes `input` to its stdin, so large text never travels as an OS argument
55
* (Linux limits one argument to 128 KiB, and a NUL cannot be passed at all). Settles only after the process closed:
6-
* a timeout, an abort or an output past `maxBuffer` kills it, and the promise rejects once it has exited.
6+
* a timeout, an abort or an output past `maxBuffer` sends SIGTERM, then SIGKILL after `killGraceMs` if the process is
7+
* still running, and the promise rejects once it has exited.
78
*/
89
export function runWithInput(command: string, args: readonly string[], options: {
9-
input?: string; signal?: AbortSignal; env?: NodeJS.ProcessEnv; timeout?: number; maxBuffer?: number;
10+
input?: string; signal?: AbortSignal; env?: NodeJS.ProcessEnv; timeout?: number; maxBuffer?: number; killGraceMs?: number;
1011
}): Promise<string> {
1112
return new Promise((resolve, reject) => {
1213
if (options.signal?.aborted) { reject(options.signal.reason ?? new Error('Aborted.')); return; }
1314
const child = spawn(command, [...args], { env: options.env, stdio: ['pipe', 'pipe', 'pipe'] });
1415
const out: Buffer[] = [], err: Buffer[] = [];
1516
let size = 0, failure: Error | null = null;
16-
const stop = (error: Error) => { failure ??= error; child.kill('SIGTERM'); };
17+
let escalation: NodeJS.Timeout | null = null;
18+
const stop = (error: Error) => {
19+
failure ??= error;
20+
child.kill('SIGTERM');
21+
escalation ??= setTimeout(() => child.kill('SIGKILL'), options.killGraceMs ?? 5_000);
22+
};
1723
const timer = options.timeout ? setTimeout(() => stop(new Error(`${command} timed out.`)), options.timeout) : null;
1824
const onAbort = () => stop(options.signal?.reason instanceof Error ? options.signal.reason : new Error('Aborted.'));
1925
options.signal?.addEventListener('abort', onAbort, { once: true });
@@ -29,6 +35,7 @@ export function runWithInput(command: string, args: readonly string[], options:
2935
child.on('error', error => stop(error));
3036
child.on('close', (code, signal) => {
3137
if (timer) clearTimeout(timer);
38+
if (escalation) clearTimeout(escalation);
3239
options.signal?.removeEventListener('abort', onAbort);
3340
const stderr = Buffer.concat(err).toString('utf8').trim();
3441
if (failure) reject(failure);

‎test/publish.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,13 @@ describe('running gh with a request body on stdin', () => {
462462
await expect(started).rejects.toThrow('stop');
463463
expect(Date.now() - aborted).toBeGreaterThanOrEqual(100);
464464
});
465+
it('kills a process that ignores SIGTERM after the grace period, and still settles', async () => {
466+
const controller = new AbortController();
467+
const started = runWithInput(process.execPath, ['-e', 'process.on("SIGTERM",()=>{});setInterval(()=>{},1000)'], { signal: controller.signal, killGraceMs: 100 });
468+
await new Promise(resolve => setTimeout(resolve, 200));
469+
controller.abort(new Error('stop'));
470+
await expect(started).rejects.toThrow('stop');
471+
});
465472
it('reports a failing exit with its stderr', async () => {
466473
await expect(runWithInput(process.execPath, ['-e', 'console.error("HTTP 422");process.exit(1)'], {})).rejects.toThrow(/exit 1\): HTTP 422/);
467474
});

0 commit comments

Comments
 (0)