Skip to content

Commit 3f2dac8

Browse files
committed
fix(ios-runner): bound a detached runner start by its launch deadline
A caller deadline no longer waits through the start: ensureRunnerSession runs the lock-held start detached and races it against the caller signal, so a short wait poll returns on time during a cold xctestrun build and the next request joins the same start. The session records a launchDeadline when it enters starting. Joiners measure readiness from it instead of opening a fresh budget each, and the reuse check retires a starting session past that budget so a runner that never answers cannot be joined forever. The HTTP progress sink skips a response that already ended, since a detached start can emit progress after its caller returned.
1 parent eeac883 commit 3f2dac8

8 files changed

Lines changed: 232 additions & 66 deletions

File tree

‎packages/platform-apple/src/runner/__tests__/runner-command-retry.test.ts‎

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -544,24 +544,33 @@ test('read-only commands retry when completed status has no retained response',
544544
});
545545
});
546546

547-
test('read-only startup commands use the session startup timeout override', async () => {
548-
const session = makeRunnerSession({
549-
port: 8100,
550-
state: 'starting',
551-
startupTimeoutMs: 240_000,
552-
});
547+
test('read-only startup commands measure readiness from the session launch deadline', async () => {
548+
vi.useFakeTimers();
549+
try {
550+
vi.setSystemTime(1_000);
551+
const session = makeRunnerSession({
552+
port: 8100,
553+
state: 'starting',
554+
launchDeadline: Deadline.fromTimeoutMs(240_000),
555+
});
553556

554-
mockEnsureRunnerSession.mockResolvedValue(session);
555-
mockExecuteRunnerCommandWithSession.mockResolvedValue({ currentUptimeMs: 42 });
557+
mockEnsureRunnerSession.mockImplementationOnce(async () => {
558+
vi.setSystemTime(41_000);
559+
return session;
560+
});
561+
mockExecuteRunnerCommandWithSession.mockResolvedValue({ currentUptimeMs: 42 });
556562

557-
const result = await runAppleRunnerCommand(
558-
IOS_SIMULATOR,
559-
{ command: 'uptime' },
560-
{ startupTimeoutMs: 240_000 },
561-
);
563+
const result = await runAppleRunnerCommand(
564+
IOS_SIMULATOR,
565+
{ command: 'uptime' },
566+
{ startupTimeoutMs: 240_000 },
567+
);
562568

563-
assert.deepEqual(result, { currentUptimeMs: 42 });
564-
assert.equal(mockExecuteRunnerCommandWithSession.mock.calls[0]?.[4], 240_000);
569+
assert.deepEqual(result, { currentUptimeMs: 42 });
570+
assert.equal(mockExecuteRunnerCommandWithSession.mock.calls[0]?.[4], 200_000);
571+
} finally {
572+
vi.useRealTimers();
573+
}
565574
});
566575

567576
test('read-only commands retry when status shows in-flight work', async () => {

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

Lines changed: 138 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,23 @@ vi.mock('../runner-xctestrun.ts', async () => {
7070

7171
import { createRequestCanceledError, isRequestCanceledError } from '@agent-device/kernel/errors';
7272
import { abortAllIosRunnerSessions, readRunnerSessionLiveness } from '../runner-session.ts';
73+
import { RUNNER_STARTUP_TIMEOUT_MS } from '../runner-startup-transport.ts';
7374
import type { RunnerLease } from '../runner-lease.ts';
7475
import { executeRunnerCommand, prepareLocalIosRunner } from '../runner-lifecycle.ts';
76+
import { captureDiagnostics } from './runner-session-fixtures.ts';
77+
78+
const SNAPSHOT = { command: 'snapshot', appBundleId: 'com.example.demo' } as const;
79+
80+
function callerDeadline(): DOMException {
81+
return new DOMException('Wait deadline exceeded', 'TimeoutError');
82+
}
83+
84+
function readinessCanceled(error: unknown): boolean {
85+
return (
86+
isRequestCanceledError(error) &&
87+
(error as { details?: { readinessPhase?: string } }).details?.readinessPhase === 'runner-start'
88+
);
89+
}
7590

7691
// Root-registry writers (`request/cancel.ts`) are not visible to the package;
7792
// this reproduces the same canceled-set/AbortController-map model locally and
@@ -182,45 +197,139 @@ test('direct command cancellation reaches runner launch without a registered req
182197
/**
183198
* A `wait` poll bounds each attempt with a `TimeoutError` deadline. When that deadline lands while the
184199
* 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).
200+
* killed and the session must stay registered as starting, so the next request joins it and spends
201+
* what is left of the launch budget, not a fresh one (#2894).
186202
*/
187203
test('a caller deadline during runner start leaves the starting runner for the next request', async () => {
204+
vi.useFakeTimers({ toFake: ['Date'] });
205+
try {
206+
const controller = new AbortController();
207+
const device = { ...IOS_SIMULATOR, id: 'runner-caller-deadline-sim' };
208+
let launchSignal: AbortSignal | undefined;
209+
mockRunCmdBackground.mockImplementationOnce((_cmd, _args, options) => {
210+
launchSignal = options?.signal;
211+
return makeBackgroundRunner(4545);
212+
});
213+
mockWaitForRunner.mockImplementationOnce(async () => {
214+
controller.abort(callerDeadline());
215+
throw createRequestCanceledError();
216+
});
217+
218+
await assert.rejects(
219+
executeRunnerCommand(device, SNAPSHOT, {
220+
signal: controller.signal,
221+
logPath: '/tmp/runner.log',
222+
}),
223+
readinessCanceled,
224+
);
225+
assert.equal(launchSignal?.aborted, false, 'the launch outlives the caller deadline');
226+
assert.equal(readRunnerSessionLiveness(device.id)?.liveness, 'starting');
227+
assert.deepEqual(readRetainedLeaseDeviceIds(), [device.id]);
228+
229+
vi.setSystemTime(Date.now() + 10_000);
230+
await executeRunnerCommand(device, SNAPSHOT, { logPath: '/tmp/runner.log' });
231+
232+
assert.equal(mockRunCmdBackground.mock.calls.length, 1, 'the retry did not launch again');
233+
assert.equal(
234+
mockWaitForRunner.mock.calls[1]?.[4],
235+
RUNNER_STARTUP_TIMEOUT_MS - 10_000,
236+
'the joiner measures readiness from the launch, not from its own arrival',
237+
);
238+
assert.equal(readRunnerSessionLiveness(device.id)?.liveness, 'ready');
239+
} finally {
240+
vi.useRealTimers();
241+
}
242+
});
243+
244+
/**
245+
* A runner whose process stays up but never answers must not be joined forever: once the launch
246+
* budget recorded on the session is spent, the next request retires it and launches again, however
247+
* the earlier joiners' waits ended (#2894).
248+
*/
249+
test('a request joining a start past its launch budget retires the runner and launches again', async () => {
250+
vi.useFakeTimers({ toFake: ['Date'] });
251+
try {
252+
const first = new AbortController();
253+
const device = { ...IOS_SIMULATOR, id: 'runner-launch-budget-sim' };
254+
mockRunCmdBackground
255+
.mockReturnValueOnce(makeBackgroundRunner(4646))
256+
.mockReturnValueOnce(makeBackgroundRunner(4747));
257+
mockWaitForRunner.mockImplementationOnce(async () => {
258+
first.abort(callerDeadline());
259+
throw createRequestCanceledError();
260+
});
261+
262+
await assert.rejects(
263+
executeRunnerCommand(device, SNAPSHOT, { signal: first.signal, logPath: '/tmp/runner.log' }),
264+
readinessCanceled,
265+
);
266+
const hung = readRunnerSessionLiveness(device.id);
267+
assert.equal(hung?.liveness, 'starting');
268+
269+
vi.setSystemTime(Date.now() + RUNNER_STARTUP_TIMEOUT_MS + 1);
270+
const second = new AbortController();
271+
const diagnostics = await captureDiagnostics(async () => {
272+
await executeRunnerCommand(device, SNAPSHOT, {
273+
signal: second.signal,
274+
logPath: '/tmp/runner.log',
275+
});
276+
});
277+
278+
assert.match(diagnostics, /runner_launch_budget_exhausted/);
279+
assert.equal(mockRunCmdBackground.mock.calls.length, 2, 'the hung runner was replaced');
280+
const relaunched = readRunnerSessionLiveness(device.id);
281+
assert.equal(relaunched?.liveness, 'ready');
282+
assert.notEqual(relaunched?.sessionId, hung?.sessionId);
283+
assert.deepEqual(readRetainedLeaseDeviceIds(), [device.id]);
284+
} finally {
285+
vi.useRealTimers();
286+
}
287+
});
288+
289+
/**
290+
* The start runs detached under the session lock. A caller whose deadline lands during the cold
291+
* xctestrun build leaves on time with the readiness verdict, the build is neither killed nor
292+
* repeated, and the next request queues behind it and joins the session it registers (#2894).
293+
*/
294+
test('a caller deadline during the xctestrun build leaves on time and the next request joins that start', async () => {
188295
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);
296+
const device = { ...IOS_SIMULATOR, id: 'runner-build-deadline-sim' };
297+
let releaseBuild: () => void = () => {};
298+
const buildReleased = new Promise<void>((resolve) => {
299+
releaseBuild = resolve;
194300
});
195-
mockWaitForRunner.mockImplementationOnce(async () => {
196-
throw createRequestCanceledError();
301+
let buildSignal: AbortSignal | undefined;
302+
mockEnsureXctestrunArtifact.mockImplementationOnce(async (_device, options) => {
303+
buildSignal = options.budget?.signal;
304+
controller.abort(callerDeadline());
305+
await buildReleased;
306+
return {
307+
xctestrunPath: '/tmp/base-runner.xctestrun',
308+
derived: '/tmp/derived',
309+
cache: 'miss',
310+
artifact: 'rebuilt',
311+
buildMs: 12,
312+
xctestrunPathSource: 'build',
313+
};
197314
});
198315

199316
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',
317+
executeRunnerCommand(device, SNAPSHOT, {
318+
signal: controller.signal,
319+
logPath: '/tmp/runner.log',
320+
}),
321+
readinessCanceled,
209322
);
210-
assert.equal(readRunnerSessionLiveness(device.id)?.liveness, 'starting');
211-
assert.deepEqual(readRetainedLeaseDeviceIds(), [device.id]);
323+
assert.equal(mockRunCmdBackground.mock.calls.length, 0, 'the caller left before the launch');
324+
assert.equal(buildSignal?.aborted, false, 'the build outlives the caller deadline');
325+
assert.equal(readRunnerSessionLiveness(device.id), null);
212326

213-
await executeRunnerCommand(
214-
device,
215-
{ command: 'snapshot', appBundleId: 'com.example.demo' },
216-
{ logPath: '/tmp/runner.log' },
217-
);
327+
const joined = executeRunnerCommand(device, SNAPSHOT, { logPath: '/tmp/runner.log' });
328+
releaseBuild();
329+
await joined;
218330

219-
assert.equal(
220-
mockRunCmdBackground.mock.calls.length,
221-
1,
222-
'the retry did not launch a second runner',
223-
);
331+
assert.equal(mockEnsureXctestrunArtifact.mock.calls.length, 1, 'the joiner did not build again');
332+
assert.equal(mockRunCmdBackground.mock.calls.length, 1, 'the joiner did not launch again');
224333
assert.equal(readRunnerSessionLiveness(device.id)?.liveness, 'ready');
225334
});
226335

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

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,12 @@ import {
2525
type RunnerLeaseAdoptionRefusal,
2626
} from './runner-lease.ts';
2727
import {
28-
requireRunnerPhaseRemainingMs,
2928
resolveExpectedRunnerCacheMetadata,
3029
resolveRunnerDerivedPath,
3130
type RunnerPhaseBudget,
3231
type RunnerXctestrunArtifact,
3332
} from './runner-xctestrun.ts';
34-
import {
35-
normalizeRunnerStartupTimeoutMs,
36-
type RunnerProcessHandle,
37-
type RunnerSession,
38-
} from './runner-session-types.ts';
33+
import type { RunnerProcessHandle, RunnerSession } from './runner-session-types.ts';
3934

4035
// A healthy localhost runner answers uptime in tens of milliseconds and a dead
4136
// port refuses immediately; the timeout only bounds the wedged-runner case,
@@ -132,7 +127,7 @@ export async function tryAdoptRunnerSessionFromLease(
132127
return skip('runner_pid_recycled', lease);
133128
}
134129

135-
const session = buildAdoptedRunnerSession(device, lease, runnerPid, expectedDerived, options);
130+
const session = buildAdoptedRunnerSession(device, lease, runnerPid, expectedDerived);
136131
try {
137132
writeRunnerLease(session.lease);
138133
} catch {
@@ -276,7 +271,6 @@ function buildAdoptedRunnerSession(
276271
lease: RunnerLease,
277272
runnerPid: number,
278273
expectedDerived: string,
279-
options: { budget?: RunnerPhaseBudget },
280274
): RunnerSession & { lease: RunnerLease } {
281275
const sessionId = lease.sessionId;
282276
const artifact: RunnerXctestrunArtifact = {
@@ -306,9 +300,6 @@ function buildAdoptedRunnerSession(
306300
state: 'ready',
307301
inFlightCommands: 0,
308302
hasAbandonedCommands: false,
309-
startupTimeoutMs: normalizeRunnerStartupTimeoutMs(
310-
requireRunnerPhaseRemainingMs(options.budget, 'runner_session_adoption'),
311-
),
312303
lease: buildRunnerLease({
313304
device,
314305
sessionId,

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,9 @@ export async function executeRunnerCommand(
313313
: livenessAtEntry !== 'ready';
314314
if (runnerNeverAnswered && isRequestCanceledError(appErr)) {
315315
// 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).
316+
// out mid-start leaves it running: the session's launch budget bounds it, the next request
317+
// joins it instead of paying it again, and the reuse check retires it once that budget is
318+
// spent (#2894).
318319
if (session && !callerDeadlineExpired(options)) {
319320
await invalidateRunnerSessionBestEffort(session, 'runner_startup_request_canceled');
320321
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { RunnerLogicalLeaseContext } from '@agent-device/contracts/runner-lease-context';
22
import type { ExecResult } from '@agent-device/host-kit/command';
33
import type { DeviceInfo } from '@agent-device/kernel/device';
4+
import type { Deadline } from './host.ts';
45
import type { RunnerXctestrunArtifact } from './runner-xctestrun.ts';
56
import type { RunnerLease } from './runner-lease.ts';
67
import type { IosRunnerDeviceStates } from './runner-error-classification.ts';
@@ -80,7 +81,12 @@ export type RunnerSession = {
8081
state: RunnerSessionState;
8182
/** Wakes one startup retry when the listener becomes ready or its process exits. */
8283
startupRetryWake?: AbortSignal;
83-
startupTimeoutMs?: number;
84+
/**
85+
* The budget the runner has to answer its first command, opened the moment its process was
86+
* launched. Every request that joins the `starting` session measures readiness from this one
87+
* clock, so a runner that never answers is given up once, not once per joiner (#2894).
88+
*/
89+
launchDeadline?: Deadline;
8490
/**
8591
* Commands the runner accepted that this process has not seen answered. It comes down only when a
8692
* response is decoded: an aborted or dropped exchange leaves the command running on the runner,

0 commit comments

Comments
 (0)