Skip to content

Commit 3e128b2

Browse files
committed
fix: re-verify lease pid identity before each signal and during legacy adoption
The stale-lease cleanup verified the runner pid once and reused it for both SIGTERM and SIGKILL; the runner usually dies on the SIGTERM, and the pid can be recycled while the awaited xcodebuild sweep runs before the escalation. Each signal now resolves a freshly verified pid. Adoption skipped identity verification entirely for legacy leases without a recorded runnerStartTime, then re-stamped the lease with the live pid's start time — laundering a recycled pid into a strongly verified lease that disposal would later kill. Adoption now shares the disposal path's verification, including the runner-shaped command-line fallback for legacy leases.
1 parent 0dc70a3 commit 3e128b2

4 files changed

Lines changed: 83 additions & 15 deletions

File tree

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
tryAdoptRunnerSessionFromLease,
1616
} from '../runner/runner-adoption.ts';
1717
import { sendRunnerCommandOnce } from '../runner/runner-transport.ts';
18-
import { isProcessAlive } from '../../../../utils/host-process.ts';
18+
import { isProcessAlive, readProcessCommand } from '../../../../utils/host-process.ts';
1919
import { mkdtempForTestSync } from '../../../../__tests__/test-utils/tmp-dir.ts';
2020

2121
vi.mock('../runner/runner-transport.ts', async (importOriginal) => {
@@ -27,6 +27,7 @@ vi.mock('../../../../utils/host-process.ts', async (importOriginal) => {
2727
return {
2828
...actual,
2929
isProcessAlive: vi.fn(() => false),
30+
readProcessCommand: vi.fn(() => null),
3031
readProcessStartTime: vi.fn(() => 'test-process-start'),
3132
};
3233
});
@@ -41,6 +42,7 @@ vi.mock('../runner/runner-xctestrun.ts', async (importOriginal) => {
4142

4243
const mockSendRunnerCommandOnce = vi.mocked(sendRunnerCommandOnce);
4344
const mockIsProcessAlive = vi.mocked(isProcessAlive);
45+
const mockReadProcessCommand = vi.mocked(readProcessCommand);
4446

4547
const simulator: DeviceInfo = {
4648
platform: 'apple',
@@ -82,6 +84,8 @@ beforeEach(() => {
8284
mockSendRunnerCommandOnce.mockReset();
8385
mockIsProcessAlive.mockReset();
8486
mockIsProcessAlive.mockReturnValue(false);
87+
mockReadProcessCommand.mockReset();
88+
mockReadProcessCommand.mockReturnValue(null);
8589
delete process.env.AGENT_DEVICE_IOS_RUNNER_DETACH;
8690
});
8791

@@ -148,6 +152,34 @@ test('adoption is skipped for a recycled runner pid (start time mismatch)', asyn
148152
expect(mockSendRunnerCommandOnce).not.toHaveBeenCalled();
149153
});
150154

155+
test('adoption is skipped for a legacy lease whose live pid is not runner-shaped', async () => {
156+
// Leases written before `runnerStartTime` existed carry no start time; the
157+
// only identity evidence left is the command line. An unverified pid must
158+
// not be adopted — adoption re-stamps the lease with the live pid's start
159+
// time, so a recycled pid would be laundered into a strongly-verified lease
160+
// that disposal later kills.
161+
writeStaleLease({ runnerStartTime: null });
162+
mockIsProcessAlive.mockReturnValue(true);
163+
mockReadProcessCommand.mockReturnValue('node /usr/local/bin/opencode run --model gpt-high');
164+
165+
expect(await tryAdoptRunnerSessionFromLease(simulator, {})).toBeNull();
166+
expect(mockSendRunnerCommandOnce).not.toHaveBeenCalled();
167+
});
168+
169+
test('adoption accepts a legacy lease whose live pid is runner-shaped', async () => {
170+
writeStaleLease({ runnerStartTime: null });
171+
mockIsProcessAlive.mockReturnValue(true);
172+
mockReadProcessCommand.mockReturnValue(
173+
'xcodebuild test-without-building -xctestrun /tmp/AgentDeviceRunner.env.session-x.xctestrun',
174+
);
175+
mockSendRunnerCommandOnce.mockResolvedValue(new Response(JSON.stringify({ ok: true })));
176+
177+
const session = await tryAdoptRunnerSessionFromLease(simulator, {});
178+
179+
expect(session?.ready).toBe(true);
180+
expect(session?.child.pid).toBe(424242);
181+
});
182+
151183
test('adoption is skipped for devices in a custom simulator set', async () => {
152184
writeStaleLease();
153185
mockIsProcessAlive.mockReturnValue(true);

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,6 +1189,26 @@ test('stale-lease cleanup signals the runner pid when its start time still match
11891189
]);
11901190
});
11911191

1192+
test('stale-lease cleanup re-verifies the pid before the SIGKILL escalation', async () => {
1193+
const device = { ...IOS_SIMULATOR, id: 'runner-lease-recycled-between-signals-sim' };
1194+
writeStaleLeaseWithRunner(device.id, { runnerPid: 55_555, runnerStartTime: 'lease-time-start' });
1195+
// The runner dies on SIGTERM and its pid is recycled while the awaited
1196+
// xcodebuild sweep runs — the SIGKILL escalation must not trust the
1197+
// verification performed for SIGTERM.
1198+
let verifications = 0;
1199+
mockReadProcessStartTime.mockImplementation((pid: number) =>
1200+
pid === 55_555 ? (++verifications === 1 ? 'lease-time-start' : 'recycled-newer-start') : null,
1201+
);
1202+
const { adapter, treeKills } = makeRecordingCleanupAdapter();
1203+
1204+
await prepareRunnerLeaseForStartup(device.id, adapter);
1205+
1206+
assert.deepEqual(treeKills, [
1207+
{ pid: 55_555, signal: 'SIGTERM' },
1208+
{ pid: undefined, signal: 'SIGKILL' },
1209+
]);
1210+
});
1211+
11921212
test('stale-lease cleanup without a recorded start time trusts only runner-shaped commands', async () => {
11931213
const device = { ...IOS_SIMULATOR, id: 'runner-lease-legacy-pid-sim' };
11941214

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path from 'node:path';
22
import { resolveIosSimulatorDeviceSetPath } from '../../../../utils/device-isolation.ts';
33
import { emitDiagnostic } from '../../../../utils/diagnostics.ts';
4-
import { isProcessAlive, readProcessStartTime } from '../../../../utils/host-process.ts';
4+
import { isProcessAlive } from '../../../../utils/host-process.ts';
55
import { parseBooleanLiteral } from '../../../../utils/source-value.ts';
66
import type { DeviceInfo } from '@agent-device/kernel/device';
77
import type { ExecResult } from '../../../../utils/exec.ts';
@@ -10,6 +10,7 @@ import { withRunnerCommandId } from './runner-contract.ts';
1010
import {
1111
buildRunnerLease,
1212
readStaleRunnerLease,
13+
verifyLeaseRunnerPidIdentity,
1314
writeRunnerLease,
1415
type RunnerLease,
1516
} from './runner-lease.ts';
@@ -69,9 +70,12 @@ export async function tryAdoptRunnerSessionFromLease(
6970
const runnerPid = lease.runnerPid;
7071
if (!runnerPid) return skip('runner_pid_missing');
7172
if (!isProcessAlive(runnerPid)) return skip('runner_process_dead');
72-
// The adopted session later signals this pid on disposal, so a recycled pid
73-
// must never be adopted even if some process answers the leased port.
74-
if (lease.runnerStartTime && readProcessStartTime(runnerPid) !== lease.runnerStartTime) {
73+
// The adopted session later signals this pid on disposal — and adoption
74+
// re-stamps the lease with the live pid's start time — so a pid that cannot
75+
// be proven to still be the leased runner must never be adopted, even if
76+
// some process answers the leased port. Legacy leases without a recorded
77+
// start time fall back to the runner-shaped command-line check.
78+
if (!verifyLeaseRunnerPidIdentity(lease, runnerPid)) {
7579
return skip('runner_pid_recycled');
7680
}
7781
const expectedDerived = resolveExpectedDerivedPath(device);

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

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -462,10 +462,9 @@ async function cleanupLeasedRunnerProcesses(
462462
reason,
463463
},
464464
});
465-
const runnerPid = resolveVerifiedLeaseRunnerPid(lease);
466-
await cleanup.cleanupRunnerProcessTree(runnerPid, 'SIGTERM');
465+
await cleanup.cleanupRunnerProcessTree(resolveVerifiedLeaseRunnerPid(lease), 'SIGTERM');
467466
await cleanup.cleanupRunnerXcodebuildProcesses(lease.deviceId, lease.ownerToken);
468-
await cleanup.cleanupRunnerProcessTree(runnerPid, 'SIGKILL');
467+
await cleanup.cleanupRunnerProcessTree(resolveVerifiedLeaseRunnerPid(lease), 'SIGKILL');
469468
cleanup.cleanupTempFile(lease.xctestrunPath);
470469
cleanup.cleanupTempFile(lease.jsonPath);
471470
releaseRunnerLease(lease);
@@ -475,19 +474,17 @@ async function cleanupLeasedRunnerProcesses(
475474
* A lease file can outlive its runner by days (SIGKILLed daemon), and pids are
476475
* recycled — killing `lease.runnerPid` raw would signal whatever process now
477476
* holds that pid, including its whole process group (#1596). Only return the
478-
* pid when the live process is provably still the leased runner: matching
479-
* recorded start time, or — for leases written before `runnerStartTime`
480-
* existed — a command line that looks like the runner's xcodebuild. The
477+
* pid when the live process is provably still the leased runner. Callers must
478+
* resolve immediately before each signal and never reuse a resolved pid across
479+
* an await: the runner usually dies on SIGTERM, and its pid can be recycled
480+
* while the awaited xcodebuild sweep runs before the SIGKILL escalation. The
481481
* pattern-based xcodebuild pkill in the cleanup adapter is unaffected and
482482
* still collects genuinely stray runner processes.
483483
*/
484484
function resolveVerifiedLeaseRunnerPid(lease: RunnerLease): number | undefined {
485485
const pid = lease.runnerPid ?? undefined;
486486
if (!pid || !isProcessAlive(pid)) return undefined;
487-
const verified = lease.runnerStartTime
488-
? readProcessStartTime(pid) === lease.runnerStartTime
489-
: isRunnerXcodebuildCommand(readProcessCommand(pid));
490-
if (verified) return pid;
487+
if (verifyLeaseRunnerPidIdentity(lease, pid)) return pid;
491488
emitDiagnostic({
492489
level: 'warn',
493490
phase: 'ios_runner_lease_recycled_pid_skipped',
@@ -501,6 +498,21 @@ function resolveVerifiedLeaseRunnerPid(lease: RunnerLease): number | undefined {
501498
return undefined;
502499
}
503500

501+
/**
502+
* Is the live process holding `pid` provably still the runner this lease
503+
* recorded? Matching recorded start time, or — for leases written before
504+
* `runnerStartTime` existed — a command line that looks like the runner's
505+
* xcodebuild. Shared with adoption: an unverified pid must never be adopted,
506+
* because adoption re-stamps the lease with the live pid's start time and
507+
* would launder a recycled pid into a strongly-verified lease that disposal
508+
* later kills (#1596).
509+
*/
510+
export function verifyLeaseRunnerPidIdentity(lease: RunnerLease, pid: number): boolean {
511+
return lease.runnerStartTime
512+
? readProcessStartTime(pid) === lease.runnerStartTime
513+
: isRunnerXcodebuildCommand(readProcessCommand(pid));
514+
}
515+
504516
function isRunnerXcodebuildCommand(command: string | null): boolean {
505517
return !!command && command.includes('xcodebuild') && command.includes('AgentDeviceRunner');
506518
}

0 commit comments

Comments
 (0)