From 5f3aa5b1086c893d53d9cc5fc1a9a92deed23f6f Mon Sep 17 00:00:00 2001 From: Garry Tan Date: Fri, 20 Nov 2026 07:01:18 -0800 Subject: [PATCH] Browse daemon skips port 10080, which Chromium refuses, and a process-census test stops flaking (#4554) The browse daemon no longer picks port 10080, which Chromium refuses to open, so its welcome page can't fail with `net::ERR_UNSAFE_PORT`. This fixes two flaky free tests at their cause: the xvfb welcome test and the paid-shard retention check. Nothing for the reader to do. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> Review-Finding: p1/specialist-batched#1 reader=testing --- ARCHITECTURE.md | 2 +- browse/src/port-allocator.ts | 12 ++++++++- browse/test/terminal-agent-port-range.test.ts | 21 +++++++++++++++- .../flaky-fix-2026-11-20-port-and-census.md | 1 + test/bootstrap-retention.test.ts | 25 +++++++++++++++++++ test/helpers/bootstrap-retention.ts | 4 +-- 6 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 changelog.d/flaky-fix-2026-11-20-port-and-census.md diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md index 9da2950ec..b9f5518e7 100644 --- a/ARCHITECTURE.md +++ b/ARCHITECTURE.md @@ -95,7 +95,7 @@ The CLI reads this file to find the server. If the file is missing or the daemon ### Port selection -Random port between 10000-49151 (retry up to 5 on collision), allocated through the shared `browse/src/port-allocator.ts` so every long-lived gstack listener draws from the same range. The range ends at 49151 on purpose: 49152-65535 is the macOS ephemeral pool, and allocating inside it meant the OS could hand the same port to another process moments later. This means 10 Conductor workspaces can each run their own browse daemon with zero configuration and zero port conflicts. The old approach (scanning 9400-9409) broke constantly in multi-workspace setups. +Random port between 10000-49151 (retry up to 5 on collision; a draw on 10080, which Chromium refuses to load with `net::ERR_UNSAFE_PORT`, is redrawn), allocated through the shared `browse/src/port-allocator.ts` so every long-lived gstack listener draws from the same range. The range ends at 49151 on purpose: 49152-65535 is the macOS ephemeral pool, and allocating inside it meant the OS could hand the same port to another process moments later. This means 10 Conductor workspaces can each run their own browse daemon with zero configuration and zero port conflicts. The old approach (scanning 9400-9409) broke constantly in multi-workspace setups. ### Version auto-restart diff --git a/browse/src/port-allocator.ts b/browse/src/port-allocator.ts index 133747695..f81b768d9 100644 --- a/browse/src/port-allocator.ts +++ b/browse/src/port-allocator.ts @@ -30,6 +30,13 @@ export const RANDOM_PORT_MIN = 10000; export const RANDOM_PORT_MAX = 49151; // last port BELOW the macOS ephemeral pool (49152-65535) export const RANDOM_PORT_RETRIES = 5; +/** + * Ports inside the scan range that Chromium refuses to load (net::ERR_UNSAFE_PORT; + * kRestrictedPorts in chromium net/base/port_util.cc). A daemon bound to one + * serves pages its own browser can never open. + */ +export const CHROMIUM_UNSAFE_PORTS: ReadonlySet = new Set([10080]); + export function normalizePortError(err: unknown): Extract { const maybeNodeError = err as NodeJS.ErrnoException | undefined; return { @@ -129,7 +136,10 @@ export async function findAvailablePort(explicitPort?: number | null): Promise { } }); + // Value: protects=a random pick never lands on a port Chromium refuses to load (Free Tests 44539190714: the daemon took + // 10080 and the welcome page failed with net::ERR_UNSAFE_PORT); fails_when=the allocator returns a draw from + // CHROMIUM_UNSAFE_PORTS; why_new=nothing excluded browser-blocked ports; seam=Math.random; no_claim=explicit BROWSE_PORT values + test('a random draw on a Chromium-blocked port is redrawn', async () => { + expect(CHROMIUM_UNSAFE_PORTS.has(10080)).toBe(true); + const span = RANDOM_PORT_MAX - RANDOM_PORT_MIN; + const draws = [(10080 - RANDOM_PORT_MIN + 0.5) / span]; + const real = Math.random; + const random = spyOn(Math, 'random').mockImplementation(() => draws.shift() ?? real()); + try { + const port = await findAvailablePort(); + expect(port).not.toBe(10080); + expect(random.mock.calls.length).toBeGreaterThanOrEqual(2); + } finally { + random.mockRestore(); + } + }); + test('explicit free port is honored', async () => { // Find a free port by binding 0, then ask the allocator for exactly it. const free = await new Promise((resolve, reject) => { diff --git a/changelog.d/flaky-fix-2026-11-20-port-and-census.md b/changelog.d/flaky-fix-2026-11-20-port-and-census.md new file mode 100644 index 000000000..bfdf6f007 --- /dev/null +++ b/changelog.d/flaky-fix-2026-11-20-port-and-census.md @@ -0,0 +1 @@ +- **The browse daemon no longer picks port 10080, which Chromium refuses to open.** The daemon and terminal agent draw a random port from 10000-49151, and 10080 is on Chromium's blocked list, so about one start in 39,000 left the browser unable to load the daemon's own pages (the welcome page failed with `net::ERR_UNSAFE_PORT`). A draw on that port is now redrawn. One flaky free test is fixed too. The paid-shard retention check no longer fails when an unrelated short-lived process stops being readable and exits while it scans running processes. diff --git a/test/bootstrap-retention.test.ts b/test/bootstrap-retention.test.ts index b8f1fb0d7..bb21cd9a9 100644 --- a/test/bootstrap-retention.test.ts +++ b/test/bootstrap-retention.test.ts @@ -126,6 +126,31 @@ describe.skipIf(process.platform !== 'linux')('bootstrap attempt retention bound expect(() => process.kill(child.pid!, 0)).not.toThrow(); }); + // Value: protects=scope creation survives a same-uid process that denies its fd table and exits mid-census (the + // flaky free test on 2026-11-20); fails_when=that denial throws again or the exited process is recorded as + // uninspectable; why_new=only live denials were covered; seam=fs.readdirSync and fs.readFileSync on that pid's /proc + test('scope creation skips an owned process that exits after denying its fd table', () => { + const child = spawn('sleep', ['30'], { detached: true, stdio: 'ignore' }); + children.push(child); + const realDir = fs.readdirSync; + const realFile = fs.readFileSync; + let stats = 0; + const dir = spyOn(fs, 'readdirSync').mockImplementation(((file: any, ...args: any[]) => { + if (file === `/proc/${child.pid}/fd`) throw Object.assign(new Error('fd table denied'), { code: 'EACCES' }); + return (realDir as any)(file, ...args); + }) as any); + const read = spyOn(fs, 'readFileSync').mockImplementation(((file: any, ...args: any[]) => { + if (file === `/proc/${child.pid}/stat` && ++stats > 1) throw Object.assign(new Error('exited'), { code: 'ENOENT' }); + return (realFile as any)(file, ...args); + }) as any); + try { + const { scope } = fixture(); + const census = JSON.parse(Object.values(scope.env)[0]!); + expect(stats).toBe(2); + expect(census.uninspectable.some((native: any) => native.pid === child.pid)).toBe(false); + } finally { dir.mockRestore(); read.mockRestore(); } + }); + test.each(['new process', 'new denial', 'different lifetime'])('%s cannot inherit an unrelated process census exclusion', async scenario => { const child = scenario === 'new process' ? undefined : await censusProcess(scenario === 'different lifetime' ? 'dumpable(0);' diff --git a/test/helpers/bootstrap-retention.ts b/test/helpers/bootstrap-retention.ts index 390dfb314..9dd953a6a 100644 --- a/test/helpers/bootstrap-retention.ts +++ b/test/helpers/bootstrap-retention.ts @@ -159,8 +159,8 @@ export function createBootstrapRetentionScope(temporaryRoot: string, durableRoot fs.readdirSync(`/proc/${pid}/fd`); } catch (error: any) { if (error.code === 'ENOENT' || error.code === 'ESRCH') continue; - if (native && (error.code === 'EACCES' || error.code === 'EPERM') && alive(native)) uninspectable.push(native); - else throw error; + if (!native || (error.code !== 'EACCES' && error.code !== 'EPERM')) throw error; + if (alive(native)) uninspectable.push(native); } } if (path.resolve(durableRoot) === temporary.path || inside(temporary.path, path.resolve(durableRoot))) throw new Error('artifact root must survive temporary cleanup');