Skip to content

Commit dac78ed

Browse files
committed
test: daemon leak oracle around the real-subprocess daemon lanes (#1781 B1)
Adds test/integration/support/daemon-leak-oracle.ts and calls it at the end of smoke-daemon-clean, smoke-daemon-http and daemon-replace-exit-flush. After shutdown the oracle asserts that no daemon-owned process survives (ownership: PPID descendant, PGID = daemon pid, AGENT_DEVICE_STATE_DIR env, state-dir argv — never global counts) and that the isolated state dir holds only classified artifacts (no *.tmp, no daemon.json/lock without a live daemon, no open capture descriptor). Red-proofs: pre-fix #1324 (a84caa8) leaves simctl recordVideo with ppid 1 / pgid = dead daemon; pre-fix #1109 (be4bd09) leaves the agent-browser daemon plus its Chrome fleet. Both are clean on main. Refs #1781 #1431
1 parent fda81c5 commit dac78ed

4 files changed

Lines changed: 385 additions & 0 deletions

File tree

‎test/integration/daemon-replace-exit-flush.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import path from 'node:path';
66
import { skipWhenLoopbackUnavailable } from '../../src/__tests__/test-utils/loopback.ts';
77
import { stopProcessForTakeover } from '../../src/daemon/daemon-process.ts';
88
import { isProcessAlive } from '../../src/utils/host-process.ts';
9+
import { assertNoDaemonLeaks } from './support/daemon-leak-oracle.ts';
910
import { runCliJson } from './test-helpers.ts';
1011

1112
// #1596: a CLI command that finds its recorded daemon unreachable replaces it
@@ -28,6 +29,7 @@ test('daemon replace mid-command returns a structured, parseable error and exits
2829

2930
const stateDir = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-replace-exit-flush-'));
3031
let info: DaemonInfo | null = null;
32+
const daemonPids: number[] = [];
3133
try {
3234
// A real daemon, started by this codebase, so its recorded version/code
3335
// signature legitimately match — the only way to reach the "unreachable"
@@ -36,6 +38,7 @@ test('daemon replace mid-command returns a structured, parseable error and exits
3638
assert.equal(started.status, 0, `${started.stderr}\n${started.stdout}`);
3739

3840
info = readDaemonInfo(stateDir);
41+
daemonPids.push(info.pid);
3942
assert.equal(isProcessAlive(info.pid), true, 'expected the started daemon to be alive');
4043

4144
// Kill it out from under its own metadata: daemon.json stays put and
@@ -69,6 +72,16 @@ test('daemon replace mid-command returns a structured, parseable error and exits
6972
);
7073

7174
info = readDaemonInfo(stateDir);
75+
daemonPids.push(info.pid);
76+
await stopProcessForTakeover(info.pid, {
77+
termTimeoutMs: 1_500,
78+
killTimeoutMs: 1_500,
79+
expectedStartTime: info.processStartTime,
80+
});
81+
info = null;
82+
// #1781 B1: neither the SIGKILLed daemon nor its replacement may leave owned
83+
// processes or unclassified state-dir residue once both are gone.
84+
await assertNoDaemonLeaks({ stateDir, daemonPids, phase: 'after-shutdown' });
7285
} finally {
7386
if (info) {
7487
await stopProcessForTakeover(info.pid, {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { skipWhenLoopbackUnavailable } from '../../src/__tests__/test-utils/loop
77
import { runCmdSync } from '../../src/utils/exec.ts';
88
import { stopProcessForTakeover } from '../../src/daemon/daemon-process.ts';
99
import { isProcessAlive } from '../../src/utils/host-process.ts';
10+
import { assertNoDaemonLeaks } from './support/daemon-leak-oracle.ts';
1011
import { runCliJson } from './test-helpers.ts';
1112

1213
type DaemonInfo = {
@@ -41,6 +42,9 @@ test('clean daemon script stops a live daemon before removing metadata', async (
4142
assert.equal(isProcessAlive(info.pid), false);
4243
assert.equal(fs.existsSync(path.join(stateDir, 'daemon.json')), false);
4344
assert.equal(fs.existsSync(path.join(stateDir, 'daemon.lock')), false);
45+
// #1781 B1: the stopped daemon must take every process it owned with it and
46+
// leave only classified artifacts in its state dir.
47+
await assertNoDaemonLeaks({ stateDir, daemonPids: [info.pid], phase: 'after-shutdown' });
4448
} finally {
4549
if (info) {
4650
await stopProcessForTakeover(info.pid, {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { DAEMON_RPC_PROTOCOL_VERSION } from '../../src/daemon/http-health.ts';
77
import { skipWhenLoopbackUnavailable } from '../../src/__tests__/test-utils/loopback.ts';
88
import { stopProcessForTakeover } from '../../src/daemon/daemon-process.ts';
99
import { formatResultDebug } from './cli-json.ts';
10+
import { assertNoDaemonLeaks } from './support/daemon-leak-oracle.ts';
1011
import { runCliJson } from './test-helpers.ts';
1112

1213
type DaemonInfo = {
@@ -112,6 +113,9 @@ async function stopDaemonForStateDir(stateDir: string): Promise<void> {
112113
killTimeoutMs: 1500,
113114
expectedStartTime: info.processStartTime,
114115
});
116+
// #1781 B1: the HTTP-mode daemon must exit without owned processes or
117+
// unclassified state-dir residue.
118+
await assertNoDaemonLeaks({ stateDir, daemonPids: [info.pid], phase: 'after-shutdown' });
115119
} finally {
116120
fs.rmSync(stateDir, { recursive: true, force: true });
117121
}

0 commit comments

Comments
 (0)