Skip to content

Commit be7dd34

Browse files
committed
fix: keep scoped simulator-set runners out of the shutdown handoff
Review finding on #1011: detach released the XCTestDevices device-set redirect while the runner kept running, and adoption rebuilt the session without it - a custom-set runner could outlive the symlink/lock that its xcodebuild depends on, breaking scoped simulator-set isolation. The redirect's lifetime is bound to the owning session by design, so scoped-set runners simply do not participate in the handoff: shutdown detach skips any session holding a redirect (it goes through the normal dispose-and-restore path), and adoption refuses devices that resolve to a custom simulator set. Default-set sessions - the only ones that never hold a redirect - keep the fast handoff.
1 parent bd9a2b9 commit be7dd34

4 files changed

Lines changed: 57 additions & 3 deletions

File tree

‎src/platforms/apple/core/__tests__/runner-adoption.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,15 @@ test('adoption succeeds for a live, matching, probe-healthy runner', async () =>
133133
expect(readStaleRunnerLease(simulator.id)).toBeNull();
134134
});
135135

136+
test('adoption is skipped for devices in a custom simulator set', async () => {
137+
writeStaleLease();
138+
mockIsProcessAlive.mockReturnValue(true);
139+
140+
const scopedDevice = { ...simulator, simulatorSetPath: '/custom/device-set' };
141+
expect(await tryAdoptRunnerSessionFromLease(scopedDevice, {})).toBeNull();
142+
expect(mockSendRunnerCommandOnce).not.toHaveBeenCalled();
143+
});
144+
136145
test('adoption is skipped when the runner process is dead', async () => {
137146
writeStaleLease();
138147
mockIsProcessAlive.mockReturnValue(false);

‎src/platforms/apple/core/__tests__/runner-session.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ vi.mock('../runner/runner-xctestrun.ts', async () => {
109109

110110
import {
111111
abortAllIosRunnerSessions,
112+
detachIosSimulatorRunnerSessionsForShutdown,
112113
ensureRunnerSession,
113114
executeRunnerCommandWithSession,
114115
getRunnerSessionSnapshot,
@@ -725,6 +726,42 @@ test('runner session does not require Apple developer mode for iOS simulators',
725726
assert.equal(mockRunAppleToolCommand.mock.calls.some(isDevToolsSecurityStatusCall), false);
726727
});
727728

729+
test('shutdown detach hands off default-set simulator runner sessions', async () => {
730+
const device = { ...IOS_SIMULATOR, id: 'runner-session-detach-default-sim' };
731+
// Default simulator set: no XCTestDevices redirect is held.
732+
mockAcquireXcodebuildSimulatorSetRedirect.mockResolvedValue(null);
733+
await ensureRunnerSession(device, {});
734+
735+
const detached = await detachIosSimulatorRunnerSessionsForShutdown();
736+
737+
assert.equal(detached, 1);
738+
assert.equal(getRunnerSessionSnapshot(device.id), null);
739+
const leaseRaw = fs.readFileSync(
740+
path.join(process.env.AGENT_DEVICE_IOS_RUNNER_LEASE_DIR ?? '', `${device.id}.json`),
741+
'utf8',
742+
);
743+
const lease = JSON.parse(leaseRaw) as { ownerToken: string };
744+
assert.match(lease.ownerToken, /^detached-owner-/);
745+
});
746+
747+
test('shutdown detach keeps scoped simulator-set runner sessions for the kill path', async () => {
748+
const device = {
749+
...IOS_SIMULATOR,
750+
id: 'runner-session-detach-scoped-sim',
751+
simulatorSetPath: '/tmp/custom-device-set',
752+
};
753+
await ensureRunnerSession(device, {});
754+
assert.equal(mockAcquireXcodebuildSimulatorSetRedirect.mock.calls.length, 1);
755+
756+
const detached = await detachIosSimulatorRunnerSessionsForShutdown();
757+
758+
// The redirect-holding session must stay for disposal, which restores the
759+
// XCTestDevices symlink; detach never releases the redirect itself.
760+
assert.equal(detached, 0);
761+
assert.ok(getRunnerSessionSnapshot(device.id));
762+
assert.equal(mockRedirectRelease.mock.calls.length, 0);
763+
});
764+
728765
test('runner session startup kills legacy ownerless xcodebuild before launching a new runner', async () => {
729766
const device = { ...IOS_SIMULATOR, id: 'runner-session-startup-stale-sim' };
730767

‎src/platforms/apple/core/runner/runner-adoption.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import path from 'node:path';
2+
import { resolveIosSimulatorDeviceSetPath } from '../../../../utils/device-isolation.ts';
23
import { emitDiagnostic } from '../../../../utils/diagnostics.ts';
34
import { isProcessAlive } from '../../../../utils/process-identity.ts';
45
import type { DeviceInfo } from '../../../../kernel/device.ts';
@@ -40,6 +41,10 @@ export async function tryAdoptRunnerSessionFromLease(
4041
options: { startupTimeoutMs?: number },
4142
): Promise<RunnerSession | null> {
4243
if (device.kind !== 'simulator' || !isIosRunnerDetachEnabled()) return null;
44+
// Custom simulator sets run behind the XCTestDevices redirect, whose
45+
// symlink+lock lifetime is bound to the owning session and cannot be
46+
// carried across daemons; scoped-set runners always restart fresh.
47+
if (resolveIosSimulatorDeviceSetPath(device.simulatorSetPath)) return null;
4348
const lease = readStaleRunnerLease(device.id);
4449
if (!lease) return null;
4550
const runnerPid = lease.runnerPid;

‎src/platforms/apple/core/runner/runner-session.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -471,16 +471,19 @@ export async function detachIosSimulatorRunnerSessionsForShutdown(): Promise<num
471471
let detached = 0;
472472
for (const [deviceId, session] of Array.from(runnerSessions.entries())) {
473473
if (session.device.kind !== 'simulator') continue;
474+
// A held device-set redirect means this runner depends on the global
475+
// XCTestDevices symlink pointing at a custom simulator set for its whole
476+
// lifetime. Handing it off would either restore the symlink under a live
477+
// runner or leak the redirect lock, so scoped-set runners keep the normal
478+
// dispose-and-restore path.
479+
if (session.simulatorSetRedirect) continue;
474480
if (!session.lease || !isRunnerProcessAlive(session.child.pid)) continue;
475481
try {
476482
writeRunnerLease(buildDetachedRunnerLease(session.lease));
477483
} catch {
478484
continue; // Could not mark the handoff; leave it for the kill path.
479485
}
480486
runnerSessions.delete(deviceId);
481-
try {
482-
await session.simulatorSetRedirect?.release();
483-
} catch {}
484487
detached += 1;
485488
emitDiagnostic({
486489
level: 'info',

0 commit comments

Comments
 (0)