Skip to content

Commit e729321

Browse files
committed
fix(ios): keep observation on the bridge while app discovery is pending and no runner is live
#2331 bounds one capture's wait for the Simulator app discovery and takes the XCTest fallback past it; #2198 stops a Simulator open from awaiting the runner. Together, a `wait` right after a relaunch on a loaded host fell back to XCTest while the runner was still starting, spent its poll budget on that start, and timed out (the iOS smoke lane after the main merge). A capture with no live runner now stays on the single-flight discovery, one wait slice at a time, until the discovery's own deadline or the request signal ends it; a runner that is already live still takes the fallback at once, the cheaper route #2331 chose.
1 parent adec882 commit e729321

2 files changed

Lines changed: 101 additions & 4 deletions

File tree

‎packages/platform-apple/src/snapshot-route.test.ts‎

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,10 @@ test('cancelled acquisition does not start a fallback after the request aborts',
204204
expect(fallback).not.toHaveBeenCalled();
205205
});
206206

207-
test('a slow app discovery yields to the XCTest fallback within its wait slice, then serves the bridge', async () => {
207+
test('a slow app discovery yields to a live runner within its wait slice, then serves the bridge', async () => {
208208
// The production resolver over a simctl whose `launchctl list` answers only when released,
209-
// the shape of a loaded CI host: the first capture must not sit on that probe.
209+
// the shape of a loaded CI host: with a runner that can answer at once, the first capture
210+
// must not sit on that probe.
210211
let release!: () => void;
211212
const released = new Promise<void>((resolve) => {
212213
release = resolve;
@@ -232,9 +233,11 @@ test('a slow app discovery yields to the XCTest fallback within its wait slice,
232233
producer: 'simulator-ax-bridge' as const,
233234
nodes: [{ index: 0, type: 'Application' }],
234235
}));
236+
const baseHost = platformRuntimeHostFixture();
235237
const route = createAppleSnapshotRoute(
236238
{
237-
...platformRuntimeHostFixture(),
239+
...baseHost,
240+
appleApplications: { ...baseHost.appleApplications, hasLiveRunnerSession: async () => true },
238241
snapshot: { captureSurface: vi.fn(), presentIosAcquisition },
239242
},
240243
{ source, resolveTarget: createSimulatorSnapshotTargetResolver() },
@@ -302,3 +305,65 @@ function runnerResult() {
302305
function signal(): AbortSignal {
303306
return new AbortController().signal;
304307
}
308+
309+
test('a slow app discovery keeps observation on the bridge while no runner can answer', async () => {
310+
// #2198: the open no longer awaits the runner, so right after a relaunch the fallback would
311+
// wait for a cold runner start. A capture with no live runner rides the single-flight
312+
// discovery instead, however many wait slices that takes.
313+
let release!: () => void;
314+
const released = new Promise<void>((resolve) => {
315+
release = resolve;
316+
});
317+
const run = vi.fn(async (args: string[]) => {
318+
if (args[0] === 'spawn') await released;
319+
return {
320+
stdout:
321+
args[0] === 'spawn'
322+
? `42\t0\tUIKitApplication:${input.options.appBundleId}[launch-a][rb-legacy]`
323+
: JSON.stringify({
324+
devices: { 'com.apple.CoreSimulator.SimRuntime.iOS-26-0': [{ udid: ios.id }] },
325+
}),
326+
stderr: '',
327+
exitCode: 0,
328+
};
329+
});
330+
const runCommand = vi.fn(async () => ({ stdout: 'start-a', stderr: '', exitCode: 0 }));
331+
const fallback = vi.fn(async () => runnerResult());
332+
const source = sourceReturning(bridgeAcquisition());
333+
const presentIosAcquisition = vi.fn(async () => ({
334+
backend: 'xctest' as const,
335+
producer: 'simulator-ax-bridge' as const,
336+
nodes: [{ index: 0, type: 'Application' }],
337+
}));
338+
const hasLiveRunnerSession = vi.fn(async () => false);
339+
const baseHost = platformRuntimeHostFixture();
340+
const route = createAppleSnapshotRoute(
341+
{
342+
...baseHost,
343+
appleApplications: { ...baseHost.appleApplications, hasLiveRunnerSession },
344+
snapshot: { captureSurface: vi.fn(), presentIosAcquisition },
345+
},
346+
{ source, resolveTarget: createSimulatorSnapshotTargetResolver() },
347+
);
348+
vi.useFakeTimers();
349+
try {
350+
await withAppleToolProvider(
351+
createLocalAppleToolProvider({ simctl: { run }, runCommand }),
352+
async () => {
353+
const capture = route.capture(ios, input, signal(), fallback);
354+
await vi.advanceTimersByTimeAsync(4_500);
355+
expect(fallback).not.toHaveBeenCalled();
356+
expect(hasLiveRunnerSession).toHaveBeenCalled();
357+
358+
release();
359+
await vi.advanceTimersByTimeAsync(0);
360+
const result = await capture;
361+
expect(result.producer).toBe('simulator-ax-bridge');
362+
expect(fallback).not.toHaveBeenCalled();
363+
expect(run.mock.calls.filter(([args]) => args[0] === 'spawn')).toHaveLength(1);
364+
},
365+
);
366+
} finally {
367+
vi.useRealTimers();
368+
}
369+
});

‎packages/platform-apple/src/snapshot-route.ts‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export function createAppleSnapshotRoute(
6666
if (!isEligible(device, input)) return await fallback(input);
6767
let target: SimulatorSnapshotTarget;
6868
try {
69-
target = await resolveTarget(device, input.options!.appBundleId!, signal);
69+
target = await resolveTargetForObservation(host, resolveTarget, device, input, signal);
7070
} catch (error) {
7171
signal.throwIfAborted();
7272
emitRouteDiagnostic('target-resolution-failed', device, undefined, error);
@@ -143,6 +143,38 @@ export function createAppleSnapshotRoute(
143143
});
144144
}
145145

146+
/**
147+
* A discovery still in flight is not a failure while no runner can answer instead. The XCTest
148+
* fallback would first wait for a runner start, and #2198 keeps observation off that wait, so the
149+
* capture stays on the single-flight discovery: each turn waits one discovery slice, and the
150+
* discovery's own deadline or the request signal ends the loop. A runner that is already live
151+
* answers at once, so there the fallback remains the cheaper route (#2331).
152+
*/
153+
async function resolveTargetForObservation(
154+
host: PlatformRuntimeHost,
155+
resolveTarget: SimulatorSnapshotTargetResolver,
156+
device: DeviceInfo,
157+
input: CaptureSnapshotInput,
158+
signal: AbortSignal,
159+
): Promise<SimulatorSnapshotTarget> {
160+
const appBundleId = input.options!.appBundleId!;
161+
for (;;) {
162+
try {
163+
return await resolveTarget(device, appBundleId, signal);
164+
} catch (error) {
165+
if (!isDiscoveryPending(error)) throw error;
166+
const execution = { requestId: input.execution?.requestId };
167+
if (await host.appleApplications.hasLiveRunnerSession(device, execution)) throw error;
168+
}
169+
}
170+
}
171+
172+
function isDiscoveryPending(error: unknown): boolean {
173+
return (
174+
error instanceof AppError && error.details?.reason === 'simulator-target-discovery-pending'
175+
);
176+
}
177+
146178
function isEligible(device: DeviceInfo, input: CaptureSnapshotInput): boolean {
147179
return (
148180
device.platform === 'apple' &&

0 commit comments

Comments
 (0)