|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { EventEmitter } from 'node:events'; |
| 3 | +import { beforeEach, test, vi } from 'vitest'; |
| 4 | +import type { ExecBackgroundResult } from '@agent-device/host-kit/command'; |
| 5 | +import { |
| 6 | + AppError, |
| 7 | + createRequestCanceledError, |
| 8 | + isRequestCanceledError, |
| 9 | +} from '@agent-device/kernel/errors'; |
| 10 | +import { IOS_SIMULATOR } from './device-fixtures.ts'; |
| 11 | +import { appleRunnerTestHost } from '../test-host.ts'; |
| 12 | +import { raceRunnerStartAgainstCaller } from '../runner-start-budget.ts'; |
| 13 | +import { registerRunnerPrepProcess, runnerPrepProcessChildren } from '../runner-xctestrun.ts'; |
| 14 | + |
| 15 | +const mockSignalPidsBestEffort = vi.fn(); |
| 16 | +const mockSignalProcessGroupBestEffort = vi.fn(); |
| 17 | +const mockRunAppleToolCommand = vi.fn(); |
| 18 | + |
| 19 | +beforeEach(() => { |
| 20 | + vi.resetAllMocks(); |
| 21 | + mockRunAppleToolCommand.mockResolvedValue({ exitCode: 0, stdout: '', stderr: '' }); |
| 22 | + appleRunnerTestHost.update({ |
| 23 | + signalPidsBestEffort: mockSignalPidsBestEffort, |
| 24 | + signalProcessGroupBestEffort: mockSignalProcessGroupBestEffort, |
| 25 | + runAppleToolCommand: mockRunAppleToolCommand, |
| 26 | + }); |
| 27 | +}); |
| 28 | + |
| 29 | +function callerDeadline(): DOMException { |
| 30 | + return new DOMException('Wait deadline exceeded', 'TimeoutError'); |
| 31 | +} |
| 32 | + |
| 33 | +/** A detached start nobody has finished — the shape of a cold build still under the lock. */ |
| 34 | +function hangingStart(): Promise<never> { |
| 35 | + const promise = new Promise<never>(() => {}); |
| 36 | + return promise; |
| 37 | +} |
| 38 | + |
| 39 | +function makePrepChild(pid: number): ExecBackgroundResult['child'] { |
| 40 | + return Object.assign(new EventEmitter(), { |
| 41 | + pid, |
| 42 | + exitCode: null, |
| 43 | + }) as ExecBackgroundResult['child']; |
| 44 | +} |
| 45 | + |
| 46 | +function signaledPids(): number[] { |
| 47 | + return mockSignalProcessGroupBestEffort.mock.calls.map(([pid]) => pid as number); |
| 48 | +} |
| 49 | + |
| 50 | +function canceled(error: unknown): boolean { |
| 51 | + return isRequestCanceledError(error) && error instanceof AppError; |
| 52 | +} |
| 53 | + |
| 54 | +async function settleAsyncWork(): Promise<void> { |
| 55 | + for (let i = 0; i < 4; i += 1) { |
| 56 | + await new Promise((resolve) => setTimeout(resolve, 5)); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * The waiter's cancel owns the build it waited on (#3177). A request canceled while queued |
| 62 | + * behind a detached cold build must stop that build: its spawn carried only the STARTING |
| 63 | + * request's cancellation signal, so without the waiter's device-scoped prep kill the build |
| 64 | + * would keep compiling under the daemon on the shared runner derived-data root, and a retried |
| 65 | + * `open` would race it. The kill is the same tree-kill escalation a session stop uses. |
| 66 | + */ |
| 67 | +test('a request canceled while waiting on the detached start stops that device build', async () => { |
| 68 | + const device = { ...IOS_SIMULATOR, id: 'runner-waiter-cancel-sim' }; |
| 69 | + const build = makePrepChild(4848); |
| 70 | + registerRunnerPrepProcess(device.id, build); |
| 71 | + |
| 72 | + const controller = new AbortController(); |
| 73 | + controller.abort(createRequestCanceledError()); |
| 74 | + await assert.rejects( |
| 75 | + raceRunnerStartAgainstCaller(hangingStart(), controller.signal, device.id), |
| 76 | + canceled, |
| 77 | + ); |
| 78 | + await settleAsyncWork(); |
| 79 | + |
| 80 | + assert.ok( |
| 81 | + signaledPids().includes(4848), |
| 82 | + 'the waiter cancel reached the build through the prep tree-kill path', |
| 83 | + ); |
| 84 | + assert.equal( |
| 85 | + runnerPrepProcessChildren(device.id).length, |
| 86 | + 0, |
| 87 | + 'the killed build left the prep ledger', |
| 88 | + ); |
| 89 | +}); |
| 90 | + |
| 91 | +/** |
| 92 | + * #2894 still holds on this seam: the caller's own deadline (a bounded poll) is NOT a |
| 93 | + * cancellation. The start it interrupts is the one the retry joins, so a deadline must leave the |
| 94 | + * device's build running and un-signaled. |
| 95 | + */ |
| 96 | +test('a caller deadline on the same waiter leaves the build running', async () => { |
| 97 | + const device = { ...IOS_SIMULATOR, id: 'runner-waiter-deadline-sim' }; |
| 98 | + const build = makePrepChild(4949); |
| 99 | + registerRunnerPrepProcess(device.id, build); |
| 100 | + |
| 101 | + const controller = new AbortController(); |
| 102 | + const waiting = raceRunnerStartAgainstCaller(hangingStart(), controller.signal, device.id); |
| 103 | + controller.abort(callerDeadline()); |
| 104 | + await assert.rejects(waiting, canceled); |
| 105 | + await settleAsyncWork(); |
| 106 | + |
| 107 | + assert.equal( |
| 108 | + mockSignalProcessGroupBestEffort.mock.calls.length, |
| 109 | + 0, |
| 110 | + 'a deadline never signals the build a retry needs (#2894)', |
| 111 | + ); |
| 112 | + assert.deepEqual( |
| 113 | + runnerPrepProcessChildren(device.id).map((child) => child.pid), |
| 114 | + [4949], |
| 115 | + 'the deadline left the build registered', |
| 116 | + ); |
| 117 | +}); |
| 118 | + |
| 119 | +/** |
| 120 | + * The kill is scoped to the waiting device. A canceled waiter for device A must not signal the |
| 121 | + * build device B is still paying for — the same request-scoping rule this PR applies to the |
| 122 | + * daemon-side timeout recovery. |
| 123 | + */ |
| 124 | +test('a canceled waiter signals only its own device build', async () => { |
| 125 | + const device = { ...IOS_SIMULATOR, id: 'runner-waiter-scope-sim' }; |
| 126 | + const ownBuild = makePrepChild(5050); |
| 127 | + const siblingBuild = makePrepChild(5151); |
| 128 | + registerRunnerPrepProcess(device.id, ownBuild); |
| 129 | + registerRunnerPrepProcess('other-device', siblingBuild); |
| 130 | + |
| 131 | + const controller = new AbortController(); |
| 132 | + controller.abort(createRequestCanceledError()); |
| 133 | + await assert.rejects( |
| 134 | + raceRunnerStartAgainstCaller(hangingStart(), controller.signal, device.id), |
| 135 | + canceled, |
| 136 | + ); |
| 137 | + await settleAsyncWork(); |
| 138 | + |
| 139 | + assert.ok(signaledPids().includes(5050), 'the waiting device build was signaled'); |
| 140 | + assert.ok( |
| 141 | + !signaledPids().includes(5151), |
| 142 | + 'a build for another device was left running (#3177 sibling protection)', |
| 143 | + ); |
| 144 | +}); |
| 145 | + |
| 146 | +/** A prep child that exits on its own leaves the ledger through its close event. */ |
| 147 | +test('a closed build leaves the prep ledger on its own', () => { |
| 148 | + const device = { ...IOS_SIMULATOR, id: 'runner-prep-close-sim' }; |
| 149 | + const build = new EventEmitter(); |
| 150 | + registerRunnerPrepProcess( |
| 151 | + device.id, |
| 152 | + Object.assign(build, { pid: 5252 }) as ExecBackgroundResult['child'], |
| 153 | + ); |
| 154 | + assert.equal(runnerPrepProcessChildren(device.id).length, 1); |
| 155 | + build.emit('close'); |
| 156 | + assert.equal(runnerPrepProcessChildren(device.id).length, 0); |
| 157 | +}); |
0 commit comments