Skip to content

Commit 47220f6

Browse files
committed
fix(apple): check cancellation before the warm toolchain fingerprint cache
1 parent e823dca commit 47220f6

3 files changed

Lines changed: 53 additions & 4 deletions

File tree

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

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import path from 'node:path';
33
import { afterEach, beforeEach, expect, test, vi } from 'vitest';
44
import type { DeviceInfo } from '@agent-device/kernel/device';
55
import { createRequestCanceledError, isRequestCanceledError } from '@agent-device/kernel/errors';
6+
import { resetAllProcessMemosForTests } from '@agent-device/kernel/ttl-memo';
67
import {
78
buildDetachedRunnerLease,
89
buildRunnerLease,
@@ -14,6 +15,7 @@ import { isIosRunnerDetachEnabled, tryAdoptRunnerSessionFromLease } from '../run
1415
import { sendRunnerCommandOnce } from '../runner-transport.ts';
1516
import { resolveExpectedRunnerCacheMetadata } from '../runner-xctestrun.ts';
1617
import { appleRunnerTestHost } from '../test-host.ts';
18+
import { appleToolchainProbeResult } from './apple-toolchain-fixtures.ts';
1719
import { mkdtempForTestSync } from './tmp-dir.ts';
1820

1921
vi.mock('../runner-transport.ts', async (importOriginal) => {
@@ -143,12 +145,12 @@ test('adoption succeeds for a live, matching, probe-healthy runner', async () =>
143145
expect(readStaleRunnerLease(simulator.id)).toBeNull();
144146
});
145147

146-
test('a request canceled during the fingerprint probe fails adoption instead of skipping it', async () => {
148+
test('a request canceled during the fingerprint probe fails adoption instead of skipping it, cold or with the fingerprint cache warm', async () => {
147149
// The fingerprint check runs the same blocking toolchain probes a fresh
148150
// startup would, and it is handed the request's signal. A cancellation from
149151
// there is not an unresolvable derived path: swallowing it would let startup
150152
// walk on past a client that is already gone (#2422).
151-
writeStaleLease();
153+
const lease = writeStaleLease();
152154
mockIsProcessAlive.mockReturnValue(true);
153155
const request = new AbortController();
154156
request.abort();
@@ -162,6 +164,33 @@ test('a request canceled during the fingerprint probe fails adoption instead of
162164
tryAdoptRunnerSessionFromLease(simulator, { signal: request.signal }),
163165
).rejects.toSatisfy(isRequestCanceledError);
164166
expect(mockSendRunnerCommandOnce).not.toHaveBeenCalled();
167+
168+
// The mock above only proves adoption propagates whatever the fingerprint
169+
// call throws. Exercise the real production code path too: warm the actual
170+
// toolchain fingerprint memo (the module-level cache in
171+
// runner-cache-metadata.ts is a singleton the mock above never touches),
172+
// then adopt again with an already-aborted signal. Before the fix,
173+
// requireRunnerToolchainFingerprint returned the memoized value without
174+
// checking the signal, so a cache hit let adoption go on to probe uptime
175+
// and write the lease for an already-canceled request (#2422 round 4).
176+
resetAllProcessMemosForTests();
177+
const { resolveExpectedRunnerCacheMetadata: actualResolveExpectedRunnerCacheMetadata } =
178+
await vi.importActual<typeof import('../runner-xctestrun.ts')>('../runner-xctestrun.ts');
179+
appleRunnerTestHost.update({ runCmdSync: vi.fn(appleToolchainProbeResult) });
180+
actualResolveExpectedRunnerCacheMetadata(simulator);
181+
182+
const warmRequest = new AbortController();
183+
warmRequest.abort();
184+
mockResolveExpectedRunnerCacheMetadata.mockImplementationOnce(
185+
actualResolveExpectedRunnerCacheMetadata,
186+
);
187+
188+
await expect(
189+
tryAdoptRunnerSessionFromLease(simulator, { signal: warmRequest.signal }),
190+
).rejects.toSatisfy(isRequestCanceledError);
191+
expect(mockSendRunnerCommandOnce).not.toHaveBeenCalled();
192+
// Ownership was never transferred: the stale lease is untouched.
193+
expect(readStaleRunnerLease(simulator.id)?.ownerToken).toBe(lease.ownerToken);
165194
});
166195

167196
test('adoption is skipped for a recycled runner pid (start time mismatch)', async () => {

‎packages/platform-apple/src/runner/__tests__/runner-cache-metadata.test.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ describe('toolchain probe budget', () => {
338338
assert.equal(runCmdSync.mock.calls.length, 1);
339339
});
340340

341-
test('an already-canceled request runs no toolchain probe at all', () => {
341+
test('an already-canceled request runs no toolchain probe at all, cold or with the fingerprint cache warm', () => {
342342
installFakeToolchainClock();
343343
runCmdSync.mockClear();
344344

@@ -350,6 +350,21 @@ describe('toolchain probe budget', () => {
350350
(error: unknown) => isRequestCanceledError(error),
351351
);
352352
assert.equal(runCmdSync.mock.calls.length, 0);
353+
354+
// Warm the real fingerprint memo with an ordinary request, then repeat
355+
// with an already-aborted signal: the cache-hit path must check
356+
// cancellation before it returns the memoized value, not skip it (#2422).
357+
resolveExpectedRunnerCacheMetadata(IOS_SIMULATOR);
358+
runCmdSync.mockClear();
359+
360+
assert.throws(
361+
() =>
362+
resolveExpectedRunnerCacheMetadata(IOS_SIMULATOR, undefined, {
363+
signal: AbortSignal.abort(),
364+
}),
365+
(error: unknown) => isRequestCanceledError(error),
366+
);
367+
assert.equal(runCmdSync.mock.calls.length, 0);
353368
});
354369

355370
test('a probe that failed on its own and merely says "timed out" in its message is not retried', () => {

‎packages/platform-apple/src/runner/runner-cache-metadata.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,14 @@ function requireRunnerToolchainFingerprint(
284284
sdkName: string,
285285
budget: RunnerCacheProbeBudget | undefined,
286286
): RunnerToolchainFingerprint {
287+
// Checked before the cache, not just before the probes: a canceled request
288+
// must surface as canceled even on a cache hit, or adoption reads a stale
289+
// "success" and goes on to probe uptime and write the lease (#2422).
290+
const clock = createToolchainProbeClock(budget);
291+
clock.throwIfCanceled();
287292
const cached = toolchainFingerprintCache().get(sdkName);
288293
if (cached) return cached;
289-
const fingerprint = readRunnerToolchainFingerprint(sdkName, createToolchainProbeClock(budget));
294+
const fingerprint = readRunnerToolchainFingerprint(sdkName, clock);
290295
if (!fingerprint.ok) throw unavailableToolchainError(fingerprint.failures);
291296
toolchainFingerprintCache().set(sdkName, fingerprint.value);
292297
return fingerprint.value;

0 commit comments

Comments
 (0)