From 3c9cda0a2d678777d6487c545b9cbb8e26618485 Mon Sep 17 00:00:00 2001 From: Garry Tan Date: Sun, 22 Nov 2026 05:02:19 -0800 Subject: [PATCH] Free test shards stop failing when a test's browser exits during the cleanup check (GST-3447) (#4666) Free test shards no longer fail with "native process identity unavailable" when a browser a test started exits while the runner is checking it, and the Free Tests planning job gets 10 minutes instead of 5. Why: these were the two causes left of main's flapping Free Tests runs; the other four flaps from 11-18 and 11-19 were fixed earlier by #4490 and #4530. Nothing to do. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> --- .github/workflows/free-tests.yml | 2 +- ...ests-browser-exit-during-identity-check.md | 1 + scripts/test-free-shards.ts | 11 ++++--- test/helpers/free-owned-browser-settlement.ts | 31 ++++++++++++++++--- 4 files changed, 36 insertions(+), 9 deletions(-) create mode 100644 changelog.d/free-tests-browser-exit-during-identity-check.md diff --git a/.github/workflows/free-tests.yml b/.github/workflows/free-tests.yml index d33924a95..1b5233359 100644 --- a/.github/workflows/free-tests.yml +++ b/.github/workflows/free-tests.yml @@ -218,7 +218,7 @@ jobs: # whose only artifact is free-plan as one that ran no shard. free-plan: runs-on: ubuntu-24.04 - timeout-minutes: 5 + timeout-minutes: 10 outputs: matrix: ${{ steps.plan.outputs.matrix }} runner: ${{ steps.plan.outputs.runner }} diff --git a/changelog.d/free-tests-browser-exit-during-identity-check.md b/changelog.d/free-tests-browser-exit-during-identity-check.md new file mode 100644 index 000000000..e2ceecade --- /dev/null +++ b/changelog.d/free-tests-browser-exit-during-identity-check.md @@ -0,0 +1 @@ +**Free test shards no longer fail when a test's browser exits while the runner checks who owns it.** The shard's cleanup tracker treated a browser process that ended on its own during its `ps` identity check as an ownership failure and failed the shard with "native process identity unavailable" (quick-bun-default on main, run 44850005025); a process that is gone now just ends that check, while a failed check of a live process still fails the shard. The Free Tests planning job also gets 10 minutes instead of 5, so a slow GitHub runner start no longer cancels it. diff --git a/scripts/test-free-shards.ts b/scripts/test-free-shards.ts index e439a3a2f..6c73bbf7a 100755 --- a/scripts/test-free-shards.ts +++ b/scripts/test-free-shards.ts @@ -1273,6 +1273,7 @@ const EPILOGUE_WORD: Record = { function trackShardBrowser(stateDir: string, env: NodeJS.ProcessEnv) { class BrowserCleanupError extends Error {} class CaptureStopped extends Error {} + class ProcessExited extends Error {} type Identity = { pid: number; parent: number; start: string; daemon: number; root: boolean }; type Capture = { abort: AbortController; deadline: number; probes: Set>; records?: [string, string] }; const identities = new Map(); @@ -1341,7 +1342,7 @@ function trackShardBrowser(stateDir: string, env: NodeJS.ProcessEnv) { }; const failure = (error: unknown) => { - if (error instanceof CaptureStopped) return; + if (error instanceof CaptureStopped || error instanceof ProcessExited) return; const code = (error as NodeJS.ErrnoException)?.code; const file = (error as NodeJS.ErrnoException & { path?: string })?.path; const pid = typeof file === 'string' ? /^\/proc\/(\d+)\//.exec(file)?.[1] : undefined; @@ -1415,10 +1416,11 @@ function trackShardBrowser(stateDir: string, env: NodeJS.ProcessEnv) { const key = `${identity.pid}:${identity.start}`; const recorded = nativeStarts.get(key); if (recorded) return recorded; - if (!await live(capture, identity)) throw new BrowserCleanupError('process exited before its native identity was captured'); + if (!await live(capture, identity)) throw new ProcessExited('process exited before its native identity was captured'); const result = await probe(capture, 'ps', ['-p', String(identity.pid), '-o', 'lstart='], 2000); const value = result.status === 0 ? result.stdout.trim().replace(/\s+/g, ' ') : ''; - if (!value || !await live(capture, identity)) throw new BrowserCleanupError('native process identity unavailable'); + if (!await live(capture, identity)) throw new ProcessExited('process exited during its native identity probe'); + if (!value) throw new BrowserCleanupError('native process identity unavailable'); check(capture); nativeStarts.set(key, value); return value; @@ -1503,7 +1505,8 @@ function trackShardBrowser(stateDir: string, env: NodeJS.ProcessEnv) { if (state?.chromiumPid !== undefined) validations.push(validateChild(state.chromiumPid, state.chromiumStartTime, state.pid)); const results = await Promise.allSettled(validations); check(operation); - for (const result of results) if (result.status === 'rejected') throw result.reason; + const rejected = results.flatMap(result => result.status === 'rejected' ? [result.reason] : []); + if (rejected.length) throw rejected.find(reason => !(reason instanceof ProcessExited)) ?? rejected[0]; operation.records = [JSON.stringify(state), JSON.stringify(agent)]; } catch (error) { check(operation); diff --git a/test/helpers/free-owned-browser-settlement.ts b/test/helpers/free-owned-browser-settlement.ts index c1a488154..14cce9969 100644 --- a/test/helpers/free-owned-browser-settlement.ts +++ b/test/helpers/free-owned-browser-settlement.ts @@ -15,7 +15,8 @@ import type { eligibleFreeRetryFiles as EligibleFreeRetryFiles } from '../../scr const ROOT = path.resolve(import.meta.dir, '../..'); export const SETTLEMENT_MODE_GROUPS = { - lifecycle: ['success', 'failure', 'timeout', 'cancel', 'cancel-force', 'cancel-cold-probes', 'settle-cold-probes', 'settle-slow-exit', 'cancel-vanishing-record'], + lifecycle: ['success', 'failure', 'timeout', 'cancel', 'cancel-force', 'cancel-cold-probes', 'settle-cold-probes', 'settle-slow-exit', 'cancel-vanishing-record', + 'identity-probe-exit'], ownership: ['endpoint-mix', 'full-state-mix', 'terminal-mix', 'chromium-mix', 'replaced-pid', 'stale-child-start', 'replaced-start', 'exit-environment-race', 'unavailable-environment', 'directory-remove-failure'], } as const; @@ -163,7 +164,7 @@ if (role === 'leaf') { const {runFreeShard} = await import(${JSON.stringify(path.join(ROOT, 'scripts/test-free-shards.ts'))}); const result = await runFreeShard(['ownership-fixture'], 1, 1, { rootDir:${JSON.stringify(ROOT)}, quiet:true, log:() => {}, wallTimeoutMs:mode === 'timeout' ? 1200 : 15000, - env:mode.endsWith('cold-probes') ? {...process.env, PATH:process.env.GSTACK_FIXTURE_PATH} : process.env, + env:process.env.GSTACK_FIXTURE_PATH ? {...process.env, PATH:process.env.GSTACK_FIXTURE_PATH} : process.env, commandFor:() => ({command:process.execPath, args:[import.meta.filename, 'shard', directory, mode]}), }); write(file('.result'), result); @@ -185,7 +186,8 @@ function ownershipProcessAlive(identity: { pid: number; start: string; ticks: st /** The caller passes the runner's retry-eligibility rule: an unattributed settlement failure must never be retried. */ export function registerOwnedBrowserSettlementCases(modes: readonly string[], { eligibleFreeRetryFiles }: { eligibleFreeRetryFiles: typeof EligibleFreeRetryFiles }): void { for (const mode of modes) { - test.skipIf(process.platform === 'win32' || ((['replaced-start', 'exit-environment-race', 'unavailable-environment', 'settle-slow-exit', 'cancel-vanishing-record'].includes(mode) || mode.endsWith('cold-probes')) && process.platform !== 'linux'))(mode, async () => { + const probeShim = mode.endsWith('cold-probes') || mode === 'identity-probe-exit'; + test.skipIf(process.platform === 'win32' || ((['replaced-start', 'exit-environment-race', 'unavailable-environment', 'settle-slow-exit', 'cancel-vanishing-record'].includes(mode) || probeShim) && process.platform !== 'linux'))(mode, async () => { const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'free-owned-browser-')); const actor = path.join(directory, 'actor.ts'); fs.writeFileSync(actor, OWNERSHIP_ACTOR); @@ -206,6 +208,26 @@ export function registerOwnedBrowserSettlementCases(modes: readonly string[], { process.exit(code); `, { mode: 0o755 }); } + } else if (mode === 'identity-probe-exit') { + // Run 44850005025 (quick-bun-default, 41729a02): an owned browser child exited while the runner's + // `ps -o lstart=` identity probe ran, and the shard failed with 'native process identity unavailable'. + // This ps kills the recorded Chromium child during its first identity probe, then answers as the real ps. + fs.mkdirSync(bin); + fs.writeFileSync(path.join(bin, 'ps'), `#!/usr/bin/env bun + import * as fs from 'node:fs'; + const args = process.argv.slice(2); + const ready = ${JSON.stringify(path.join(directory, 'owned.ready'))}; + const marker = ${JSON.stringify(path.join(directory, 'owned.probe-exit'))}; + const pid = Number(args[args.indexOf('-p') + 1]); + if (args.includes('lstart=') && !fs.existsSync(marker) && fs.existsSync(ready) && JSON.parse(fs.readFileSync(ready, 'utf8')).state.chromiumPid === pid) { + process.kill(pid, 'SIGKILL'); + for (const until = Date.now() + 1000; Date.now() < until; await Bun.sleep(5)) { + try { if (/\\) [ZX] /.test(fs.readFileSync('/proc/' + pid + '/stat', 'utf8'))) break; } catch { break; } + } + fs.writeFileSync(marker, String(pid)); + } + process.exit(await Bun.spawn([${JSON.stringify(Bun.which('ps'))}, ...args], {stdout:'inherit', stderr:'inherit', timeout:1000}).exited); + `, { mode: 0o755 }); } const waitFor = async (filename: string) => { const deadline = Date.now() + 8000; @@ -223,7 +245,7 @@ export function registerOwnedBrowserSettlementCases(modes: readonly string[], { processes.push(sibling); await waitFor(path.join(directory, 'sibling.ready')); const harness = Bun.spawn([process.execPath, actor, 'harness', directory, mode], { - env: mode.endsWith('cold-probes') ? { ...process.env, PATH: bin + path.delimiter + process.env.PATH, GSTACK_FIXTURE_PATH: process.env.PATH } : process.env, + env: probeShim ? { ...process.env, PATH: bin + path.delimiter + process.env.PATH, GSTACK_FIXTURE_PATH: process.env.PATH } : process.env, stdout: 'ignore', stderr: 'pipe', }); processes.push(harness); @@ -252,6 +274,7 @@ export function registerOwnedBrowserSettlementCases(modes: readonly string[], { // Run 13 (feaa28d) and a 16-vCPU stress run: the cancelled daemon's state file // vanished between the existence check and the read ('browser ownership unavailable'). if (mode === 'cancel-vanishing-record') expect(fs.existsSync(path.join(directory, 'owned.vanished'))).toBe(true); + if (mode === 'identity-probe-exit') expect(fs.existsSync(path.join(directory, 'owned.probe-exit'))).toBe(true); if (invalid) { expect(fs.existsSync(path.dirname(own.stateFile))).toBe(true); expect(fs.existsSync(path.join(directory, 'owned.interrupted'))).toBe(mode === 'directory-remove-failure');