Skip to content

Commit af42000

Browse files
committed
fix(host-kit): keep a same-lifetime command change unverified and record emulator launches at spawn
judgeProcessIdentity now reads a confirmed start time with a command that does not match as unverified, not reassigned: exec and argv rewrites keep the start time, so it is neither proof the process is gone nor permission to signal. The startup reaper counts a stop whose proof was a reassigned pid as missing, never terminated. The recording marker check still refuses a readable mismatch. The Android emulator host records a launch right after the spawn, before any later step can throw, so a failed launch can still be terminated. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016qAu53rMak1bXMuR5U2gF4
1 parent 4128813 commit af42000

7 files changed

Lines changed: 83 additions & 24 deletions

‎packages/host-kit/src/internal/host-process-stop-confirm.test.ts‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -199,15 +199,12 @@ const STOP_CASES: HostCase[] = [
199199
expect: UNVERIFIED,
200200
},
201201
{
202-
// The grace period ends with the pid running another program under the same lifetime, as after
203-
// an `exec`: SIGKILL re-proves identity first and spares it.
202+
// The grace period ends with the same lifetime running another program, as after an `exec`:
203+
// that is neither proof it is gone nor permission for SIGKILL, so it is retained.
204204
name: 'execs another program while it is given its grace period',
205205
commandAfterSignal: { signal: 'SIGTERM', command: '/usr/bin/other --serve' },
206206
...HELD,
207-
expect: {
208-
outcome: { status: 'stopped', signal: 'SIGTERM', reassigned: true },
209-
signals: ['SIGTERM'],
210-
},
207+
expect: { outcome: UNVERIFIED.outcome, signals: ['SIGTERM'] },
211208
},
212209
{
213210
name: 'finalizes on the interrupt its caller opens with',
@@ -227,7 +224,15 @@ const STOP_CASES: HostCase[] = [
227224
expect: REASSIGNED,
228225
},
229226
{
230-
name: 'pid whose command line names a different program',
227+
// A confirmed start time with another command line is the same lifetime, as after an argv
228+
// rewrite: never signaled, and never reported gone.
229+
name: 'pid whose command line names a different program under its recorded lifetime',
230+
identity: { command: '/usr/bin/other --serve' },
231+
expect: UNVERIFIED,
232+
},
233+
{
234+
name: 'pid whose command line names a different program, with only a command recorded',
235+
claims: 'matches',
231236
identity: { command: '/usr/bin/other --serve' },
232237
expect: REASSIGNED,
233238
},

‎packages/host-kit/src/internal/host-process.ts‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -727,10 +727,12 @@ export type ProcessStopBudget = Readonly<{
727727
* question before a signal:
728728
*
729729
* - `exited`: the host shows the pid as a zombie, so the recorded process already terminated.
730-
* - `reassigned`: a readable start time or command contradicts the record, so the pid now names a
731-
* different process and the recorded one is gone.
730+
* - `reassigned`: a readable start time contradicts the record, or, with no start time to confirm,
731+
* a readable command does: the pid now names a different process and the recorded one is gone.
732732
* - `ours`: at least one recorded claim is confirmed by a readable fact and none is contradicted.
733-
* - `unverified`: no recorded claim is confirmed. A silent host is never permission to signal.
733+
* - `unverified`: no recorded claim is confirmed, or the recorded start time is confirmed while
734+
* the command is not. That is the same lifetime running something else, as after an `exec` or
735+
* an argv rewrite: it is neither proof the recorded process is gone nor permission to signal.
734736
*/
735737
export type ProcessIdentityVerdict = 'ours' | 'exited' | 'reassigned' | 'unverified';
736738

@@ -739,11 +741,11 @@ export function judgeProcessIdentity(
739741
facts: HostProcessIdentityFacts,
740742
): ProcessIdentityVerdict {
741743
if (facts.zombie) return 'exited';
742-
if (expected.startTime && facts.startTime && facts.startTime !== expected.startTime) {
743-
return 'reassigned';
744-
}
745-
if (expected.matches && facts.command && !expected.matches(facts.command)) return 'reassigned';
746744
const confirmedStartTime = Boolean(expected.startTime && facts.startTime);
745+
if (confirmedStartTime && facts.startTime !== expected.startTime) return 'reassigned';
746+
if (expected.matches && facts.command && !expected.matches(facts.command)) {
747+
return confirmedStartTime ? 'unverified' : 'reassigned';
748+
}
747749
const confirmedCommand = Boolean(expected.matches && facts.command);
748750
return confirmedStartTime || confirmedCommand ? 'ours' : 'unverified';
749751
}

‎packages/host-kit/src/internal/owned-process-reaper.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,9 @@ async function reapOwnedProcess(
174174
}
175175

176176
function reapOutcomeOf(stop: ConfirmedProcessStop): ReapOutcome {
177-
if (stop.status === 'stopped') return stop.signal ? 'terminated' : 'missing';
177+
// A pid proven to name another process is forgotten as missing, whether or not it was signaled
178+
// first: nobody watched the recorded process leave.
179+
if (stop.status === 'stopped') return stop.signal && !stop.reassigned ? 'terminated' : 'missing';
178180
return stop.reason === 'identity-unverified' ? 'unconfirmed' : 'survived';
179181
}
180182

‎packages/host-kit/src/internal/owned-process-record.test.ts‎

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const processState = vi.hoisted(() => ({
1515
exitsOnSignal: true,
1616
/** The command a signaled process shows afterwards, as after an `exec`. */
1717
commandAfterSignal: undefined as string | undefined,
18+
/** The start time a signaled pid shows afterwards, as when the pid is handed to a new process. */
19+
startAfterSignal: undefined as string | undefined,
1820
signals: [] as Array<string | number | undefined>,
1921
mockRunCmd: vi.fn(),
2022
}));
@@ -33,6 +35,7 @@ function driveHost(): void {
3335
processState.signals.push(signal);
3436
if (processState.commandAfterSignal)
3537
processState.commands.set(pid, processState.commandAfterSignal);
38+
if (processState.startAfterSignal) processState.starts.set(pid, processState.startAfterSignal);
3639
if (processState.exitsOnSignal) processState.alive.delete(pid);
3740
return true;
3841
}) as typeof process.kill);
@@ -58,6 +61,7 @@ beforeEach(() => {
5861
processState.zombiePids.clear();
5962
processState.exitsOnSignal = true;
6063
processState.commandAfterSignal = undefined;
64+
processState.startAfterSignal = undefined;
6165
processState.signals = [];
6266
processState.mockRunCmd.mockReset();
6367
driveHost();
@@ -255,7 +259,7 @@ test('startup keeps a confirmed process that outlives SIGKILL as survived, not a
255259
}
256260
});
257261

