|
1 | 1 | import assert from 'node:assert/strict'; |
2 | | -import { mkdirSync, writeFileSync } from 'node:fs'; |
| 2 | +import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'; |
3 | 3 | import { createServer, type Server } from 'node:http'; |
4 | 4 | import path from 'node:path'; |
5 | 5 | import test from 'node:test'; |
6 | 6 | import { type CliJsonResult, formatResultDebug, runBuiltCliJson } from './cli-json.ts'; |
7 | 7 | import { assertPngDimensions, assertPngFile } from './provider-scenarios/assertions.ts'; |
8 | 8 | import { runCleanupWithCoverageReport } from './web-e2e/coverage-report.ts'; |
| 9 | +import { assertNoDaemonLeaks } from './support/daemon-leak-oracle.ts'; |
| 10 | +import { |
| 11 | + inspectManagedAgentBrowserProcesses, |
| 12 | + type AgentBrowserProcessSummary, |
| 13 | +} from '../../src/platforms/web/agent-browser-lifecycle.ts'; |
| 14 | +import { getManagedAgentBrowserStatus } from '../../src/platforms/web/agent-browser-tool.ts'; |
| 15 | +import { isProcessAlive } from '../../src/utils/host-process.ts'; |
9 | 16 |
|
10 | 17 | const TEST_NAME = 'live web platform e2e smoke'; |
| 18 | +const SHUTDOWN_TEST_NAME = 'live web platform e2e daemon-shutdown browser cleanup'; |
11 | 19 | const WEB_E2E_ENABLED = process.env.AGENT_DEVICE_WEB_E2E === '1'; |
| 20 | +// #1868: SIGTERM must close the managed Chrome fleet well before agent-browser's own idle timer |
| 21 | +// (DEFAULT_AGENT_BROWSER_IDLE_TIMEOUT_MS, 5 minutes) would have. Left at its production default |
| 22 | +// here (unlike the functional smoke test's shortened override below) so a pass can only mean the |
| 23 | +// daemon's shutdown teardown actively closed the browser, not that the idle timer coincidentally |
| 24 | +// beat this test's own poll deadline. |
| 25 | +const WEB_SHUTDOWN_SETTLE_TIMEOUT_MS = 45_000; |
| 26 | +const WEB_SHUTDOWN_SETTLE_POLL_MS = 500; |
12 | 27 |
|
13 | 28 | type StepRecord = { |
14 | 29 | step: string; |
|
42 | 57 | }, |
43 | 58 | ); |
44 | 59 |
|
| 60 | +// #1868: teardownSessionResources gained a best-effort web-close step so a daemon shutdown (or |
| 61 | +// an expired-session reap) tells agent-browser to close its Chrome fleet immediately instead of |
| 62 | +// leaving it for agent-browser's own multi-minute idle timer. The unit tests around |
| 63 | +// teardownSessionResources and teardownDaemonSessionForShutdown mock the agent-browser CLI call |
| 64 | +// and prove only that it gets dispatched with the right arguments; this live lane is the one |
| 65 | +// place that proves the whole chain — a real managed browser, a real daemon process, a real |
| 66 | +// SIGTERM — actually ends with zero owned Chrome processes, not just an issued close command. |
| 67 | +test( |
| 68 | + SHUTDOWN_TEST_NAME, |
| 69 | + { |
| 70 | + skip: WEB_E2E_ENABLED |
| 71 | + ? false |
| 72 | + : 'Set AGENT_DEVICE_WEB_E2E=1 to run the managed web backend smoke.', |
| 73 | + }, |
| 74 | + async () => { |
| 75 | + await runWebShutdownSmoke(await createWebSmokeContext()); |
| 76 | + }, |
| 77 | +); |
| 78 | + |
| 79 | +async function runWebShutdownSmoke(context: WebSmokeContext): Promise<void> { |
| 80 | + try { |
| 81 | + await runStep(context, 'set up managed web backend', ['web', 'setup', '--json']); |
| 82 | + await runStep(context, 'open local fixture', ['open', context.url, ...context.common]); |
| 83 | + |
| 84 | + const stateDir = context.env.AGENT_DEVICE_STATE_DIR; |
| 85 | + assert.ok(stateDir, 'expected the smoke context to configure a state dir'); |
| 86 | + const status = getManagedAgentBrowserStatus({ stateDir }); |
| 87 | + |
| 88 | + const before = await inspectManagedAgentBrowserProcesses(status); |
| 89 | + assert.ok( |
| 90 | + before.count > 0, |
| 91 | + `expected the managed browser fleet to be running after open, found none: ${formatProcessSummary(before)}`, |
| 92 | + ); |
| 93 | + |
| 94 | + const daemonPid = readDaemonPid(stateDir); |
| 95 | + assert.equal(isProcessAlive(daemonPid), true, 'expected a live daemon before SIGTERM'); |
| 96 | + |
| 97 | + // The scenario #1868 is about: SIGTERM the daemon directly, the way an operator or an |
| 98 | + // orchestrator shutting the container down would, rather than going through `close`. |
| 99 | + process.kill(daemonPid, 'SIGTERM'); |
| 100 | + |
| 101 | + const after = await settleManagedBrowserProcesses(status); |
| 102 | + assert.equal( |
| 103 | + after.count, |
| 104 | + 0, |
| 105 | + `expected zero owned Chrome processes after daemon shutdown, found: ${formatProcessSummary(after)}`, |
| 106 | + ); |
| 107 | + assert.equal( |
| 108 | + isProcessAlive(daemonPid), |
| 109 | + false, |
| 110 | + 'expected the daemon process itself to have exited after SIGTERM', |
| 111 | + ); |
| 112 | + |
| 113 | + // #1781 B1: the daemon leak oracle this fix unblocks wiring to a web lane — no stray |
| 114 | + // state-dir residue either, on top of the process-level proof above. |
| 115 | + await assertNoDaemonLeaks({ stateDir, daemonPids: [daemonPid], phase: 'after-shutdown' }); |
| 116 | + } finally { |
| 117 | + await closeServer(context.server); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +function readDaemonPid(stateDir: string): number { |
| 122 | + const info = JSON.parse(readFileSync(path.join(stateDir, 'daemon.json'), 'utf8')) as { |
| 123 | + pid?: number; |
| 124 | + }; |
| 125 | + assert.equal(typeof info.pid, 'number', `daemon.json has no pid: ${JSON.stringify(info)}`); |
| 126 | + return info.pid as number; |
| 127 | +} |
| 128 | + |
| 129 | +async function settleManagedBrowserProcesses( |
| 130 | + status: Parameters<typeof inspectManagedAgentBrowserProcesses>[0], |
| 131 | +): Promise<AgentBrowserProcessSummary> { |
| 132 | + const deadline = Date.now() + WEB_SHUTDOWN_SETTLE_TIMEOUT_MS; |
| 133 | + let summary = await inspectManagedAgentBrowserProcesses(status); |
| 134 | + while (summary.count > 0 && Date.now() < deadline) { |
| 135 | + await new Promise((resolve) => setTimeout(resolve, WEB_SHUTDOWN_SETTLE_POLL_MS)); |
| 136 | + summary = await inspectManagedAgentBrowserProcesses(status); |
| 137 | + } |
| 138 | + return summary; |
| 139 | +} |
| 140 | + |
| 141 | +function formatProcessSummary(summary: AgentBrowserProcessSummary): string { |
| 142 | + return JSON.stringify({ |
| 143 | + count: summary.count, |
| 144 | + pids: summary.pids, |
| 145 | + reasons: summary.processes.map((match) => match.reason), |
| 146 | + }); |
| 147 | +} |
| 148 | + |
45 | 149 | async function runWebSmoke(context: WebSmokeContext): Promise<void> { |
46 | 150 | let opened = false; |
47 | 151 |
|
|
0 commit comments