Skip to content

Commit 56d63cb

Browse files
committed
fix(ios-runner): a caller deadline no longer kills a starting runner
A wait poll bounds each attempt with a TimeoutError abort. That signal was merged with the request cancellation and handed to the xcodebuild launch, so a short wait timeout during runner start killed the runner and the retry paid the whole start again. The start now reacts only to request cancellation; a caller deadline leaves the session starting so the next request joins it. Closes callstack#2894.
1 parent 34f2eb1 commit 56d63cb

6 files changed

Lines changed: 205 additions & 25 deletions

File tree

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import assert from 'node:assert/strict';
2+
import { beforeEach, test } from 'vitest';
3+
import { createRequestCanceledError } from '@agent-device/kernel/errors';
4+
import { appleRunnerTestHost } from '../test-host.ts';
5+
import {
6+
callerDeadlineExpired,
7+
isCallerDeadlineAbortReason,
8+
resolveRunnerStartupSignal,
9+
} from '../runner-contract.ts';
10+
11+
const registered = new Map<string, AbortController>();
12+
const canceled = new Set<string>();
13+
14+
function deadlineReason(): DOMException {
15+
return new DOMException('Wait deadline exceeded', 'TimeoutError');
16+
}
17+
18+
beforeEach(() => {
19+
registered.clear();
20+
canceled.clear();
21+
appleRunnerTestHost.update({
22+
getRequestSignal: (requestId) => (requestId ? registered.get(requestId)?.signal : undefined),
23+
isRequestCanceled: (requestId) => requestId !== undefined && canceled.has(requestId),
24+
});
25+
});
26+
27+
test('a caller deadline is the typed TimeoutError reason, not its text', () => {
28+
assert.equal(isCallerDeadlineAbortReason(deadlineReason()), true);
29+
assert.equal(isCallerDeadlineAbortReason(new Error('Wait deadline exceeded')), false);
30+
assert.equal(isCallerDeadlineAbortReason(createRequestCanceledError()), false);
31+
});
32+
33+
test('the startup signal ignores a caller deadline and forwards every other abort', () => {
34+
const deadline = new AbortController();
35+
const startup = resolveRunnerStartupSignal({ signal: deadline.signal });
36+
assert.ok(startup);
37+
deadline.abort(deadlineReason());
38+
assert.equal(startup.aborted, false);
39+
40+
const disconnect = new AbortController();
41+
const killed = resolveRunnerStartupSignal({ signal: disconnect.signal });
42+
assert.ok(killed);
43+
const reason = new Error('client disconnected');
44+
disconnect.abort(reason);
45+
assert.equal(killed.aborted, true);
46+
assert.equal(killed.reason, reason);
47+
});
48+
49+
test('a caller signal already aborted by its deadline does not abort the startup signal', () => {
50+
const expired = new AbortController();
51+
expired.abort(deadlineReason());
52+
const startup = resolveRunnerStartupSignal({ signal: expired.signal });
53+
assert.equal(startup?.aborted, false);
54+
});
55+
56+
test('the registered request signal kills a start even when it rides with a caller deadline', () => {
57+
const request = new AbortController();
58+
registered.set('req-1', request);
59+
const deadline = new AbortController();
60+
const startup = resolveRunnerStartupSignal({ requestId: 'req-1', signal: deadline.signal });
61+
assert.ok(startup);
62+
deadline.abort(deadlineReason());
63+
assert.equal(startup.aborted, false);
64+
request.abort(createRequestCanceledError());
65+
assert.equal(startup.aborted, true);
66+
});
67+
68+
test('without a caller signal the registered request signal is the startup signal itself', () => {
69+
const request = new AbortController();
70+
registered.set('req-2', request);
71+
assert.equal(resolveRunnerStartupSignal({ requestId: 'req-2' }), request.signal);
72+
assert.equal(resolveRunnerStartupSignal({}), undefined);
73+
});
74+
75+
test('callerDeadlineExpired reads the deadline reason and yields to a cancelled request', () => {
76+
const deadline = new AbortController();
77+
deadline.abort(deadlineReason());
78+
assert.equal(callerDeadlineExpired({ signal: deadline.signal }), true);
79+
canceled.add('req-3');
80+
assert.equal(callerDeadlineExpired({ requestId: 'req-3', signal: deadline.signal }), false);
81+
const plain = new AbortController();
82+
plain.abort(new Error('client disconnected'));
83+
assert.equal(callerDeadlineExpired({ signal: plain.signal }), false);
84+
assert.equal(callerDeadlineExpired({}), false);
85+
});

