Skip to content

Commit f1a6cfd

Browse files
thymikeeclaude
andcommitted
fix(ios): keep dispatched no when a canceled runner route proves nothing was written
Each route of the read-only connect loop replaced its failure with a bare canceled error, so a cancellation whose own failure proved nothing was written (a usbmux connect still in progress, a refused fetch, a simulator curl that could not connect) reported `dispatched: unknown`. Routes now keep `no` when that failure is the proof, and the final simulator post combines the loop's write evidence with its own failure the same way. An abort that interrupts a POST, or the request's shared abort reason, still reads as possibly written. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 8171586 commit f1a6cfd

2 files changed

Lines changed: 130 additions & 6 deletions

File tree

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

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,109 @@ test('waitForRunner discloses whether a canceled command could have been written
131131
);
132132
});
133133

134+
function refusedConnect(): Error {
135+
return Object.assign(new Error('connect ECONNREFUSED 127.0.0.1:8100'), { code: 'ECONNREFUSED' });
136+
}
137+
138+
function canceledWithDispatch(dispatched: string) {
139+
return (error: unknown) =>
140+
isRequestCanceledError(error) && (error as AppError).details?.dispatched === dispatched;
141+
}
142+
143+
test('waitForRunner keeps no when usbmux is canceled while it is still connecting', async () => {
144+
const controller = new AbortController();
145+
mockUsbmuxPostCommand.mockImplementation(async () => {
146+
controller.abort(createRequestCanceledError());
147+
throw createRequestCanceledError({ dispatched: 'no' });
148+
});
149+
150+
await assert.rejects(
151+
() =>
152+
waitForRunner(
153+
xctestIosDevice,
154+
8100,
155+
{ command: 'snapshot' },
156+
undefined,
157+
5_000,
158+
undefined,
159+
controller.signal,
160+
),
161+
canceledWithDispatch('no'),
162+
);
163+
});
164+
165+
test('waitForRunner keeps no when a canceled network attempt was refused', async () => {
166+
const controller = new AbortController();
167+
vi.stubGlobal(
168+
'fetch',
169+
vi.fn(async () => {
170+
controller.abort(createRequestCanceledError());
171+
throw refusedConnect();
172+
}),
173+
);
174+
175+
await assert.rejects(
176+
() =>
177+
waitForRunner(
178+
iosSimulator,
179+
8100,
180+
{ command: 'snapshot' },
181+
undefined,
182+
5_000,
183+
undefined,
184+
controller.signal,
185+
),
186+
canceledWithDispatch('no'),
187+
);
188+
});
189+
190+
test('waitForRunner keeps no when a canceled simulator fallback could not connect', async () => {
191+
vi.stubGlobal('fetch', vi.fn().mockRejectedValue(refusedConnect()));
192+
const controller = new AbortController();
193+
mockRunCmd.mockImplementation(async () => {
194+
controller.abort(createRequestCanceledError());
195+
return { exitCode: 7, stdout: '', stderr: 'curl: (7) Failed to connect' };
196+
});
197+
198+
await assert.rejects(
199+
() =>
200+
waitForRunner(
201+
iosSimulator,
202+
8100,
203+
{ command: 'snapshot' },
204+
undefined,
205+
5_000,
206+
makeReadyRunnerSession(),
207+
controller.signal,
208+
),
209+
canceledWithDispatch('no'),
210+
);
211+
});
212+
213+
test('waitForRunner keeps no when the final simulator post is canceled after a refused connect', async () => {
214+
vi.stubGlobal('fetch', vi.fn().mockRejectedValue(refusedConnect()));
215+
const controller = new AbortController();
216+
mockRunCmd.mockImplementation(async () => {
217+
controller.abort(createRequestCanceledError());
218+
return { exitCode: 7, stdout: '', stderr: 'curl: (7) Failed to connect' };
219+
});
220+
221+
await assert.rejects(
222+
() =>
223+
waitForRunner(
224+
iosSimulator,
225+
8100,
226+
{ command: 'snapshot' },
227+
undefined,
228+
100,
229+
undefined,
230+
controller.signal,
231+
),
232+
canceledWithDispatch('no'),
233+
);
234+
assert.equal(mockRunCmd.mock.calls.length, 1);
235+
});
236+
134237
test('waitForRunner reuses cached physical-device tunnel IP across commands', async () => {
135238
mockUsbmuxPostCommand.mockRejectedValue(usbmuxDeviceUnattachedError());
136239
stubSuccessfulFetch();

‎packages/platform-apple/src/runner/runner-startup-transport.ts‎

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,9 @@ export async function waitForRunner(
103103
if (!isRunnerCommandProvablyUnwritten(err)) commandMayHaveBeenWritten = true;
104104
},
105105
}).catch((error: unknown) => {
106-
// A cancellation that interrupts an attempt may land after its POST was written.
107-
if (isRequestCanceledError(error)) commandMayHaveBeenWritten = true;
106+
if (isRequestCanceledError(error) && !isRunnerCommandProvablyUnwritten(error)) {
107+
commandMayHaveBeenWritten = true;
108+
}
108109
throw error;
109110
});
110111
if (response) return response;
@@ -158,7 +159,11 @@ export async function waitForRunner(
158159
remainingMs,
159160
signal,
160161
).catch((error: unknown) => {
161-
if (signal?.aborted || isRequestCanceledError(error)) throw canceledRunnerConnect(true);
162+
if (signal?.aborted || isRequestCanceledError(error)) {
163+
throw canceledRunnerConnect(
164+
commandMayHaveBeenWritten || !isProvablyUnwrittenBeforeCancel(error, signal),
165+
);
166+
}
162167
throw withRunnerWriteEvidence(error, commandMayHaveBeenWritten);
163168
});
164169
return new Response(simResponse.body, { status: simResponse.status });
@@ -186,6 +191,22 @@ function canceledRunnerConnect(commandMayHaveBeenWritten: boolean): AppError {
186191
return createRequestCanceledError({ dispatched: commandMayHaveBeenWritten ? 'unknown' : 'no' });
187192
}
188193

