mirror of
https://github.com/garrytan/gstack.git
synced 2026-11-23 08:31:44 +01:00
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
This commit is contained in:
1 parent
bd0428974a
commit
5f3aa5b108
6 files changed
+60
-5
No files matched your search
+1
-1
@@ -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
|
||||
|
||||
|
||||
@@ -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<number> = new Set([10080]);
|
||||
|
||||
export function normalizePortError(err: unknown): Extract<PortCheckResult, { available: false }> {
|
||||
const maybeNodeError = err as NodeJS.ErrnoException | undefined;
|
||||
return {
|
||||
@@ -129,7 +136,10 @@ export async function findAvailablePort(explicitPort?: number | null): Promise<n
|
||||
|
||||
const attempts: FailedPortAttempt[] = [];
|
||||
for (let attempt = 0; attempt < RANDOM_PORT_RETRIES; attempt++) {
|
||||
const port = RANDOM_PORT_MIN + Math.floor(Math.random() * (RANDOM_PORT_MAX - RANDOM_PORT_MIN));
|
||||
let port: number;
|
||||
do {
|
||||
port = RANDOM_PORT_MIN + Math.floor(Math.random() * (RANDOM_PORT_MAX - RANDOM_PORT_MIN));
|
||||
} while (CHROMIUM_UNSAFE_PORTS.has(port));
|
||||
const result = await checkPortAvailable(port);
|
||||
if (result.available) {
|
||||
return port;
|
||||
|
||||
@@ -7,11 +7,12 @@
|
||||
* their traffic as phantom 404s across every Node test suite on the machine.
|
||||
*/
|
||||
|
||||
import { describe, test, expect } from 'bun:test';
|
||||
import { describe, test, expect, spyOn } from 'bun:test';
|
||||
import * as fs from 'fs';
|
||||
import * as net from 'net';
|
||||
import * as path from 'path';
|
||||
import {
|
||||
CHROMIUM_UNSAFE_PORTS,
|
||||
findAvailablePort,
|
||||
RANDOM_PORT_MIN,
|
||||
RANDOM_PORT_MAX,
|
||||
@@ -34,6 +35,24 @@ describe('shared port allocator (#2314)', () => {
|
||||
}
|
||||
});
|
||||
|
||||
// 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<number>((resolve, reject) => {
|
||||
|
||||
@@ -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.
|
||||
@@ -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);'
|
||||
|
||||
@@ -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');
|
||||
|
||||
Reference in new issue
Block a user