Skip to content

Commit 142265a

Browse files
committed
fix(ios): keep the discovery slot until both probes settle
Review follow-up. Promise.all rejected as soon as either probe failed and the finally then released the single-flight entry while the other probe was still running, so a failed `simctl list devices` next to a slow `launchctl list` let every later capture start its own launch-job probe. Both probes now settle before the discovery does; the regression test runs three captures against exactly that partial failure and sees one spawn, then the settled discovery reports the runtime failure and frees the slot.
1 parent 2f8824a commit 142265a

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,37 @@ test('one discovery shares a single deadline across its simctl probes and the id
229229
vi.useRealTimers();
230230
}
231231
});
232+
233+
test('a failed runtime probe does not release the slot while the launch-job probe still runs', async () => {
234+
const fixture = targetFixture();
235+
const release = deferredSpawn(fixture);
236+
const respond = fixture.run.getMockImplementation()!;
237+
fixture.run.mockImplementation(async (args: string[], options) =>
238+
args[0] === 'list'
239+
? { stdout: '', stderr: 'simctl list failed', exitCode: 1 }
240+
: await respond(args, options),
241+
);
242+
vi.useFakeTimers();
243+
try {
244+
await withAppleToolProvider(fixture.provider, async () => {
245+
for (let attempt = 0; attempt < 3; attempt += 1) {
246+
const pending = fixture.resolve(ios, app, signal());
247+
const rejected = expect(pending).rejects.toMatchObject({
248+
details: { reason: 'simulator-target-discovery-pending' },
249+
});
250+
await vi.advanceTimersByTimeAsync(1_500);
251+
await rejected;
252+
}
253+
expect(fixture.discoveryCount()).toBe(1);
254+
255+
release();
256+
await vi.advanceTimersByTimeAsync(0);
257+
// The settled discovery reports the runtime failure and frees the slot for a fresh probe.
258+
await expect(fixture.resolve(ios, app, signal())).rejects.toMatchObject({
259+
details: { reason: 'simulator-runtime-probe-failed' },
260+
});
261+
});
262+
} finally {
263+
vi.useRealTimers();
264+
}
265+
});

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,20 @@ async function resolveSimulatorSnapshotTarget(
9595
runtimeByDevice: Map<string, Promise<string>>,
9696
): Promise<SimulatorSnapshotTarget> {
9797
const deadline = Date.now() + TARGET_DISCOVERY_TIMEOUT_MS;
98-
const [jobs, runtime] = await Promise.all([
98+
// Both probes settle before this discovery does: a discovery that gave up on the first
99+
// failure would release its single-flight slot while the other probe still runs, and every
100+
// capture after it would start a probe of its own.
101+
const [jobsProbe, runtimeProbe] = await Promise.allSettled([
99102
runSimctl(device, ['spawn', device.id, 'launchctl', 'list'], {
100103
allowFailure: true,
101104
timeoutMs: remainingMs(deadline),
102105
}),
103106
readSimulatorRuntime(device, runtimeByDevice, deadline),
104107
]);
108+
if (jobsProbe.status === 'rejected') throw jobsProbe.reason;
109+
if (runtimeProbe.status === 'rejected') throw runtimeProbe.reason;
110+
const jobs = jobsProbe.value;
111+
const runtime = runtimeProbe.value;
105112
if (jobs.exitCode !== 0) {
106113
throw targetError('simulator-target-probe-failed', device, appBundleId);
107114
}

0 commit comments

Comments
 (0)