194+
/**
195+
* The cancellation one route reports. It keeps `dispatched: no` only when the failure it replaces
196+
* proves nothing was written (a refused connect, or a cancel seen before the POST); an abort that
197+
* interrupted the POST itself proves nothing, so the loop reads it as possibly written.
198+
*/
199+
function canceledRunnerAttempt(error: unknown, signal: AbortSignal | undefined): AppError {
200+
return createRequestCanceledError(
201+
isProvablyUnwrittenBeforeCancel(error, signal) ? { dispatched: 'no' } : {},
202+
);
203+
}
204+
205+
/** The request's shared abort reason is never evidence about one send. */
206+
function isProvablyUnwrittenBeforeCancel(error: unknown, signal: AbortSignal | undefined): boolean {
207+
return error !== signal?.reason && isRunnerCommandProvablyUnwritten(error);
208+
}
209+
189210
/**
190211
* An earlier attempt that may have written the command may also have executed it, so the loop's
191212
* failure is `unknown` whatever a later refused attempt stamped.
@@ -400,7 +421,7 @@ async function tryRunnerRoute(
400421
);
401422
} catch (error) {
402423
if (params.signal?.aborted || isRequestCanceledError(error)) {
403-
throw createRequestCanceledError();
424+
throw canceledRunnerAttempt(error, params.signal);
404425
}
405426
if (isUsbmuxDeviceUnattachedError(error)) {
406427
if (!canFallBackFromUsbmux(device, error)) {
@@ -453,7 +474,7 @@ async function tryRunnerEndpoints(
453474
);
454475
} catch (error) {
455476
if (signal?.aborted || isRequestCanceledError(error)) {
456-
throw createRequestCanceledError();
477+
throw canceledRunnerAttempt(error, signal);
457478
}
458479
onError(endpoint, error);
459480
}
@@ -479,7 +500,7 @@ async function tryRunnerSimulatorEndpoint(
479500
return new Response(simResponse.body, { status: simResponse.status });
480501
} catch (error) {
481502
if (signal?.aborted || isRequestCanceledError(error)) {
482-
throw createRequestCanceledError();
503+
throw canceledRunnerAttempt(error, signal);
483504
}
484505
onError(error);
485506
return null;

0 commit comments

Comments
 (0)