Skip to content

Commit 957e5ee

Browse files
mchwangclaude
andcommitted
Keep the daemon's exit status when a cancel lands after the client exited
Review fix (Copilot, #70): runDocker checked the AbortSignal's current state before trusting a numeric exit code. If the Docker client had already exited with a daemon answer (for example a name conflict) and cancel() ran before the callback, the answer was discarded and the call was treated as killed with an unknown outcome, so a refused create counted as possibly created. Only Node's own abort error now marks a call as cancelled. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 15fa924 commit 957e5ee

2 files changed

Lines changed: 48 additions & 1 deletion

File tree

‎agents/docker.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ export function runDocker(args: readonly string[], options: DockerOptions): Prom
4141
const out = String(stdout ?? ''), err = String(stderr ?? '');
4242
if (!error) { resolve({ status: 0, stdout: out, stderr: err }); return; }
4343
const exitCode = (error as { code?: unknown }).code;
44-
const aborted = options.signal?.aborted === true || error.name === 'AbortError';
44+
// Only Node's own abort error means this client was killed by the signal. A numeric exit code is the daemon's
45+
// answer even if the signal was aborted after the process exited but before this callback ran.
46+
const aborted = error.name === 'AbortError' || (error as { code?: unknown }).code === 'ABORT_ERR';
4547
const killed = aborted || (error as { killed?: boolean }).killed === true;
4648
// A numeric code means the client ran and the daemon answered; anything else leaves the outcome unknown.
4749
if (typeof exitCode === 'number' && !killed) { resolve({ status: exitCode, stdout: out, stderr: err }); return; }

‎test/agent-docker.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { afterEach, describe, expect, it, vi } from 'vitest';
2+
3+
// runDocker must keep the daemon's answer when a cancel lands after the client exited (#51 item 2).
4+
const calls = vi.hoisted(() => ({ finish: undefined as undefined | ((error: unknown, stdout: string, stderr: string) => void) }));
5+
vi.mock('node:child_process', () => ({
6+
execFile: vi.fn((_file: string, _args: string[], _options: unknown, callback: (error: unknown, stdout: string,
7+
stderr: string) => void) => { calls.finish = callback; return {}; }),
8+
}));
9+
const { runDocker } = await import('../agents/docker.ts');
10+
const { createOutcomeUnknown } = await import('../agents/client-outcome.ts');
11+
12+
describe('runDocker exit classification', () => {
13+
afterEach(() => { calls.finish = undefined; });
14+
15+
it('keeps a daemon-answered exit status when the signal is aborted before the callback runs', async () => {
16+
const controller = new AbortController();
17+
const pending = runDocker(['create', '--name', 'taken'], { timeoutMs: 5_000, signal: controller.signal });
18+
// The client already exited 1 (name conflict); the cancel arrives before Node delivers the result.
19+
controller.abort();
20+
calls.finish!(Object.assign(new Error('Command failed'), { code: 1, killed: false }), '', 'Conflict. The name is in use');
21+
const outcome = await pending;
22+
expect(outcome).toMatchObject({ status: 1, stderr: 'Conflict. The name is in use' });
23+
expect(outcome.error).toBeUndefined();
24+
expect(createOutcomeUnknown({ status: outcome.status })).toBe(false);
25+
});
26+
27+
it('reports a client killed by the abort as cancelled, with the outcome unknown', async () => {
28+
const controller = new AbortController();
29+
const pending = runDocker(['create', '--name', 'x'], { timeoutMs: 5_000, signal: controller.signal });
30+
controller.abort();
31+
calls.finish!(Object.assign(new Error('The operation was aborted'), { name: 'AbortError', code: 'ABORT_ERR' }), '', '');
32+
const outcome = await pending;
33+
expect(outcome.status).toBeNull();
34+
expect(outcome.error?.message).toContain('was cancelled');
35+
expect(createOutcomeUnknown({ status: null, cause: outcome.error })).toBe(true);
36+
});
37+
38+
it('reports a client killed at its deadline as a timeout', async () => {
39+
const pending = runDocker(['create'], { timeoutMs: 5_000 });
40+
calls.finish!(Object.assign(new Error('Command failed'), { code: null, killed: true, signal: 'SIGKILL' }), '', '');
41+
const outcome = await pending;
42+
expect(outcome.status).toBeNull();
43+
expect((outcome.error as { code?: unknown }).code).toBe('ETIMEDOUT');
44+
});
45+
});

0 commit comments

Comments
 (0)