Abort kills the command on the worker (#8 follow-up)

- worker: commands run with spawn in their own process group (same
  timeout, 10 MB output limit and result fields as exec). A new
  cancel_command message (by command_id, or every command of an
  execution) sends SIGTERM to the group, then SIGKILL after 5 s; the
  result reports cancelled: true. Old workers ignore the message.
- server: the abort route sends cancel_command to the worker running each
  of the execution's commands.
- Tests: the whole process tree dies on cancel (a background child too),
  execution-wide cancel leaves other executions alone, and end to end an
  aborted quick command's process is gone from the worker. Liveness
  checks treat zombies as dead (an orphan waits to be reaped).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
jaredandClaude Opus 5.5 committed 2026-11-22 23:59:50 -05:00
1 parent dc732a1ec5
commit 073114c92e
4 files changed
+164 -46

No files matched your search

+46
View File
@@ -113,3 +113,49 @@ describe('detectVersion (#40)', () => {
expect(detectVersion(path.join(__dirname, '..', 'worker'))).toMatch(/^[0-9a-f]{40}$/);
});
});
describe('cancel_command (#8)', () => {
const fs = require('fs');
const os = require('os');
const path = require('path');
// Running, not merely a zombie waiting to be reaped (kill(pid, 0) succeeds on zombies).
const alive = pid => {
try { return !/^\S+ \(.*\) Z/.test(fs.readFileSync(`/proc/${pid}/stat`, 'utf8')); } catch { return false; }
};
test('cancelling stops the command and everything it started', async () => {
const w = makeWorker();
const pidFile = path.join(fs.mkdtempSync(path.join(os.tmpdir(), 'pulse-cancel-')), 'pid');
const pending = w.executeCommand({ command: `sleep 30 & echo $! > ${pidFile}; wait`, execution_id: 'ex-c', command_id: 'cmd-c' });
for (let i = 0; i < 50 && !fs.existsSync(pidFile); i++) await new Promise(r => setTimeout(r, 50));
const childPid = Number(fs.readFileSync(pidFile, 'utf8'));
expect(alive(childPid)).toBe(true);
w.cancelCommand({ execution_id: 'ex-c', command_id: 'cmd-c' });
await pending;
expect(w.results[0]).toMatchObject({ success: false, cancelled: true, error: 'Cancelled', command_id: 'cmd-c' });
for (let i = 0; i < 40 && alive(childPid); i++) await new Promise(r => setTimeout(r, 50));
expect(alive(childPid)).toBe(false); // the background child is gone too
expect(w.running.size).toBe(0);
expect(w.activeTasks).toBe(0);
});
test('cancel by execution stops all of its commands, and only those', async () => {
const w = makeWorker();
const a = w.executeCommand({ command: 'sleep 30', execution_id: 'ex-1', command_id: 'a' });
const b = w.executeCommand({ command: 'sleep 30', execution_id: 'ex-1', command_id: 'b' });
const other = w.executeCommand({ command: 'sleep 0.5; echo other', execution_id: 'ex-2', command_id: 'c' });
await new Promise(r => setTimeout(r, 100));
w.handleMessage({ type: 'cancel_command', execution_id: 'ex-1' });
await Promise.all([a, b, other]);
const byId = Object.fromEntries(w.results.map(r => [r.command_id, r]));
expect(byId.a.cancelled).toBe(true);
expect(byId.b.cancelled).toBe(true);
expect(byId.c).toMatchObject({ success: true, stdout: 'other\n' });
});
test('cancelling an unknown command does nothing', () => {
const w = makeWorker();
expect(() => w.cancelCommand({ execution_id: 'nope', command_id: 'nope' })).not.toThrow();
});
});