Skip to content

Commit 10bc49d

Browse files
thymikeeclaude
andcommitted
fix(ios): check the bridge circuit before reading surface host state
Review follow-up. With a lingering surface host and a generation whose circuit is already disabled, every capture paid the runner's host state reads for a capture that goes to the runner anyway. Resolve the target and check its circuit first, and reuse that target for the bridge capture. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 4d5422f commit 10bc49d

2 files changed

Lines changed: 63 additions & 27 deletions

File tree

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

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,7 @@ test.for([
103103
const source = sourceReturning(bridgeAcquisition());
104104
const fallback = vi.fn(async () => runnerResult());
105105
const systemSurfaceForeground = vi.fn(async () => foreground);
106-
const presentIosAcquisition = vi.fn(async () => ({
107-
backend: 'xctest' as const,
108-
producer: 'simulator-ax-bridge' as const,
109-
nodes: [{ index: 0, type: 'Application' }],
110-
}));
111-
const host = {
112-
...platformRuntimeHostFixture(),
113-
snapshot: { captureSurface: vi.fn(), presentIosAcquisition },
114-
};
115-
const route = createAppleSnapshotRoute(host, {
106+
const route = createAppleSnapshotRoute(platformRuntimeHostFixture(), {
116107
source,
117108
resolveTarget: vi.fn(async () => target),
118109
systemSurfacePresent: async () => presentSurface,
@@ -126,10 +117,33 @@ test.for([
126117
expect.objectContaining({ requestId: 'req-1' }),
127118
);
128119
expect(source.acquire).toHaveBeenCalledTimes(bridge ? 1 : 0);
129-
expect(fallback).toHaveBeenCalledTimes(bridge ? 0 : 1);
130120
},
131121
);
132122

123+
test('a generation the bridge no longer serves never pays the surface host foreground reads', async () => {
124+
const source = sourceReturning({
125+
stage: 'failed',
126+
failure: { kind: 'transport-failure', code: 'bridge-disconnected' },
127+
});
128+
let presence: 'absent' | typeof presentSurface = 'absent';
129+
const systemSurfaceForeground = vi.fn(async () => 'background' as const);
130+
const fallback = vi.fn(async () => runnerResult());
131+
const route = createAppleSnapshotRoute(platformRuntimeHostFixture(), {
132+
source,
133+
resolveTarget: vi.fn(async () => target),
134+
systemSurfacePresent: async () => presence,
135+
systemSurfaceForeground,
136+
});
137+
138+
await route.capture(ios, input, signal(), fallback);
139+
presence = presentSurface;
140+
await route.capture(ios, input, signal(), fallback);
141+
142+
expect(source.acquire).toHaveBeenCalledOnce();
143+
expect(systemSurfaceForeground).not.toHaveBeenCalled();
144+
expect(fallback).toHaveBeenCalledTimes(2);
145+
});
146+
133147
// The structural guarantee every comparison site downstream relies on (#2438): a surface capture is
134148
// lineaged to the HOST, not to the app (`target.targetId` here), so its comparison key cannot equal
135149
// an app capture's and no comparison site needs its own surface check.

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

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,22 @@ export function createAppleSnapshotRoute(
140140
[unknownGenerationResidue()],
141141
);
142142
}
143-
if (
144-
surfacePresence !== 'absent' &&
145-
!(await isEverySurfaceHostInBackground(device, input, signal, systemSurfaceForeground))
146-
) {
147-
return await runSurfaceFallback(device, input, fallback, surfacePresence.host.bundleId);
143+
let lingeringHostTarget: SimulatorSnapshotTarget | undefined;
144+
if (surfacePresence !== 'absent') {
145+
lingeringHostTarget = await bridgeTargetWhileSurfaceHostRuns(device, input, signal, {
146+
resolveTarget,
147+
isBridgeDisabled,
148+
probe: systemSurfaceForeground,
149+
});
150+
if (!lingeringHostTarget) {
151+
return await runSurfaceFallback(device, input, fallback, surfacePresence.host.bundleId);
152+
}
148153
}
149154
let target: SimulatorSnapshotTarget;
150155
try {
151-
target = await resolveTargetForObservation(host, resolveTarget, device, input, signal);
156+
target =
157+
lingeringHostTarget ??
158+
(await resolveTargetForObservation(host, resolveTarget, device, input, signal));
152159
} catch (error) {
153160
rethrowIfResolutionCancelled(signal, error);
154161
emitRouteDiagnostic('target-resolution-failed', device, undefined, error);
@@ -233,25 +240,40 @@ export function createAppleSnapshotRoute(
233240
}
234241

235242
/**
236-
* Whether the live runner reports every registered host off screen, which is what lets a capture
237-
* whose host process is only lingering keep the bridge. Only a positive read decides: anything the
238-
* probe could not establish keeps the runner route that serves a presented surface.
243+
* The bridge target for a capture while a registered surface host process runs, or undefined when the
244+
* capture keeps the runner route that serves a presented surface. A host process outlives what it
245+
* presented, so only the live runner reading every registered host off screen admits the bridge, and
246+
* only for a generation the bridge still serves: a disabled one would pay the runner reads for a
247+
* capture that goes to the runner anyway. The target is resolved once, without waiting on a pending
248+
* discovery; any failure keeps the runner route, as before this refinement.
239249
*/
240-
async function isEverySurfaceHostInBackground(
250+
async function bridgeTargetWhileSurfaceHostRuns(
241251
device: DeviceInfo,
242252
input: CaptureSnapshotInput,
243253
signal: AbortSignal,
244-
probe: SystemSurfaceForegroundProbe,
245-
): Promise<boolean> {
254+
deps: Readonly<{
255+
resolveTarget: SimulatorSnapshotTargetResolver;
256+
isBridgeDisabled: (target: SimulatorSnapshotTarget) => boolean;
257+
probe: SystemSurfaceForegroundProbe;
258+
}>,
259+
): Promise<SimulatorSnapshotTarget | undefined> {
260+
let target: SimulatorSnapshotTarget;
261+
try {
262+
target = await deps.resolveTarget(device, input.options!.appBundleId!, signal);
263+
} catch (error) {
264+
rethrowIfResolutionCancelled(signal, error);
265+
return undefined;
266+
}
267+
if (deps.isBridgeDisabled(target)) return undefined;
246268
const requestId = input.execution?.requestId;
247-
const foreground = await probe(device, { signal, ...(requestId ? { requestId } : {}) });
248-
if (foreground !== 'background') return false;
269+
const foreground = await deps.probe(device, { signal, ...(requestId ? { requestId } : {}) });
270+
if (foreground !== 'background') return undefined;
249271
emitDiagnostic({
250272
level: 'debug',
251273
phase: 'ios_snapshot_route_surface_host_background',
252-
data: { deviceId: device.id },
274+
data: { deviceId: device.id, generation: target.generation },
253275
});
254-
return true;
276+
return target;
255277
}
256278

257279
/**

0 commit comments

Comments
 (0)