‎packages/platform-apple/src/runner/__tests__/runner-request-cancellation.test.ts‎

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,9 @@ test('direct command cancellation reaches runner launch without a registered req
161161
const controller = new AbortController();
162162
const device = { ...IOS_SIMULATOR, id: 'runner-direct-signal-sim' };
163163
mockRunCmdBackground.mockImplementationOnce((_cmd, _args, options) => {
164-
assert.equal(options?.signal, controller.signal);
165-
controller.abort(new Error('wait deadline exceeded'));
164+
assert.equal(options?.signal?.aborted, false);
165+
controller.abort(new Error('client disconnected'));
166+
assert.equal(options?.signal?.aborted, true);
166167
return makeBackgroundRunner(4141);
167168
});
168169

@@ -178,6 +179,51 @@ test('direct command cancellation reaches runner launch without a registered req
178179
assert.equal(readRunnerSessionLiveness(device.id), null);
179180
});
180181

182+
/**
183+
* A `wait` poll bounds each attempt with a `TimeoutError` deadline. When that deadline lands while the
184+
* runner is still starting, the start it interrupts is the one the retry needs: the launch must not be
185+
* killed and the session must stay registered as starting, so the next request joins it (#2894).
186+
*/
187+
test('a caller deadline during runner start leaves the starting runner for the next request', async () => {
188+
const controller = new AbortController();
189+
const device = { ...IOS_SIMULATOR, id: 'runner-caller-deadline-sim' };
190+
mockRunCmdBackground.mockImplementationOnce((_cmd, _args, options) => {
191+
controller.abort(new DOMException('Wait deadline exceeded', 'TimeoutError'));
192+
assert.equal(options?.signal?.aborted, false, 'the launch outlives the caller deadline');
193+
return makeBackgroundRunner(4545);
194+
});
195+
mockWaitForRunner.mockImplementationOnce(async () => {
196+
throw createRequestCanceledError();
197+
});
198+
199+
await assert.rejects(
200+
executeRunnerCommand(
201+
device,
202+
{ command: 'snapshot', appBundleId: 'com.example.demo' },
203+
{ signal: controller.signal, logPath: '/tmp/runner.log' },
204+
),
205+
(error: unknown) =>
206+
isRequestCanceledError(error) &&
207+
(error as { details?: { readinessPhase?: string } }).details?.readinessPhase ===
208+
'runner-start',
209+
);
210+
assert.equal(readRunnerSessionLiveness(device.id)?.liveness, 'starting');
211+
assert.deepEqual(readRetainedLeaseDeviceIds(), [device.id]);
212+
213+
await executeRunnerCommand(
214+
device,
215+
{ command: 'snapshot', appBundleId: 'com.example.demo' },
216+
{ logPath: '/tmp/runner.log' },
217+
);
218+
219+
assert.equal(
220+
mockRunCmdBackground.mock.calls.length,
221+
1,
222+
'the retry did not launch a second runner',
223+
);
224+
assert.equal(readRunnerSessionLiveness(device.id)?.liveness, 'ready');
225+
});
226+
181227
test('prepare cancellation stops only its runner and preserves unrelated prep', async () => {
182228
const survivorRequestId = 'prepare-runner-survivor-B';
183229
const canceledRequestId = 'prepare-runner-canceled-A';

‎packages/platform-apple/src/runner/runner-client.ts‎

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { retryWithPolicy, emitDiagnostic, getRequestSignal, isRequestCanceled } from './host.ts';
1+
import { retryWithPolicy, emitDiagnostic } from './host.ts';
22
import { isIosFamily, type DeviceInfo } from '@agent-device/kernel/device';
33
import {
44
ensureRunnerSession,
@@ -9,6 +9,7 @@ import {
99
} from './runner-session.ts';
1010
import {
1111
assertRunnerRequestActive,
12+
callerDeadlineExpired,
1213
resolveRunnerRequestSignal,
1314
withRunnerCommandId,
1415
type RunnerCommand,
@@ -57,21 +58,6 @@ function readOnlyResendBudget(error: unknown): number {
5758
return isRetryableRunnerError(error) ? TRANSPORT_RESEND_ATTEMPTS : 1;
5859
}
5960

60-
/**
61-
* Whether the caller's own deadline ended this command, as opposed to the request being cancelled.
62-
* A `wait` bounds each poll with an abort signal whose reason is a `TimeoutError`
63-
* (`runWithinWaitDeadline`); a cancelled request aborts through the registered request signal or
64-
* the cancellation registry. The typed reason decides, so a deadline that lands mid-fetch (surfacing
65-
* as whatever the transport threw on abort) is read the same way as one that wakes a delay.
66-
*/
67-
function callerDeadlineExpired(options: AppleRunnerCommandOptions): boolean {
68-
if (isRequestCanceled(options.requestId) || getRequestSignal(options.requestId)?.aborted) {
69-
return false;
70-
}
71-
const reason: unknown = options.signal?.aborted ? options.signal.reason : undefined;
72-
return reason instanceof DOMException && reason.name === 'TimeoutError';
73-
}
74-
7561
export async function runAppleRunnerCommand(
7662
device: DeviceInfo,
7763
command: RunnerCommand,

‎packages/platform-apple/src/runner/runner-contract.ts‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,64 @@ export function resolveRunnerRequestSignal(options: {
157157
return AbortSignal.any([registeredSignal, options.signal]);
158158
}
159159

160+
type RunnerRequestSignalOptions = {
161+
requestId?: string;
162+
signal?: AbortSignal;
163+
};
164+
165+
/**
166+
* Whether an abort reason is a caller's own deadline rather than a cancelled request. A `wait`
167+
* bounds each poll with an abort signal whose reason is a `TimeoutError` (`runWithinWaitDeadline`);
168+
* a cancelled request aborts through the registered request signal or the cancellation registry.
169+
* The typed reason decides, never the error text the transport threw on abort.
170+
*/
171+
export function isCallerDeadlineAbortReason(reason: unknown): boolean {
172+
return reason instanceof DOMException && reason.name === 'TimeoutError';
173+
}
174+
175+
/**
176+
* Whether the caller's own deadline ended this command, as opposed to the request being cancelled.
177+
* A deadline that lands mid-fetch (surfacing as whatever the transport threw on abort) is read the
178+
* same way as one that wakes a delay.
179+
*/
180+
export function callerDeadlineExpired(options: RunnerRequestSignalOptions): boolean {
181+
if (isRequestCanceled(options.requestId) || getRequestSignal(options.requestId)?.aborted) {
182+
return false;
183+
}
184+
return options.signal?.aborted === true && isCallerDeadlineAbortReason(options.signal.reason);
185+
}
186+
187+
/**
188+
* The signal a runner start reacts to. A cancelled request (client disconnect) must kill the
189+
* blocking xctestrun build and the runner launch instead of orphaning them, so the registered request
190+
* signal passes through untouched. A caller's own deadline must not: the runner start it interrupts
191+
* is the one the retry needs, and a start that pays itself again on every short-timeout poll never
192+
* finishes on a slow host (#2894). The start keeps going on its own startup budget, and the caller's
193+
* command is still cut off by its unfiltered signal once the runner answers.
194+
*/
195+
export function resolveRunnerStartupSignal(
196+
options: RunnerRequestSignalOptions,
197+
): AbortSignal | undefined {
198+
const registeredSignal = getRequestSignal(options.requestId);
199+
const callerSignal = options.signal;
200+
if (!callerSignal || callerSignal === registeredSignal) return registeredSignal;
201+
const controller = new AbortController();
202+
const forward = (signal: AbortSignal) => {
203+
if (controller.signal.aborted) return;
204+
if (isCallerDeadlineAbortReason(signal.reason)) return;
205+
controller.abort(signal.reason);
206+
};
207+
for (const signal of [registeredSignal, callerSignal]) {
208+
if (!signal) continue;
209+
if (signal.aborted) {
210+
forward(signal);
211+
continue;
212+
}
213+
signal.addEventListener('abort', () => forward(signal), { once: true });
214+
}
215+
return controller.signal;
216+
}
217+
160218
/**
161219
* The code the XCTest runner answers with when it declines to place a scroll gesture under the
162220
* on-screen keyboard (#2500). It is the runner's own vocabulary, so it is declared here beside the

‎packages/platform-apple/src/runner/runner-lifecycle.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
} from './runner-session.ts';
2222
import {
2323
assertRunnerRequestActive,
24+
callerDeadlineExpired,
2425
resolveRunnerRequestSignal,
2526
withRunnerCommandId,
2627
type RunnerCommand,
@@ -311,7 +312,10 @@ export async function executeRunnerCommand(
311312
? session.state === 'starting'
312313
: livenessAtEntry !== 'ready';
313314
if (runnerNeverAnswered && isRequestCanceledError(appErr)) {
314-
if (session) {
315+
// A cancelled request leaves no half-started runner behind. A caller whose own deadline ran
316+
// out mid-start leaves it running: the start is on its own budget, and the next request
317+
// joins it instead of paying it again (#2894).
318+
if (session && !callerDeadlineExpired(options)) {
315319
await invalidateRunnerSessionBestEffort(session, 'runner_startup_request_canceled');
316320
}
317321
throw createRequestCanceledError(

‎packages/platform-apple/src/runner/runner-session.ts‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import {
3535
decodeRunnerResponseBody,
3636
isRunnerResponseOk,
3737
readRunnerResponseData,
38-
resolveRunnerRequestSignal,
38+
resolveRunnerStartupSignal,
3939
withRunnerCommandId,
4040
type RunnerCommand,
4141
} from './runner-contract.ts';
@@ -138,14 +138,15 @@ export async function ensureRunnerSession(
138138
return await withRunnerSessionLock(device.id, async () => {
139139
// One budget for the whole startup phase, opened here from the request-level
140140
// `startupTimeoutMs`: the reuse check's toolchain probes, adoption and the startup
141-
// itself all spend this one clock. The request's abort signal rides with it, so a
141+
// itself all spend this one clock. The request's cancellation rides with it, so a
142142
// client disconnect kills the blocking xctestrun build and runner launch
143-
// (killProcessTree via exec) instead of orphaning them. Request-scoped: only this
144-
// request's device startup reacts, and a signal-less internal caller (shutdown)
145-
// simply gets undefined.
143+
// (killProcessTree via exec) instead of orphaning them; a caller's own deadline does
144+
// not, so the start it interrupts is still there for the retry (#2894). Request-scoped:
145+
// only this request's device startup reacts, and a signal-less internal caller
146+
// (shutdown) simply gets undefined.
146147
const startupBudget = createRunnerPhaseBudget(
147148
options.startupTimeoutMs,
148-
resolveRunnerRequestSignal(options),
149+
resolveRunnerStartupSignal(options),
149150
);
150151
const existing = runnerSessions.get(device.id);
151152
if (existing) {

0 commit comments

Comments
 (0)