Skip to content

Commit 64644c3

Browse files
committed
fix(test): settle the daemon's exit on its own deadline in the web shutdown lane
The daemon-exit assertion in the #1868 shutdown lane was an instantaneous liveness read placed right after settleManagedBrowserProcesses, so it silently borrowed the Chrome fleet's poll deadline for a condition it never polled. It passed only while the fleet outlasted the daemon. Nothing enforces that order. The daemon awaits the `agent-browser close` CLI's return, never the disappearance of the Chrome pids that call reaps, and awaits it under a teardown-budget race it walks away from on expiry — so either can finish first, and both have been observed: macOS reaps the fleet ~13s after the daemon, while CI once had the daemon still alive ~4.8s after the fleet and failed the lane. A re-run of the same commit passed. Settle both endings concurrently from the same SIGTERM, each against its own deadline, and assert no ordering between them. The guarantee #1868 is actually about — the fleet closed because the daemon closed it, not because agent-browser's idle timer beat us — is held by WEB_SHUTDOWN_IDLE_TIMEOUT_MS, not by any ordering here. settleDaemonExit waits out the daemon's identity rather than the bare pid: a pid the host recycles inside the 45s window is a different process, and reading it as "the daemon is still alive" would fail this lane for the one reason it must not — a false red. It reuses isAgentDeviceDaemonProcess and both existing settle constants, and reports elapsed time so a deadline failure says how long the daemon outlived its SIGTERM.
1 parent ea1d6b8 commit 64644c3

1 file changed

Lines changed: 41 additions & 5 deletions

File tree

‎test/integration/smoke-web-platform.test.ts‎

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ import {
1717
getManagedAgentBrowserStatus,
1818
type AgentBrowserToolStatus,
1919
} from '../../src/platforms/web/agent-browser-tool.ts';
20-
import { stopProcessForTakeover } from '../../src/daemon/daemon-process.ts';
20+
import {
21+
isAgentDeviceDaemonProcess,
22+
stopProcessForTakeover,
23+
} from '../../src/daemon/daemon-process.ts';
2124
import {
2225
expandProcessTree,
2326
isProcessAlive,
@@ -194,16 +197,28 @@ async function runWebShutdownSmoke(context: WebSmokeContext): Promise<void> {
194197
// orchestrator shutting the container down would, rather than going through `close`.
195198
process.kill(daemonPid, 'SIGTERM');
196199

197-
const after = await settleManagedBrowserProcesses(status);
200+
// Two unordered endings, so each gets its own deadline from this SIGTERM rather than one
201+
// waiting out the other. The daemon awaits the `agent-browser close` CLI's RETURN, never the
202+
// disappearance of the Chrome pids that call reaps, and awaits it under a teardown-budget
203+
// race it walks away from on expiry — so either can finish first, and both orders have been
204+
// seen (macOS: fleet gone ~13s after the daemon; CI: daemon alive ~4.8s after the fleet).
205+
// Settling only the fleet and then reading daemon liveness once, as this did, silently lent
206+
// the daemon the fleet's deadline and failed whenever CI reaped Chrome first. The guarantee
207+
// #1868 is about — the fleet closed BECAUSE the daemon closed it — is held by
208+
// WEB_SHUTDOWN_IDLE_TIMEOUT_MS, not by any ordering between these two.
209+
const [after, daemonExit] = await Promise.all([
210+
settleManagedBrowserProcesses(status),
211+
settleDaemonExit(daemonIdentity),
212+
]);
198213
assert.equal(
199214
after.count,
200215
0,
201216
`expected zero owned Chrome processes after daemon shutdown, found: ${formatProcessSummary(after)}`,
202217
);
203218
assert.equal(
204-
isProcessAlive(daemonPid),
205-
false,
206-
'expected the daemon process itself to have exited after SIGTERM',
219+
daemonExit.exited,
220+
true,
221+
`expected the daemon process itself to have exited after SIGTERM, still alive ${daemonExit.elapsedMs}ms later`,
207222
);
208223

209224
// #1781 B1: the daemon leak oracle this fix unblocks wiring to a web lane — no stray
@@ -298,6 +313,27 @@ async function settleManagedBrowserProcesses(
298313
return summary;
299314
}
300315

316+
// The daemon-side sibling of settleManagedBrowserProcesses, on the same deadline and poll: 45s
317+
// clears the daemon's dominant term, the web session's 35s teardown budget
318+
// (WEB_BROWSER_SESSION_TEARDOWN_BUDGET_MS + DAEMON_SESSION_TEARDOWN_TIMEOUT_MS), spent before the
319+
// rest of its shutdown sequence even starts. Waits out the identity, not the bare pid: a pid the
320+
// host recycled inside that 45s window is a DIFFERENT process, and reading it as "the daemon is
321+
// still alive" would fail this lane for the one reason it is not allowed to — a false red. The
322+
// sibling test at the top of this file exists for that same hazard. Reports elapsed time so a
323+
// deadline failure says how long the daemon outlived its SIGTERM, not merely that it did.
324+
async function settleDaemonExit(
325+
identity: WebShutdownDaemonIdentity,
326+
): Promise<{ exited: boolean; elapsedMs: number }> {
327+
const start = Date.now();
328+
const deadline = start + WEB_SHUTDOWN_SETTLE_TIMEOUT_MS;
329+
let alive = isAgentDeviceDaemonProcess(identity.pid, identity.startTime);
330+
while (alive && Date.now() < deadline) {
331+
await new Promise((resolve) => setTimeout(resolve, WEB_SHUTDOWN_SETTLE_POLL_MS));
332+
alive = isAgentDeviceDaemonProcess(identity.pid, identity.startTime);
333+
}
334+
return { exited: !alive, elapsedMs: Date.now() - start };
335+
}
336+
301337
function formatProcessSummary(summary: AgentBrowserProcessSummary): string {
302338
return JSON.stringify({
303339
count: summary.count,

0 commit comments

Comments
 (0)