Skip to content

Commit 029c9e9

Browse files
committed
refactor(ios-runner): own the close release decision in the runner module (#2552)
Collapse the retain-vs-stop close decision into the module that owns the occupancy bit. Close now calls one intent, releaseRunnerOnClose(deviceId, { retain }), instead of asking stopRunnerSessionIfBusy for a boolean and re-branching in lifecycle. The runner module keeps warm reuse for an idle runner and stops one still draining, so the busy fact never crosses the package boundary, the fire-and-forget scheduling path is gone, and the close route stays awaited.
1 parent 44f78a9 commit 029c9e9

18 files changed

Lines changed: 120 additions & 153 deletions

‎.fallowrc.json‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@
9595
"exports": [
9696
"detachIosSimulatorRunnerSessionsForShutdown",
9797
"hasLiveIosRunnerSession",
98+
"releaseIosRunnerOnClose",
9899
"releaseSpeculativeIosRunnerSessionFor",
99-
"stopAllIosRunnerSessions",
100-
"stopIosRunnerSessionIfBusy"
100+
"stopAllIosRunnerSessions"
101101
]
102102
},
103103
{
@@ -161,7 +161,6 @@
161161
"resolveRunnerAppBundleId",
162162
"detachIosSimulatorRunnerSessionsForShutdown",
163163
"getRunnerSessionSnapshot",
164-
"scheduleIosRunnerIdleStop",
165164
"stopIosRunnerSession",
166165
"stopAllIosRunnerSessions",
167166
"readStaleRunnerLease",

‎packages/contracts/src/application-lifecycle-runtime.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -316,12 +316,12 @@ export type AppleApplicationTools = Readonly<{
316316
execution: Readonly<{ requestId?: string }>,
317317
): Promise<boolean>;
318318
/**
319-
* Stops this device's runner only if its last exchange reported main-thread work still draining,
320-
* and returns whether it did. `close` calls it before deciding to retain a runner, so a stalled
321-
* runner is never pooled back out to the next `open` (#2552).
319+
* Releases this device's runner at session close. When `retain` is set and the runner is idle it
320+
* keeps warm reuse under an idle-stop timer; otherwise it stops now. A runner whose last exchange
321+
* reported main-thread work still draining is never retained, so a stalled process is not pooled
322+
* back out to the next `open` (#2552). Awaited so `close` returns only once the lease is gone.
322323
*/
323-
stopRunnerSessionIfBusy(deviceId: string): Promise<boolean>;
324-
scheduleRunnerIdleStop(deviceId: string): void;
324+
releaseRunnerOnClose(deviceId: string, options: Readonly<{ retain: boolean }>): Promise<void>;
325325
prepareRunner(
326326
device: DeviceInfo,
327327
input: PrepareAppleRunnerInput,

‎packages/platform-apple/src/core/runner-client.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,10 @@ export const detachIosSimulatorRunnerSessionsForShutdown: AppleRunnerClient['det
3737
client.detachIosSimulatorRunnerSessionsForShutdown;
3838
export const getRunnerSessionSnapshot: AppleRunnerClient['getRunnerSessionSnapshot'] =
3939
client.getRunnerSessionSnapshot;
40-
export const scheduleIosRunnerIdleStop: AppleRunnerClient['scheduleIosRunnerIdleStop'] =
41-
client.scheduleIosRunnerIdleStop;
40+
export const releaseIosRunnerOnClose: AppleRunnerClient['releaseIosRunnerOnClose'] =
41+
client.releaseIosRunnerOnClose;
4242
export const stopIosRunnerSession: AppleRunnerClient['stopIosRunnerSession'] =
4343
client.stopIosRunnerSession;
44-
export const stopIosRunnerSessionIfBusy: AppleRunnerClient['stopIosRunnerSessionIfBusy'] =
45-
client.stopIosRunnerSessionIfBusy;
4644
export const stopAllIosRunnerSessions: AppleRunnerClient['stopAllIosRunnerSessions'] =
4745
client.stopAllIosRunnerSessions;
4846
export const runApplePressSeries: AppleRunnerClient['runApplePressSeries'] =

‎packages/platform-apple/src/lifecycle.test.ts‎

Lines changed: 29 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -244,47 +244,41 @@ test('discards a retained physical iOS runner when relaunch fails and preserves
244244
expect(notifyRunnerAppRelaunched).not.toHaveBeenCalled();
245245
});
246246

247-
test('close finalization stops a busy retained runner instead of pooling it back (#2552)', async () => {
248-
const signal = new AbortController().signal;
249-
const baseHost = platformRuntimeHostFixture();
250-
const stopRunnerSessionIfBusy = vi.fn(async () => true);
251-
const stopRunnerSession = vi.fn(async () => {});
252-
const scheduleRunnerIdleStop = vi.fn();
253-
const host = {
254-
...baseHost,
255-
appleApplications: {
256-
...baseHost.appleApplications,
257-
stopRunnerSessionIfBusy,
258-
stopRunnerSession,
259-
scheduleRunnerIdleStop,
260-
},
261-
} as unknown as PlatformRuntimeHost;
262-
const lifecycle = bindAppleApplicationLifecycle({ host, device, signal });
247+
test.each([true, false])(
248+
'close finalization delegates the runner release to the runner module with retain=%s (#2552)',
249+
async (retainRunner) => {
250+
const signal = new AbortController().signal;
251+
const baseHost = platformRuntimeHostFixture();
252+
const releaseRunnerOnClose = vi.fn(async () => {});
253+
const dismissCloseAlerts = vi.fn(async () => {});
254+
const host = {
255+
...baseHost,
256+
appleApplications: {
257+
...baseHost.appleApplications,
258+
releaseRunnerOnClose,
259+
dismissCloseAlerts,
260+
},
261+
} as unknown as PlatformRuntimeHost;
262+
const lifecycle = bindAppleApplicationLifecycle({ host, device, signal });
263263

264-
await lifecycle.finalizeApplicationClose({
265-
surface: 'app',
266-
retainRunner: true,
267-
stateDir: '/tmp',
268-
});
264+
await lifecycle.finalizeApplicationClose({ surface: 'app', retainRunner, stateDir: '/tmp' });
269265

270-
expect(stopRunnerSessionIfBusy).toHaveBeenCalledWith(device.id);
271-
expect(scheduleRunnerIdleStop).not.toHaveBeenCalled();
272-
expect(stopRunnerSession).not.toHaveBeenCalled();
273-
});
266+
expect(releaseRunnerOnClose).toHaveBeenCalledWith(device.id, { retain: retainRunner });
267+
expect(dismissCloseAlerts).toHaveBeenCalled();
268+
},
269+
);
274270

275-
test('close finalization retains an idle runner and schedules its idle stop', async () => {
271+
test('daemon-shutdown finalization dismisses alerts and defers the runner release to the gateway (#2552)', async () => {
276272
const signal = new AbortController().signal;
277273
const baseHost = platformRuntimeHostFixture();
278-
const stopRunnerSessionIfBusy = vi.fn(async () => false);
279-
const stopRunnerSession = vi.fn(async () => {});
280-
const scheduleRunnerIdleStop = vi.fn();
274+
const releaseRunnerOnClose = vi.fn(async () => {});
275+
const dismissCloseAlerts = vi.fn(async () => {});
281276
const host = {
282277
...baseHost,
283278
appleApplications: {
284279
...baseHost.appleApplications,
285-
stopRunnerSessionIfBusy,
286-
stopRunnerSession,
287-
scheduleRunnerIdleStop,
280+
releaseRunnerOnClose,
281+
dismissCloseAlerts,
288282
},
289283
} as unknown as PlatformRuntimeHost;
290284
const lifecycle = bindAppleApplicationLifecycle({ host, device, signal });
@@ -293,36 +287,11 @@ test('close finalization retains an idle runner and schedules its idle stop', as
293287
surface: 'app',
294288
retainRunner: true,
295289
stateDir: '/tmp',
290+
daemonShutdown: true,
296291
});
297292

298-
expect(stopRunnerSessionIfBusy).toHaveBeenCalledWith(device.id);
299-
expect(scheduleRunnerIdleStop).toHaveBeenCalledWith(device.id);
300-
expect(stopRunnerSession).not.toHaveBeenCalled();
301-
});
302-
303-
test('close finalization stops a non-retained runner without the busy probe', async () => {
304-
const signal = new AbortController().signal;
305-
const baseHost = platformRuntimeHostFixture();
306-
const stopRunnerSessionIfBusy = vi.fn(async () => true);
307-
const stopRunnerSession = vi.fn(async () => {});
308-
const host = {
309-
...baseHost,
310-
appleApplications: {
311-
...baseHost.appleApplications,
312-
stopRunnerSessionIfBusy,
313-
stopRunnerSession,
314-
},
315-
} as unknown as PlatformRuntimeHost;
316-
const lifecycle = bindAppleApplicationLifecycle({ host, device, signal });
317-
318-
await lifecycle.finalizeApplicationClose({
319-
surface: 'app',
320-
retainRunner: false,
321-
stateDir: '/tmp',
322-
});
323-
324-
expect(stopRunnerSessionIfBusy).not.toHaveBeenCalled();
325-
expect(stopRunnerSession).toHaveBeenCalledWith(device.id);
293+
expect(releaseRunnerOnClose).not.toHaveBeenCalled();
294+
expect(dismissCloseAlerts).toHaveBeenCalled();
326295
});
327296

328297
test('prepare shares one startup budget across the Simulator boot and the runner preparation', async () => {

‎packages/platform-apple/src/lifecycle.ts‎

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -329,18 +329,10 @@ async function finalizeAppleApplicationClose(
329329
if (input.daemonShutdown) {
330330
await host.appleApplications.dismissCloseAlerts(device, input).catch(() => {});
331331
} else {
332-
// A runner whose last exchange reported main-thread work still draining refuses every command
333-
// until it drains or wedges, so retaining it would hand the same stalled process to the next
334-
// `open` (#2552). Stop it now, awaited and lease-released, before the retain-vs-stop decision.
335-
const stoppedStalledRunner =
336-
input.retainRunner && (await host.appleApplications.stopRunnerSessionIfBusy(device.id));
337-
if (!stoppedStalledRunner) {
338-
if (input.retainRunner) {
339-
host.appleApplications.scheduleRunnerIdleStop(device.id);
340-
} else {
341-
await host.appleApplications.stopRunnerSession(device.id);
342-
}
343-
}
332+
// The runner module owns the retain-vs-stop decision: it keeps warm reuse for an idle runner and
333+
// stops one whose last exchange reported main-thread work still draining, so `close` never pools
334+
// a stalled process back out to the next `open` (#2552). Awaited so the lease is released first.
335+
await host.appleApplications.releaseRunnerOnClose(device.id, { retain: input.retainRunner });
344336
await host.appleApplications.dismissCloseAlerts(device, input).catch(() => {});
345337
}
346338
const shutdown =

‎packages/platform-apple/src/runner-operations-facade.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@ export {
88
prewarmAppleRunnerCache,
99
prewarmIosRunnerSession,
1010
readStaleRunnerLease,
11+
releaseIosRunnerOnClose,
1112
releaseSpeculativeIosRunnerSessionFor,
1213
resolveRunnerAppBundleId,
1314
runAppleRunnerCommand,
14-
scheduleIosRunnerIdleStop,
1515
stopAllIosRunnerSessions,
1616
stopIosRunnerSession,
17-
stopIosRunnerSessionIfBusy,
1817
verifyLeaseRunnerPidIdentity,
1918
} from './core/runner-client.ts';
2019
export { queryAppleRunnerSelector } from './core/runner-selector-query.ts';

‎packages/platform-apple/src/runner/__tests__/runner-session-close.test.ts‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ import {
106106
ensureRunnerSession,
107107
executeRunnerCommandWithSession,
108108
getRunnerSessionSnapshot,
109-
stopIosRunnerSessionIfBusy,
109+
releaseIosRunnerOnClose,
110110
} from '../runner-session.ts';
111111

112112
// Test-only stand-in for the daemon's runtime lease-owner-state-dir setter (root-only; the package
@@ -287,17 +287,22 @@ test('a served non-busy error clears a stale busy report so close keeps a draine
287287
assert.equal(session.runnerMainThreadBusy, false);
288288
});
289289

290-
test('stopIosRunnerSessionIfBusy leaves an idle runner and stops a busy one so the next open boots fresh (#2552)', async () => {
291-
const device = { ...IOS_SIMULATOR, id: 'runner-session-stop-if-busy-sim' };
290+
test('releaseIosRunnerOnClose retains an idle runner, disposes a busy one, and the reopen boots fresh (#2552)', async () => {
291+
const device = { ...IOS_SIMULATOR, id: 'runner-session-release-on-close-sim' };
292292
const session = await ensureRunnerSession(device, {});
293293

294-
assert.equal(await stopIosRunnerSessionIfBusy(device.id), false);
294+
// Idle + retain: warm reuse, same session comes back for the next open.
295+
await releaseIosRunnerOnClose(device.id, { retain: true });
295296
assert.ok(getRunnerSessionSnapshot(device.id));
297+
assert.equal((await ensureRunnerSession(device, {})).sessionId, session.sessionId);
296298

299+
// Busy + retain: the stalled runner is disposed, so the next open boots a clean one.
297300
session.runnerMainThreadBusy = true;
298-
assert.equal(await stopIosRunnerSessionIfBusy(device.id), true);
301+
await releaseIosRunnerOnClose(device.id, { retain: true });
299302
assert.equal(getRunnerSessionSnapshot(device.id), null);
303+
assert.notEqual((await ensureRunnerSession(device, {})).sessionId, session.sessionId);
300304

301-
const reopened = await ensureRunnerSession(device, {});
302-
assert.notEqual(reopened.sessionId, session.sessionId);
305+
// Non-retained close: stops regardless of occupancy.
306+
await releaseIosRunnerOnClose(device.id, { retain: false });
307+
assert.equal(getRunnerSessionSnapshot(device.id), null);
303308
});

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ import { runApplePressSeries } from './runner-sequence.ts';
1919
import {
2020
detachIosSimulatorRunnerSessionsForShutdown,
2121
getRunnerSessionSnapshot,
22-
scheduleIosRunnerIdleStop,
22+
releaseIosRunnerOnClose,
2323
stopAllIosRunnerSessions,
2424
stopIosRunnerSession,
25-
stopIosRunnerSessionIfBusy,
2625
} from './runner-session.ts';
2726
import { hasCachedAppleRunnerArtifact, resolveRunnerAppBundleId } from './runner-xctestrun.ts';
2827

@@ -45,9 +44,8 @@ export type AppleRunnerClient = {
4544
hasCachedAppleRunnerArtifact: typeof hasCachedAppleRunnerArtifact;
4645
detachIosSimulatorRunnerSessionsForShutdown: typeof detachIosSimulatorRunnerSessionsForShutdown;
4746
getRunnerSessionSnapshot: typeof getRunnerSessionSnapshot;
48-
scheduleIosRunnerIdleStop: typeof scheduleIosRunnerIdleStop;
47+
releaseIosRunnerOnClose: typeof releaseIosRunnerOnClose;
4948
stopIosRunnerSession: typeof stopIosRunnerSession;
50-
stopIosRunnerSessionIfBusy: typeof stopIosRunnerSessionIfBusy;
5149
stopAllIosRunnerSessions: typeof stopAllIosRunnerSessions;
5250
runApplePressSeries: typeof runApplePressSeries;
5351
cleanupRunnerLeasesForOwner: typeof cleanupRunnerLeasesForOwner;
@@ -77,9 +75,8 @@ export function createAppleRunnerClient(host: AppleRunnerHost): AppleRunnerClien
7775
hasCachedAppleRunnerArtifact,
7876
detachIosSimulatorRunnerSessionsForShutdown,
7977
getRunnerSessionSnapshot,
80-
scheduleIosRunnerIdleStop,
78+
releaseIosRunnerOnClose,
8179
stopIosRunnerSession,
82-
stopIosRunnerSessionIfBusy,
8380
stopAllIosRunnerSessions,
8481
runApplePressSeries,
8582
cleanupRunnerLeasesForOwner,

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

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -581,20 +581,29 @@ export async function stopIosRunnerSession(deviceId: string): Promise<void> {
581581
}
582582

583583
/**
584-
* Stops a retained runner whose last exchange reported main-thread work still draining, and reports
585-
* whether it did. Retaining such a runner only hands the same stalled process to the next `open`, so
586-
* `close` stops it instead of scheduling an idle stop (#2552). Returns false without touching the
587-
* runner when it is idle, so the caller's retain-vs-stop decision stays atomic (no re-check window).
584+
* Releases a runner at session close, preferring warm reuse only when the runner is actually
585+
* reusable. A non-retained close, or a retained close over a runner whose last exchange reported
586+
* main-thread work still draining, stops it now: a busy runner refuses every command until it drains
587+
* or wedges, so pooling it back hands the same stalled process to the next `open` (#2552). An idle
588+
* retained runner keeps warm reuse via the idle-stop timer. The decision is owned here because the
589+
* occupancy fact lives on the session, and awaited so `close` returns only once the lease is gone.
588590
*/
589-
export async function stopIosRunnerSessionIfBusy(deviceId: string): Promise<boolean> {
590-
if (runnerSessions.get(deviceId)?.runnerMainThreadBusy !== true) return false;
591-
emitDiagnostic({
592-
level: 'info',
593-
phase: 'ios_runner_retain_skipped_busy',
594-
data: { deviceId },
595-
});
591+
export async function releaseIosRunnerOnClose(
592+
deviceId: string,
593+
options: { retain: boolean },
594+
): Promise<void> {
595+
if (options.retain && runnerSessions.get(deviceId)?.runnerMainThreadBusy !== true) {
596+
scheduleIosRunnerIdleStop(deviceId);
597+
return;
598+
}
599+
if (options.retain) {
600+
emitDiagnostic({
601+
level: 'info',
602+
phase: 'ios_runner_retain_skipped_busy',
603+
data: { deviceId },
604+
});
605+
}
596606
await stopIosRunnerSession(deviceId);
597-
return true;
598607
}
599608

600609
export async function abortAllIosRunnerSessions(): Promise<void> {

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ export function platformRuntimeHostFixture(): PlatformRuntimeHost {
4444
stopRunnerSession: async () => {},
4545
hasLiveRunnerSession: async () => false,
4646
releaseSpeculativeRunner: async () => false,
47-
scheduleRunnerIdleStop: () => {},
48-
stopRunnerSessionIfBusy: async () => false,
47+
releaseRunnerOnClose: async () => {},
4948
prepareRunner: async () => ({ runner: {}, connectMs: 0, healthCheckMs: 0 }),
5049
applyRuntimeHints: async () => {},
5150
clearRuntimeHints: async () => {},

0 commit comments

Comments
 (0)