258-
test('startup does not force-kill a live process after its identity changes', async () => {
262+
test('startup does not force-kill or forget a live process whose command changes after SIGTERM', async () => {
259263
const { store, cleanup } = reapFixture('replaced');
260264
hostRuns(BROWSER);
261265
processState.exitsOnSignal = false;
@@ -268,7 +272,46 @@ test('startup does not force-kill a live process after its identity changes', as
268272
killTimeoutMs: 1,
269273
});
270274

271-
assert.deepEqual(summary.terminatedPids, [BROWSER.pid]);
275+
assert.deepEqual(summary.unconfirmedPids, [BROWSER.pid]);
276+
assert.deepEqual(processState.signals, ['SIGTERM']);
277+
assert.equal(store.read().length, 1);
278+
} finally {
279+
cleanup();
280+
}
281+
});
282+
283+
test('startup keeps a record whose recorded lifetime now shows another command, without signaling it', async () => {
284+
// A browser that rewrites its argv keeps its start time: that is not proof it is gone.
285+
const { store, cleanup } = reapFixture('argv-rewrite');
286+
hostRuns({ ...BROWSER, command: '/opt/chrome --type=browser' });
287+
try {
288+
store.replace({ kind: 'daemon' }, [BROWSER]);
289+
290+
const summary = await reapOwnedProcessRecordsAtStartup(store);
291+
292+
assert.deepEqual(summary.unconfirmedPids, [BROWSER.pid]);
293+
assert.deepEqual(processState.signals, []);
294+
assert.equal(store.read().length, 1);
295+
} finally {
296+
cleanup();
297+
}
298+
});
299+
300+
test('startup counts a pid reassigned after SIGTERM as missing, not terminated', async () => {
301+
const { store, cleanup } = reapFixture('reassigned-after-term');
302+
hostRuns(BROWSER);
303+
processState.exitsOnSignal = false;
304+
processState.startAfterSignal = 'Tue Nov 10 09:00:00 2026';
305+
try {
306+
store.replace({ kind: 'daemon' }, [BROWSER]);
307+
308+
const summary = await reapOwnedProcessRecordsAtStartup(store, {
309+
termTimeoutMs: 1,
310+
killTimeoutMs: 1,
311+
});
312+
313+
assert.deepEqual(summary.missingPids, [BROWSER.pid]);
314+
assert.deepEqual(summary.terminatedPids, []);
272315
assert.deepEqual(processState.signals, ['SIGTERM']);
273316
assert.equal(store.read().length, 0);
274317
} finally {

‎src/platform-runtime-android-emulator-host.test.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ test('a terminate that could not prove the exit keeps the launch for a retry', a
139139
expect(stopProcessAndConfirmExit).toHaveBeenCalledTimes(2);
140140
});
141141

142-
test('a launch that fails after spawning leaves nothing for terminate to signal', async () => {
142+
test('a launch that fails after spawning can still be stopped by terminate', async () => {
143143
runCmdDetached
144144
.mockImplementationOnce(() => 4242)
145145
.mockImplementationOnce(() => {
@@ -150,5 +150,9 @@ test('a launch that fails after spawning leaves nothing for terminate to signal'
150150
await expect(host.launch('Pixel_9', false, 90_000)).rejects.toThrow('osascript missing');
151151
await host.terminate(4242);
152152

153-
expect(stopProcessAndConfirmExit).not.toHaveBeenCalled();
153+
expect(stopProcessAndConfirmExit).toHaveBeenCalledOnce();
154+
expect(stopProcessAndConfirmExit).toHaveBeenCalledWith(
155+
expect.objectContaining({ pid: 4242 }),
156+
expect.anything(),
157+
);
154158
});

‎src/platform-runtime-android-emulator-host.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ export function createAndroidEmulatorHost(
5353
? await readFrontmostAppPid().catch(() => undefined)
5454
: undefined;
5555
const pid = runCmdDetached('emulator', args);
56-
const startTime = readProcessIdentityFacts(pid).then((facts) => facts.startTime);
56+
// Recorded before any later step can throw, so a failed launch can still be stopped.
57+
launched.set(pid, {
58+
avdName,
59+
startTime: readProcessIdentityFacts(pid).then((facts) => facts.startTime),
60+
});
5761
if (frontmostPid !== undefined) {
5862
runCmdDetached('osascript', [
5963
'-l',
@@ -65,8 +69,6 @@ export function createAndroidEmulatorHost(
6569
String(bootBudgetMs),
6670
]);
6771
}
68-
// Recorded only once the launch succeeds, so a failed launch leaves nothing behind.
69-
launched.set(pid, { avdName, startTime });
7072
return pid;
7173
},
7274
terminate: async (pid) => {

‎src/platform-runtime-screen-recording-process-host.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,10 @@ async function readOwnershipEvidence(
133133
if (verdict === 'exited') return 'gone';
134134
if (marker.startTime.length === 0 || marker.command.length === 0) return 'not-ours';
135135
if (verdict === 'reassigned') return 'not-ours';
136-
// A recording marker is ours only while both recorded facts are confirmed.
136+
// A recording marker is ours only while both recorded facts are confirmed, and a readable fact
137+
// that does not match is a refusal.
137138
if (facts.startTime === null || facts.command === null) return 'unprovable';
138-
return verdict === 'ours' ? 'ours' : 'unprovable';
139+
return verdict === 'ours' ? 'ours' : 'not-ours';
139140
}
140141

141142
export async function terminateManagedProcessSet(

0 commit comments

Comments
 (0)