Skip to content

Commit 5c07e20

Browse files
igorcostaAutohand Evolve
andcommitted
Make foreground shell cancellation deterministic in CI
Run piped POSIX shell commands in their own process group and signal the full group during graceful and forced termination so descendants cannot retain output pipes after cancellation. Extend the shell cancellation regression to cover a stubborn descendant and measure shutdown from the abort boundary. Co-authored-by: Autohand Evolve <code-noreply@autohand.ai>
1 parent 5fd4476 commit 5c07e20

2 files changed

Lines changed: 30 additions & 7 deletions

File tree

‎src/ui/shellCommand.ts‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { buildAutohandChildProcessEnv } from '../utils/childProcessEnv.js';
1919
*/
2020
const DEFAULT_SHELL_TIMEOUT = 30000;
2121
const DEFAULT_KILL_GRACE_PERIOD_MS = 1_000;
22+
const SUPPORTS_PROCESS_GROUP_SIGNALS = process.platform !== 'win32';
2223

2324
export class ShellCommandAbortedError extends Error {
2425
readonly output: string;
@@ -32,6 +33,23 @@ export class ShellCommandAbortedError extends Error {
3233
}
3334
}
3435

36+
function signalForegroundProcessGroup(
37+
child: ReturnType<typeof spawn>,
38+
signal: NodeJS.Signals
39+
): void {
40+
const pid = child.pid;
41+
if (!SUPPORTS_PROCESS_GROUP_SIGNALS || pid === undefined) {
42+
child.kill(signal);
43+
return;
44+
}
45+
46+
try {
47+
process.kill(-pid, signal);
48+
} catch (error) {
49+
if ((error as NodeJS.ErrnoException).code !== 'ESRCH') throw error;
50+
}
51+
}
52+
3553
const SHELL_HOT_TIP_SUGGESTIONS = [
3654
'git status',
3755
'ls -la',
@@ -510,6 +528,7 @@ export async function executeShellCommandAsync(
510528
child = spawn(trimmedCommand, {
511529
cwd: cwd ?? process.cwd(),
512530
shell: true,
531+
detached: SUPPORTS_PROCESS_GROUP_SIGNALS,
513532
stdio: ['ignore', 'pipe', 'pipe'],
514533
env: buildAutohandChildProcessEnv(),
515534
});
@@ -526,9 +545,9 @@ export async function executeShellCommandAsync(
526545
if (resolved || aborted || timedOut) return;
527546
aborted = reason === 'abort';
528547
timedOut = reason === 'timeout';
529-
child.kill('SIGTERM');
548+
signalForegroundProcessGroup(child, 'SIGTERM');
530549
forceKillId = setTimeout(() => {
531-
if (!resolved) child.kill('SIGKILL');
550+
if (!resolved) signalForegroundProcessGroup(child, 'SIGKILL');
532551
}, killGracePeriodMs);
533552
forceKillId.unref?.();
534553
};

‎tests/ui/shellCommand.test.ts‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,27 +220,31 @@ describe('executeStreamingShellCommand', () => {
220220
await waitForProcessExit(pid);
221221
});
222222

223-
it('forces a non-PTY foreground command to exit after its grace period', async () => {
223+
it('forces an entire non-PTY foreground process group to exit after its grace period', async () => {
224224
const markerPath = join(tmpdir(), `autohand-shell-force-pid-${Date.now()}`);
225225
const controller = new AbortController();
226-
const startedAt = Date.now();
227-
const script = [
228-
`require('node:fs').writeFileSync(${JSON.stringify(markerPath)}, String(process.pid))`,
226+
const stubbornChildScript = [
229227
"process.on('SIGTERM', () => {})",
228+
`require('node:fs').writeFileSync(${JSON.stringify(markerPath)}, String(process.pid))`,
230229
'setTimeout(() => process.exit(0), 800)',
231230
].join(';');
231+
const script = [
232+
`const child = require('node:child_process').spawn(process.execPath, ['-e', ${JSON.stringify(stubbornChildScript)}], { stdio: 'inherit' })`,
233+
'child.on(\'exit\', (code) => process.exit(code ?? 0))',
234+
].join(';');
232235
const commandPromise = executeStreamingShellCommand(
233236
`${process.execPath} -e ${JSON.stringify(script)}`,
234237
tmpdir(),
235238
{ preferPty: false, signal: controller.signal, killGracePeriodMs: 30 }
236239
);
237240
const pid = await waitForProcessId(markerPath);
238241

242+
const abortedAt = Date.now();
239243
controller.abort();
240244
const error = await commandPromise.catch((caught: unknown) => caught);
241245

242246
expect(error).toMatchObject({ name: 'AbortError' });
243-
expect(Date.now() - startedAt).toBeLessThan(500);
247+
expect(Date.now() - abortedAt).toBeLessThan(500);
244248
await waitForProcessExit(pid);
245249
});
246250

0 commit comments

Comments
 (0)