Skip to content

Commit 4e78ccc

Browse files
committed
fix(apple): retire wedged runner snapshot bridge
1 parent b42c7c2 commit 4e78ccc

10 files changed

Lines changed: 204 additions & 33 deletions

File tree

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerSnapshotBridge.m‎

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,13 @@ static NSUInteger RunnerSnapshotBridgeBudgetMs(NSDictionary *request)
179179
return MIN(budget.unsignedIntegerValue, kMaximumDurationMs);
180180
}
181181

182-
/// 1 while a read is admitted and has not returned, including a read abandoned past its budget: the
182+
/// A read stays admitted until it returns, including a read abandoned past its budget: the
183183
/// accessibility call it waits on cannot be cancelled, so later reads are refused until it returns
184184
/// rather than queued behind it.
185-
static atomic_int RunnerSnapshotBridgeReadsInFlight = 0;
185+
static BOOL RunnerSnapshotBridgeReadsInFlight = NO;
186+
static NSUInteger RunnerSnapshotBridgeReadSequence = 0;
187+
static NSTimeInterval RunnerSnapshotBridgeAbandonedAt = 0;
188+
static const NSTimeInterval RunnerSnapshotBridgeWedgeSeconds = 5;
186189

187190
static dispatch_queue_t RunnerSnapshotBridgeReaderQueue(void)
188191
{
@@ -208,11 +211,18 @@ @implementation RunnerSnapshotBridgeAnswer
208211
NSUInteger budgetMs,
209212
NSDictionary * (^read)(void))
210213
{
211-
// Admission is one atomic step, because requests arrive on a concurrent queue.
212-
int idle = 0;
213-
if (!atomic_compare_exchange_strong(&RunnerSnapshotBridgeReadsInFlight, &idle, 1)) {
214-
return RunnerSnapshotBridgeFailure(request, @"application_not_responding", @"bridge-read-in-flight",
215-
@"another accessibility read has not returned yet");
214+
NSUInteger sequence;
215+
@synchronized (RunnerSnapshotBridgeAnswer.class) {
216+
if (RunnerSnapshotBridgeReadsInFlight) {
217+
BOOL wedged = RunnerSnapshotBridgeAbandonedAt > 0 &&
218+
NSProcessInfo.processInfo.systemUptime - RunnerSnapshotBridgeAbandonedAt >= RunnerSnapshotBridgeWedgeSeconds;
219+
return RunnerSnapshotBridgeFailure(request, @"application_not_responding",
220+
wedged ? @"bridge-reader-wedged" : @"bridge-read-in-flight",
221+
@"another accessibility read has not returned yet");
222+
}
223+
RunnerSnapshotBridgeReadsInFlight = YES;
224+
RunnerSnapshotBridgeAbandonedAt = 0;
225+
sequence = ++RunnerSnapshotBridgeReadSequence;
216226
}
217227
RunnerSnapshotBridgeAnswer *answer = [RunnerSnapshotBridgeAnswer new];
218228
dispatch_semaphore_t done = dispatch_semaphore_create(0);
@@ -223,10 +233,18 @@ @implementation RunnerSnapshotBridgeAnswer
223233
answer.value = RunnerSnapshotBridgeFailure(request, @"reader_unavailable", @"reader-exception",
224234
exception.reason ?: @"the snapshot reader raised an exception");
225235
}
226-
atomic_store(&RunnerSnapshotBridgeReadsInFlight, 0);
236+
@synchronized (RunnerSnapshotBridgeAnswer.class) {
237+
RunnerSnapshotBridgeReadsInFlight = NO;
238+
RunnerSnapshotBridgeAbandonedAt = 0;
239+
}
227240
dispatch_semaphore_signal(done);
228241
});
229242
if (dispatch_semaphore_wait(done, dispatch_time(DISPATCH_TIME_NOW, (int64_t)budgetMs * NSEC_PER_MSEC)) != 0) {
243+
@synchronized (RunnerSnapshotBridgeAnswer.class) {
244+
if (RunnerSnapshotBridgeReadsInFlight && RunnerSnapshotBridgeReadSequence == sequence) {
245+
RunnerSnapshotBridgeAbandonedAt = NSProcessInfo.processInfo.systemUptime;
246+
}
247+
}
230248
return RunnerSnapshotBridgeFailure(request, @"application_not_responding", @"bridge-read-abandoned",
231249
@"the accessibility read outlived its budget and was abandoned");
232250
}

