Skip to content

Commit 0d1aee2

Browse files
committed
test(daemon): assert daemon exit with the predicate retirement confirms
stopAndRetireDaemon confirms exit through waitForDaemonExit, which counts a zombie as terminated. A force-killed detached daemon is reparented and stays a zombie until its new parent reaps it, and kill(pid, 0) still finds a zombie, so the immediate isProcessAlive probe after a confirmed retirement raced the reaper. Assert the recorded lifetime ended with waitForDaemonExit. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016qAu53rMak1bXMuR5U2gF4
1 parent a97f794 commit 0d1aee2

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

‎test/integration/smoke-daemon-clean.test.ts‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { runCmdSync } from '@agent-device/host-kit/command';
99
import { isProcessAlive, readProcessStartTime } from '@agent-device/host-kit/process';
1010
import { stopAndRetireDaemon } from '@agent-device/daemon-contracts/daemon-registration-owner';
1111
import { resolveDaemonPaths } from '@agent-device/daemon-contracts/daemon-resolution';
12+
import { waitForDaemonExit } from '@agent-device/daemon-contracts/daemon-process';
1213

1314
import { assertNoDaemonLeaks } from './support/daemon-leak-oracle.ts';
1415
import { runCliJson } from './test-helpers.ts';
@@ -101,7 +102,7 @@ test('clean daemon script stops a live daemon before removing metadata', async (
101102
},
102103
);
103104
assert.equal(cleanup.exitCode, 0, cleanup.stderr);
104-
assert.equal(isProcessAlive(info.pid), false);
105+
await assertDaemonLifetimeEnded(info);
105106
assert.equal(fs.existsSync(path.join(stateDir, 'daemon.json')), false);
106107
assert.equal(fs.existsSync(path.join(stateDir, 'daemon.lock')), false);
107108
// #1781 B1: the stopped daemon must take every process it owned with it and
@@ -137,14 +138,27 @@ test('forced retirement waits for a real daemon and reclaims its abandoned regis
137138
if (result.status !== 'retired') assert.fail('retirement not confirmed');
138139
assert.equal(result.termination.mode, 'forced');
139140
assert.equal(result.removedInfo, true);
140-
assert.equal(isProcessAlive(info.pid), false);
141+
await assertDaemonLifetimeEnded(info);
141142
assert.equal(fs.existsSync(paths.infoPath), false);
142143
assert.equal(fs.existsSync(paths.lockPath), false);
143144
} finally {
144145
await cleanupDaemonTestState(stateDir, info ?? null);
145146
}
146147
});
147148

149+
/**
150+
* Checks the daemon lifetime with the exit predicate that retirement confirms. A killed detached
151+
* daemon is a zombie until its new parent reaps it, and `kill(pid, 0)` still finds a zombie.
152+
*/
153+
async function assertDaemonLifetimeEnded(info: DaemonInfo): Promise<void> {
154+
assert.ok(info.processStartTime, 'the daemon record names its process start time');
155+
const wait = await waitForDaemonExit(
156+
{ pid: info.pid, startTime: info.processStartTime },
157+
{ timeoutMs: 0 },
158+
);
159+
assert.equal(wait.exited, true, `daemon pid ${info.pid} still runs its recorded lifetime`);
160+
}
161+
148162
function readDaemonInfo(stateDir: string): DaemonInfo {
149163
return JSON.parse(fs.readFileSync(path.join(stateDir, 'daemon.json'), 'utf8')) as DaemonInfo;
150164
}

0 commit comments

Comments
 (0)