fix(browse): server runtime — restore off the boot path, shutdown that cannot hang, watchdog that still reaps tunnels

Four review findings on the wave's own new wiring: session restore ran
before Bun.serve with sequential 15s gotos while the CLI gives up at 8s
(one slow saved URL bricked every $B command) — restore now runs in the
background after bind; the shutdown snapshot gets a 2s deadline so a
wedged page.evaluate can't hold the port forever behind the new
ack-first stop; the persistence ticker gets in-flight + shutdown gates
and is cleared before the final snapshot; and the absorbed #2565
handoff fix no longer clears the whole parent watchdog — a suppress
flag keeps the tunnel-orphan reaper alive (handoff→resume→tunnel is no
longer an unreapable internet-exposed daemon). pair-agent with consent
off now names the real remedy instead of ngrok install instructions.
Lock-acquisition edge branches (garbage pidfile, vanish-race depth cap)
pinned.
This commit is contained in:
Garry Tan
2026-08-14 17:12:27 -07:00
parent 079988ba70
commit a840d0b7df
5 changed files with 353 additions and 99 deletions
+138 -16
View File
@@ -1,8 +1,12 @@
import { describe, test, expect, afterEach } from 'bun:test';
import { describe, test, expect, afterEach, beforeEach, mock } from 'bun:test';
import { spawn, type Subprocess } from 'bun';
import * as path from 'path';
import * as fs from 'fs';
import * as os from 'os';
import * as crypto from 'crypto';
import { buildFetchHandler, __testInternals__, type ServerConfig } from '../src/server';
import { __resetRegistry } from '../src/token-registry';
import { resolveConfig } from '../src/config';
// End-to-end regression tests for the parent-process watchdog in server.ts.
// The watchdog has layered behavior since v0.18.1.0 (#1025) and v0.18.2.0
@@ -18,11 +22,9 @@ import * as os from 'os';
// eventual cleanup.
//
// Tunnel mode coverage (parent dies → shutdown because idle timeout doesn't
// apply) is not covered by an automated test here — tunnelActive is a runtime
// variable set by /pair-agent's tunnel-create flow, not an env var, so faking
// it would require invasive test-only hooks. The mode check is documented
// inline at the watchdog and SIGTERM handlers, and would regress visibly for
// /pair-agent users (server lingers after disconnect).
// apply) is covered behaviorally in the in-process suite at the bottom of this
// file: the tick is exported via __testInternals__.parentWatchdogTick (same
// seam as idleCheckTick) and tunnelActive is simulated via setTunnelActive.
//
// Each test spawns the real server.ts. Tests 1 and 2 verify behavior via
// stdout log line (fast). Test 3 waits for the watchdog poll cycle to confirm
@@ -165,11 +167,18 @@ describe('parent-process watchdog (v0.18.1.0)', () => {
// so the next poll shut the daemon down and discarded whatever the user had been
// handed off to do — observed as repeated session loss mid-login.
//
// The fix must NOT clear the interval, though: the same tick is the
// tunnel-orphan reaper (idle timeout is disabled in tunnel mode, so parent
// death is the ONLY thing that reaps an internet-exposed daemon). Promotion
// sets a suppress flag the tick re-reads each pass — "being headed" no longer
// kills the daemon on parent death, but an active tunnel still does.
//
// Driving a real `handoff` needs a headed Chromium, which does not belong in the
// free tier, so this pins the WIRING instead — the same static-tripwire approach
// used by cdp-session-cleanup.test.ts and server-auth.test.ts. If either half of
// the contract is dropped, the crash returns silently and these fail.
describe('watchdog is cancelled on runtime promotion to headed', () => {
// the contract is dropped, the crash returns silently and these fail. The
// behavioral halves (suppression + tunnel reaping) run in-process below.
describe('headed parent-death shutdown is suppressed on runtime promotion', () => {
const read = (rel: string) => fs.readFileSync(path.join(ROOT, rel), 'utf-8');
test('handoff() notifies the server that it promoted the daemon', () => {
@@ -181,17 +190,130 @@ describe('watchdog is cancelled on runtime promotion to headed', () => {
expect(src.slice(promote, promote + 800)).toContain('this.onHeadedPromotion?.()');
});
test('the server binds that callback to the watchdog canceller', () => {
test('the server binds that callback to the suppress-flag setter', () => {
const src = read('src/server.ts');
// The timer must be reachable — `setInterval(` with its return value dropped
// cannot be cleared, which was the original defect.
expect(src).toContain('parentWatchdogTimer = setInterval(');
expect(src).toContain('function clearParentWatchdog()');
expect(src).toContain('clearInterval(parentWatchdogTimer)');
expect(src).toContain('function suppressHeadedParentShutdown()');
// Bound on BOTH the module-level manager and any embedder-supplied one; the
// watchdog reads activeBrowserManager, so binding only the default instance
// leaves embedders (e.g. gbrowser) promoting silently.
expect(src).toContain('browserManager.onHeadedPromotion = clearParentWatchdog');
expect(src).toContain('cfgBrowserManager.onHeadedPromotion = clearParentWatchdog');
expect(src).toContain('browserManager.onHeadedPromotion = suppressHeadedParentShutdown');
expect(src).toContain('cfgBrowserManager.onHeadedPromotion = suppressHeadedParentShutdown');
});
test('promotion must NOT clear the interval — the tick doubles as the tunnel-orphan reaper', () => {
const src = read('src/server.ts');
// The original #2565 absorption cleared the ENTIRE interval on promotion.
// Sequence handoff → resume → /pair-agent tunnel then left an
// internet-exposed daemon that nothing reaps. The tick must stay
// registered and re-check the suppress flag + tunnelActive every pass.
expect(src).not.toContain('clearInterval(parentWatchdogTimer)');
expect(src).toContain('setInterval(parentWatchdogTick');
const tickStart = src.indexOf('function parentWatchdogTick(');
expect(tickStart).toBeGreaterThan(-1);
const tick = src.slice(tickStart, src.indexOf('\n}', tickStart));
expect(tick).toContain('headedParentShutdownSuppressed');
expect(tick).toContain('tunnelActive');
});
});
// ─── Behavioral: suppressed watchdog still reaps tunnel orphans ────────────
//
// In-process, via the same __testInternals__ seam server-factory.test.ts uses
// for idleCheckTick. parentWatchdogTick(deadPid) simulates the 15s poll
// discovering a dead parent; setTunnelActive simulates /pair-agent's
// tunnel-create flow; suppressHeadedParentShutdown is exactly what the
// handoff promotion callback invokes.
function makeMinimalConfig(mode: 'launched' | 'headed', tmpDir: string): ServerConfig {
const base = resolveConfig();
return {
authToken: 'watchdog-test-' + crypto.randomBytes(16).toString('hex'),
browsePort: 34567,
idleTimeoutMs: 1_800_000,
// State paths pointed at a scratch dir so shutdown()'s cleanup can never
// touch a real daemon's files on the machine running the tests.
config: { ...base, stateFile: path.join(tmpDir, 'browse-state.json'), stateDir: tmpDir },
browserManager: {
getConnectionMode: () => mode,
isWatching: () => false,
stopWatch: () => {},
close: async () => {},
onDisconnect: null,
} as any,
startTime: Date.now(),
// Skip terminal-agent teardown: identity files live under the REAL state
// dir conventions and this suite must stay hermetic.
ownsTerminalAgent: false,
};
}
describe('suppressed watchdog still reaps tunnel orphans (behavioral)', () => {
// A PID above darwin/linux default pid_max: process.kill(pid, 0) throws
// ESRCH, which the tick reads as "parent exited".
const DEAD_PID = 999_999;
let scratch: string;
const savedChromiumProfile = process.env.CHROMIUM_PROFILE;
beforeEach(() => {
scratch = fs.mkdtempSync(path.join(os.tmpdir(), 'watchdog-tick-'));
// shutdown() runs cleanSingletonLocks(resolveChromiumProfile()); point it
// at scratch so the operator's real profile is never inspected.
process.env.CHROMIUM_PROFILE = path.join(scratch, 'chromium-profile');
__resetRegistry();
__testInternals__.setTunnelActive(false);
__testInternals__.setLastActivity(Date.now());
__testInternals__.resetShutdownState();
__testInternals__.resetParentWatchdogState();
});
afterEach(() => {
if (savedChromiumProfile === undefined) delete process.env.CHROMIUM_PROFILE;
else process.env.CHROMIUM_PROFILE = savedChromiumProfile;
__testInternals__.setTunnelActive(false);
__testInternals__.resetShutdownState();
__testInternals__.resetParentWatchdogState();
try { fs.rmSync(scratch, { recursive: true, force: true }); } catch {}
});
// Drain the fire-and-forget shutdown promise chain (flushBuffers + close)
// the same way server-factory.test.ts does before asserting on exit.
async function drainShutdown(): Promise<void> {
await Promise.resolve();
await Promise.resolve();
await new Promise<void>((r) => setImmediate(r));
await new Promise<void>((r) => setImmediate(r));
}
test('after promotion suppression, parent death does NOT shut down a headed daemon (#2565)', async () => {
const exitMock = mock((_code?: number) => {});
const originalExit = process.exit;
(process as any).exit = exitMock;
try {
buildFetchHandler(makeMinimalConfig('headed', scratch));
__testInternals__.suppressHeadedParentShutdown(); // what handoff promotion triggers
__testInternals__.parentWatchdogTick(DEAD_PID);
await drainShutdown();
expect(exitMock).not.toHaveBeenCalled();
} finally {
(process as any).exit = originalExit;
}
});
test('CRITICAL: suppression active + tunnel live — parent death still shuts down', async () => {
const exitMock = mock((_code?: number) => {});
const originalExit = process.exit;
(process as any).exit = exitMock;
try {
buildFetchHandler(makeMinimalConfig('headed', scratch));
__testInternals__.suppressHeadedParentShutdown();
__testInternals__.setTunnelActive(true); // handoff → resume → /pair-agent tunnel
__testInternals__.parentWatchdogTick(DEAD_PID);
await drainShutdown();
// The tick is the ONLY reaper for tunnel orphans (idle timeout is
// disabled in tunnel mode). If this fails, an internet-exposed daemon
// outlives its parent forever.
expect(exitMock).toHaveBeenCalled();
} finally {
(process as any).exit = originalExit;
}
});
});