‎contracts/fixtures/ios-snapshot-bridge-refusals.json‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"description": "Every refusal code the snapshot bridge emits, with the scope the route gives it. A capture-scoped refusal speaks for one capture and keeps the app generation on the bridge; every other code retires the generation. Emitters: the Objective-C reader (apple/snapshot-bridge), the runner-served bridge (RunnerSnapshotBridge.m, RunnerTests+SnapshotBridge.swift), and the TypeScript snapshot source (packages/platform-apple/src/snapshot-source, runner-snapshot-bridge.ts). The TypeScript policy packages/platform-apple/src/snapshot-route-failure-policy.ts mirrors this table. test/repo-guards/snapshot-bridge-refusals.test.ts fails when an emitted code is missing here, when a code here is emitted nowhere, or when the policy disagrees. Classify a new code here by an explicit decision; an unlisted code fails the guard.",
2+
"description": "Every refusal code the snapshot bridge emits, with the scope the route gives it. A capture-scoped refusal speaks for one capture and keeps the app generation on the bridge; generation-scoped codes retire the generation, and runner-session-scoped codes retire the runner bridge. Emitters: the Objective-C reader (apple/snapshot-bridge), the runner-served bridge (RunnerSnapshotBridge.m, RunnerTests+SnapshotBridge.swift), and the TypeScript snapshot source (packages/platform-apple/src/snapshot-source, runner-snapshot-bridge.ts). The TypeScript policy packages/platform-apple/src/snapshot-route-failure-policy.ts mirrors this table. test/repo-guards/snapshot-bridge-refusals.test.ts fails when an emitted code is missing here, when a code here is emitted nowhere, or when the policy disagrees. Classify a new code here by an explicit decision; an unlisted code fails the guard.",
33
"captureScoped": [
44
{
55
"code": "window-coordinate-space-unresolved",
@@ -112,5 +112,6 @@
112112
"unknown",
113113
"user-interaction-invalid",
114114
"verb-not-supported"
115-
]
115+
],
116+
"runnerSessionScoped": ["bridge-reader-wedged"]
116117
}

‎docs/adr/0004-ios-snapshot-backend-strategy.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,11 @@ The runner must not end itself, because it also runs every XCTest command.
811811
The runner abandons such a read (`bridge-read-abandoned`).
812812
The runner admits one read at a time.
813813
Until the admitted read returns, the runner refuses each new read (`bridge-read-in-flight`).
814-
A read that a cancelled capture left running meets that refusal too.
814+
After an abandoned read stays in flight for five seconds, the runner refuses with `bridge-reader-wedged`.
815+
That refusal retires the bridge for the runner session, across app generations.
816+
The route clears its circuit and cached targets when the runner session changes.
817+
The runner does not replace the reader queue, because a second concurrent accessibility read is not proven safe.
818+
A read that a cancelled capture left running meets the in-flight refusal too.
815819
The refusal says only that another read is running, so it has capture scope.
816820
The daemon gives the runner its read budget minus a transport margin.
817821
Thus, the runner abandons a slow read first, and the daemon receives `bridge-read-abandoned`, not a lost reply.

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ export function createAppleRunnerRuntime(options: {
5959
// Every capture of a remote Simulator reaches the runner, through the bridge or through XCTest. So
6060
// the foreground read starts a runner that is not up yet, under the capture's own budget, instead
6161
// of giving up the proof that keeps the capture on the bridge.
62+
let snapshotRoute: SnapshotBridgeRoute | undefined;
63+
let runnerSessionId: string | undefined;
6264
const readForeground = createSystemSurfaceForegroundProbe(async (device, input) => {
6365
if (!options.provider.hasLiveSession(device)) {
6466
try {
@@ -73,9 +75,14 @@ export function createAppleRunnerRuntime(options: {
7375
}
7476
}
7577
const session = readRunnerSessionLiveness(device.id);
76-
return session?.liveness === 'ready' ? { sessionId: session.sessionId } : undefined;
78+
if (session?.liveness !== 'ready') return undefined;
79+
if (runnerSessionId !== undefined && runnerSessionId !== session.sessionId) {
80+
bridge.resetTargets();
81+
snapshotRoute?.resetRunnerSession();
82+
}
83+
runnerSessionId = session.sessionId;
84+
return { sessionId: session.sessionId };
7785
});
78-
let snapshotRoute: SnapshotBridgeRoute | undefined;
7986
/**
8087
* The route outlives every request binding: its circuit, depth hints, and cached target describe
8188
* the app generations this runner has read. The presenter comes with the first binding and is the

‎packages/platform-apple/src/runner-snapshot-bridge.test.ts‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,3 +485,63 @@ test('a runner whose session id cannot be read answers the foreground proof as u
485485
expect(fallbacks).toHaveLength(1);
486486
expect(sent.map((command) => command.command)).not.toContain('appState');
487487
});
488+
489+
test('a wedged runner retires the bridge for the session across captures and app generations', async () => {
490+
const state: { process: RunnerProcess; refuseTarget?: string; refuseDescribe?: string } = {
491+
process: { pid: 41, processStartTime: '1700000000.000001' },
492+
refuseDescribe: 'bridge-read-abandoned',
493+
};
494+
const { provider, sent } = bridgeRunner(state);
495+
const { snapshot, fallbacks } = runtimeSnapshots(withHostStates(provider, 'notRunning'));
496+
await snapshot();
497+
state.refuseTarget = 'bridge-reader-wedged';
498+
await snapshot();
499+
state.process = { pid: 77, processStartTime: '1700000100.000002' };
500+
await snapshot();
501+
await snapshot();
502+
expect(verbs(sent)).toEqual(['target', 'describe', 'target']);
503+
expect(fallbacks).toHaveLength(4);
504+
});
505+
506+
test('a never-returning read costs one wedged refusal after the runner bound', async () => {
507+
const state: {
508+
process: RunnerProcess;
509+
holdNextDescribe?: Promise<void>;
510+
refuseDescribe?: string;
511+
} = {
512+
process: { pid: 41, processStartTime: '1700000000.000001' },
513+
};
514+
const { provider, sent } = bridgeRunner(state);
515+
const { snapshot } = bridgeRoute(provider);
516+
await snapshot();
517+
state.holdNextDescribe = new Promise(() => {});
518+
const controller = new AbortController();
519+
const pending = snapshot(controller.signal);
520+
await new Promise((resolve) => setTimeout(resolve, 0));
521+
controller.abort();
522+
await expect(pending).rejects.toThrow();
523+
await snapshot();
524+
// The fake runner reaches its abandonment bound with the same read still held.
525+
state.refuseDescribe = 'bridge-reader-wedged';
526+
await snapshot();
527+
await snapshot();
528+
await snapshot();
529+
expect(verbs(sent)).toEqual(['target', 'describe', 'describe', 'describe', 'describe']);
530+
});
531+
532+
test('a replacement runner retries a generation retired by the previous runner once', async () => {
533+
const state: { process: RunnerProcess; refuseDescribe?: string } = {
534+
process: { pid: 41, processStartTime: '1700000000.000001' },
535+
refuseDescribe: 'bridge-read-abandoned',
536+
};
537+
const { provider, sent } = bridgeRunner(state);
538+
const { snapshot, presented } = runtimeSnapshots(withHostStates(provider, 'notRunning'));
539+
await snapshot();
540+
registry.session = { sessionId: 'runner-session-2', liveness: 'ready' };
541+
state.refuseDescribe = undefined;
542+
sent.length = 0;
543+
await snapshot();
544+
await snapshot();
545+
expect(verbs(sent)).toEqual(['target', 'describe', 'describe']);
546+
expect(presented).toHaveLength(2);
547+
});

‎packages/platform-apple/src/runner-snapshot-bridge.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const RUNNER_TRANSPORT_MARGIN_MS = 1_000;
4343

4444
export type RunnerSnapshotBridge = Readonly<{
4545
source: SnapshotBridgeSource<RunnerSnapshotBridgeTarget>;
46+
resetTargets(): void;
4647
resolveTarget(
4748
device: DeviceInfo,
4849
appBundleId: string,
@@ -60,6 +61,7 @@ export function createRunnerSnapshotBridge(
6061
options: Readonly<{ device: DeviceInfo; provider: AppleRunnerProvider }>,
6162
): RunnerSnapshotBridge {
6263
const targets = new Map<string, RunnerSnapshotBridgeTarget>();
64+
let targetEpoch = 0;
6365
// The runner admits one bridge read at a time and refuses an overlapping one at once, so it alone
6466
// owns that order: a read a cancelled capture left running costs the next capture its bridge read,
6567
// and the route answers that refusal for that capture only.
@@ -93,6 +95,7 @@ export function createRunnerSnapshotBridge(
9395
if (cached && refresh !== 'refresh') return cached;
9496
// Only a resolved target replaces the cached one: a refresh that fails leaves the generation the
9597
// route planned against.
98+
const epoch = targetEpoch;
9699
const requestId = randomUUID();
97100
const envelope = await send(
98101
appBundleId,
@@ -104,7 +107,7 @@ export function createRunnerSnapshotBridge(
104107
AbortSignal.any([signal, AbortSignal.timeout(TARGET_READ_TIMEOUT_MS)]),
105108
);
106109
const target = readTarget(envelope, { requestId, appBundleId, targetId });
107-
targets.set(targetId, target);
110+
if (epoch === targetEpoch) targets.set(targetId, target);
108111
return target;
109112
};
110113

@@ -158,7 +161,14 @@ export function createRunnerSnapshotBridge(
158161
close: async () => targets.clear(),
159162
},
160163
});
161-
return { source, resolveTarget };
164+
return {
165+
source,
166+
resolveTarget,
167+
resetTargets: () => {
168+
targetEpoch++;
169+
targets.clear();
170+
},
171+
};
162172
}
163173

164174
function parseRunnerEnvelope(data: Record<string, unknown>): SnapshotBridgeEnvelope {
@@ -192,6 +202,8 @@ function readTarget(
192202
!processStartTime
193203
) {
194204
const code = typeof envelope.error_code === 'string' ? envelope.error_code : 'target-invalid';
205+
if (code === 'bridge-reader-wedged')
206+
throw snapshotSourceError('timeout', 'bridge-reader-wedged');
195207
throw new AppError(
196208
'COMMAND_FAILED',
197209
`Unable to resolve the foreground app in the runner (${code}).`,

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,19 @@ const fixture = JSON.parse(
135135
),
136136
'utf8',
137137
),
138-
) as { captureScoped: { code: string; consequence: string }[]; generationScoped: string[] };
138+
) as {
139+
captureScoped: { code: string; consequence: string }[];
140+
generationScoped: string[];
141+
runnerSessionScoped: string[];
142+
};
139143

140144
test('the policy gives each code of the shared refusal table its scope and consequence', () => {
145+
for (const code of fixture.runnerSessionScoped) {
146+
expect(bridgeFailureDisposition({ kind: 'timeout', code })).toEqual({
147+
scope: 'runner-session',
148+
code,
149+
});
150+
}
141151
for (const { code, consequence } of fixture.captureScoped) {
142152
// A preparation still running is classified by its failure kind, not by its code.
143153
const kind = consequence === 'bridge-preparation' ? 'preparing' : 'unsupported';

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export type BridgeFailureDisposition =
3939
consequence: 'system-surface-host';
4040
surfaceHost: SurfaceHostRefusal;
4141
}>
42-
| Readonly<{ scope: 'generation'; code: string }>;
42+
| Readonly<{ scope: 'generation' | 'runner-session'; code: string }>;
4343

4444
/** Every disposition that reaches the generic fallback: all but a surface host refusal. */
4545
export type BridgeFallbackDisposition = Exclude<
@@ -76,6 +76,9 @@ const CAPTURE_SCOPED_REFUSALS: ReadonlyMap<string, CaptureScopedConsequence> = n
7676
* it. Do not widen the default; classify a new known refusal in the table.
7777
*/
7878
export function bridgeFailureDisposition(failure: SnapshotSourceFailure): BridgeFailureDisposition {
79+
if (failure.code === 'bridge-reader-wedged') {
80+
return { scope: 'runner-session', code: failure.code };
81+
}
7982
if (failure.kind === 'preparing') {
8083
return { scope: 'capture', code: failure.code, consequence: 'bridge-preparation' };
8184
}

0 commit comments

Comments
 (0)