Skip to content

test(api): isolate loopback ports across worker processes - #891

Merged
juliopolycarpo merged 6 commits into
mainfrom
test/cross-worker-races
Aug 19, 2026
Merged

juliopolycarpo merged 6 commits into
mainfrom
test/cross-worker-races

Conversation

@juliopolycarpo

@juliopolycarpo juliopolycarpo commented Aug 19, 2026 •

Copy link
Copy Markdown
Owner

Summary

  • Make API OAuth tests safe under Bun worker isolation by assigning OS ports per process instead of sharing fixed loopback ports.
  • Build ChatGPT authorize and token requests from the same bound port; production still uses the registered port 1455 callback.
  • Remove managed test config directories during teardown and document how to reproduce cross-worker races and the remaining upstream unit-lane blocker.
  • Closes Test lanes hide cross-file races behind their parallelism settings #888.

Changes

  • Return the port actually bound by the shared OAuth loopback server and add a ChatGPT test override that uses port 0.
  • Derive ChatGPT redirect_uri consistently for authorization and token exchange; update busy-port tests to arrange collisions explicitly.
  • Update provider-agnostic loopback tests to use OS-assigned ports, use BUN_TEST_WORKER_ID for test config paths, and remove those paths in afterAll.
  • Add testing guidance covering fixed-port races, whole-lane reproduction, and Bun issue #37968.

Test Plan

  • bun run check passes
  • bun run test passes
  • bun run build passes
  • New UI strings use @mangostudio/shared/i18n (no new UI strings)
  • Manually smoke-tested: login, chat creation, image generation

Screenshots / GIFs

Not applicable — backend, test infrastructure, and documentation changes only.

Notes

No new environment variables, schema changes, or breaking changes. Production continues to bind the registered ChatGPT callback port. The unit lane remains without worker parallelism pending upstream Bun fixes; the integration lane is covered by the passing CI test job.

The ChatGPT sign-in flow binds the loopback port OpenAI registered for its
public client, 127.0.0.1:1455. Several test files drive that flow, so running
a lane in worker processes makes them compete for one machine-wide resource:
the loser gets a spurious 503 ChatGptOAuthPortBusyError and the file that
raised it loses its remaining cases. Running the suite at all also collides
with a real `codex login` on the developer's machine.

The loopback server now reports the port it actually bound, and both the
authorize request and the token exchange derive their redirect URI from it
instead of from the constant. Production still binds the registered port, so
the redirect URI it sends is unchanged. setChatGptLoopbackPortForTest lets the
test environment pin the port to 0, so every test process takes an OS-assigned
one; the two cases that need a busy port now hold one themselves rather than
squatting on a well-known number.

The provider-agnostic loopback test shared the same shape with a fixed port
14559 and is fixed the same way.

Signed-off-by: Julio Polycarpo <julio@polycarpo.dev>
Every API test process creates ~/.mangostudio-test-<pid>-<worker> and nothing
ever removed it, so each run left one more behind: this checkout had 3178 of
them, 13 MB, in the user's home directory. `process.on('exit')` does not fire
under the Bun test runner, but an `afterAll` registered by the shared setup
does — once per isolate with `--isolate`, once per process without it — so the
directory is gone by the time the runner exits either way.

The worker component of that name was reading BUN_WORKER_ID, which Bun does
not set; every directory ever created was suffixed `-0`. Bun 1.3.14 exports
BUN_TEST_WORKER_ID, so the name now identifies the worker it belongs to. The
pid was already doing the isolating, so this changes no behaviour beyond the
name.

Signed-off-by: Julio Polycarpo <julio@polycarpo.dev>
The parallelism section carried the flag semantics and the cost table but not
what actually breaks when the worker count goes up. It now names the one race
the integration lane had, states the rule it came from — never bind a
well-known port in a test, ask for 0 and read the bound port back — and records
the reproduction technique that made an intermittent failure reliable: pin the
run to CI's core count with taskset and capture whole logs.

It also records the unit lane's remaining blocker as upstream, with the
measurements, so the next reader does not spend the day re-deriving that the
google-auth-library frames in the stack are the messenger rather than the cause.

Signed-off-by: Julio Polycarpo <julio@polycarpo.dev>
@juliopolycarpo juliopolycarpo added type: test Test coverage, test infrastructure, or test reliability work. area: api Elysia routes, API modules, middleware, or backend services. labels Aug 19, 2026
@github-actions github-actions Bot added area: connectors API key connectors, secret persistence, connector configuration, or sync. area: build Binary build, runtime paths, packaging, or standalone output. area: docs Documentation tree, guides, reference docs, or contributor docs. labels Aug 19, 2026
@coderabbitai

coderabbitai Bot commented Aug 19, 2026 •

Copy link
Copy Markdown

Important

  • 🔍 Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 23d45e4a-cf6d-4587-b73d-5604d8ecd4ab


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

github-actions Bot commented Aug 19, 2026 •

Copy link
Copy Markdown

PR head: 35a1011 • base: c10501b • CI run

Commits — 6 commits

Base c10501b → head 35a1011, oldest first.

  • b8abab0 test(api): stop test files racing for the ChatGPT loopback port
  • 71e4460 test(api): remove the managed test config directory on teardown
  • a598fb0 docs(testing): record the named cross-worker races and how to find them
  • 467fecd docs(testing): scope the unit-lane blocker to where it was measured
  • b68e9f8 docs(testing): confirm the unit-lane blocker on a CI runner
  • 35a1011 docs(testing): point the unit-lane blocker at its upstream issue
Full commit messages

b8abab0 test(api): stop test files racing for the ChatGPT loopback port

test(api): stop test files racing for the ChatGPT loopback port

The ChatGPT sign-in flow binds the loopback port OpenAI registered for its
public client, 127.0.0.1:1455. Several test files drive that flow, so running
a lane in worker processes makes them compete for one machine-wide resource:
the loser gets a spurious 503 ChatGptOAuthPortBusyError and the file that
raised it loses its remaining cases. Running the suite at all also collides
with a real `codex login` on the developer's machine.

The loopback server now reports the port it actually bound, and both the
authorize request and the token exchange derive their redirect URI from it
instead of from the constant. Production still binds the registered port, so
the redirect URI it sends is unchanged. setChatGptLoopbackPortForTest lets the
test environment pin the port to 0, so every test process takes an OS-assigned
one; the two cases that need a busy port now hold one themselves rather than
squatting on a well-known number.

The provider-agnostic loopback test shared the same shape with a fixed port
14559 and is fixed the same way.

Signed-off-by: Julio Polycarpo <julio@polycarpo.dev>

71e4460 test(api): remove the managed test config directory on teardown

test(api): remove the managed test config directory on teardown

Every API test process creates ~/.mangostudio-test-<pid>-<worker> and nothing
ever removed it, so each run left one more behind: this checkout had 3178 of
them, 13 MB, in the user's home directory. `process.on('exit')` does not fire
under the Bun test runner, but an `afterAll` registered by the shared setup
does — once per isolate with `--isolate`, once per process without it — so the
directory is gone by the time the runner exits either way.

The worker component of that name was reading BUN_WORKER_ID, which Bun does
not set; every directory ever created was suffixed `-0`. Bun 1.3.14 exports
BUN_TEST_WORKER_ID, so the name now identifies the worker it belongs to. The
pid was already doing the isolating, so this changes no behaviour beyond the
name.

Signed-off-by: Julio Polycarpo <julio@polycarpo.dev>

a598fb0 docs(testing): record the named cross-worker races and how to find them

docs(testing): record the named cross-worker races and how to find them

The parallelism section carried the flag semantics and the cost table but not
what actually breaks when the worker count goes up. It now names the one race
the integration lane had, states the rule it came from — never bind a
well-known port in a test, ask for 0 and read the bound port back — and records
the reproduction technique that made an intermittent failure reliable: pin the
run to CI's core count with taskset and capture whole logs.

It also records the unit lane's remaining blocker as upstream, with the
measurements, so the next reader does not spend the day re-deriving that the
google-auth-library frames in the stack are the messenger rather than the cause.

Signed-off-by: Julio Polycarpo <julio@polycarpo.dev>

467fecd docs(testing): scope the unit-lane blocker to where it was measured

docs(testing): scope the unit-lane blocker to where it was measured

Every measurement of the epoll_ctl abort comes from one WSL2 host, and
epoll_ctl is kernel surface, so "the unit lane cannot take worker parallelism"
was a wider claim than the evidence carries. Say what was measured and where,
and mark the runner case as untested rather than implied.

Signed-off-by: Julio Polycarpo <julio@polycarpo.dev>

b68e9f8 docs(testing): confirm the unit-lane blocker on a CI runner

docs(testing): confirm the unit-lane blocker on a CI runner

The previous commit marked the runner case untested. It has now been tested: a
non-blocking matrix on ubuntu-latest reproduced the abort in 2 of 36 runs of the
unit lane at four workers, on a 4-core Azure kernel, so it is a property of the
lane rather than of one WSL2 box. The same matrix ran the integration lane 12
times without a failure at 50.6s, which is the other half of the answer — that
lane is ready, the unit lane is not.

Signed-off-by: Julio Polycarpo <julio@polycarpo.dev>

35a1011 docs(testing): point the unit-lane blocker at its upstream issue

docs(testing): point the unit-lane blocker at its upstream issue

It is oven-sh/bun#37968, already reproduced and root-caused by Bun — a leaked
epoll registration on a reused stdio descriptor at the isolate global swap —
with a fix open at oven-sh/bun#38008. Naming it saves the next reader the
investigation, and gives them the one thing worth checking first: whether the
fix has shipped.

Its recommended workaround, regular files instead of pipes, made the failure
likelier rather than rarer in this suite, so the note says to measure before
adopting it.

Signed-off-by: Julio Polycarpo <julio@polycarpo.dev>

📝 Changelog Preview

Entries this branch would add to the changelog on release:

[Unreleased]

📚 Documentation

  • (testing) Point the unit-lane blocker at its upstream issue
  • (testing) Confirm the unit-lane blocker on a CI runner
  • (testing) Scope the unit-lane blocker to where it was measured
  • (testing) Record the named cross-worker races and how to find them

🧪 Testing

  • (api) Remove the managed test config directory on teardown
  • (api) Stop test files racing for the ChatGPT loopback port
CI duration (wall clock, critical path, slowest jobs)

CI Duration

Report-only timing from the Actions jobs API; runner variance does not affect the gate.
Comparisons cover the jobs both runs share, so main-only stages never skew the baseline.

Metric Base Head Δ
Wall clock (shared jobs) 9m 4s 19m 11s 🔴 ▲ +10m 7s
Critical path Test / Unit & Integration Tests · 7m 15s Test / Unit & Integration Tests · 7m 33s 🔴 ▲ +18s

Since previous PR run: 9m 2s → 19m 11s (🔴 ▲ +10m 9s)

Five slowest head jobs

Job Base Head Δ
Test / Unit & Integration Tests 7m 15s 7m 33s 🔴 ▲ +18s
Smoke — Browser / Chromium smoke suite 2m 4s 6m 44s 🔴 ▲ +4m 40s
Distribution / Build immutable distribution 3m 30s 3m 5s 🟢 ▼ -25s
QA Metrics / Collect 1m 32s 1m 37s 🔴 ▲ +5s
Smoke — Binary / Binary windows-arm64 45s 49s 🔴 ▲ +4s

QA Gate — Coverage & Quality

Base: c10501b • Head: bdda098 • generated 2026-08-19T08:21:04.094Z

✅ No attention signals — collected metrics look healthy against base.

LoC (code): 🔴 ▲ +61 • Line coverage (all workspaces): ⚪ ▲ = 0 • Quick check: pass • Duplication: 🟢 ▼ -0pp • Bundle gzip: ⚪ ▲ = 0 • Locked deps: ⚪ ▲ = 0 • Tests passed: 🟢 ▲ +2

Metric details (coverage, LoC, bundle, dependencies, tests, duplication, tooling)

Coverage

API/shared/runtime branches and statements are source-derived from LCOV line hits because Bun LCOV does not emit branch or statement records.

Workspace Metric Base Head Δ
frontend lines 86.19% (13,713/15,911) 86.19% (13,713/15,911) ⚪ ▲ = 0
frontend statements 76.07% (7,825/10,287) 76.07% (7,825/10,287) ⚪ ▲ = 0
frontend functions 72.75% (2,411/3,314) 72.75% (2,411/3,314) ⚪ ▲ = 0
frontend branches 68.04% (5,636/8,283) 68.04% (5,636/8,283) ⚪ ▲ = 0
api lines 82.25% (69,555/84,568) 82.25% (69,566/84,579) ⚪ ▲ = 0
api statements 81.43% (28,008/34,395) 81.44% (28,018/34,405) 🟢 ▲ +0.01pp
api functions 85.03% (7,016/8,251) 85.02% (7,017/8,253) 🔴 ▼ -0.01pp
api branches 49.75% (10,469/21,043) 49.75% (10,473/21,051) ⚪ ▲ = 0
shared lines 97.85% (14,380/14,696) 97.85% (14,380/14,696) ⚪ ▲ = 0
shared statements 94.68% (2,618/2,765) 94.68% (2,618/2,765) ⚪ ▲ = 0
shared functions 92.87% (430/463) 92.87% (430/463) ⚪ ▲ = 0
shared branches 60.82% (902/1,483) 60.82% (902/1,483) ⚪ ▲ = 0
runtime lines 81.90% (25,785/31,484) 81.89% (25,783/31,484) 🔴 ▼ -0.01pp
runtime statements 79.02% (9,472/11,987) 79.00% (9,470/11,987) 🔴 ▼ -0.02pp
runtime functions 78.02% (1,988/2,548) 78.02% (1,988/2,548) ⚪ ▲ = 0
runtime branches 48.96% (3,635/7,425) 48.94% (3,634/7,425) 🔴 ▼ -0.02pp

Lines of Code

Workspace Base Head Δ
frontend 590 files / 68,052 lines 590 files / 68,052 lines files ⚪ ▲ = 0 • code ⚪ ▲ = 0
api 989 files / 144,569 lines 989 files / 144,630 lines files ⚪ ▲ = 0 • code 🔴 ▲ +61
shared 197 files / 27,217 lines 197 files / 27,217 lines files ⚪ ▲ = 0 • code ⚪ ▲ = 0
runtime 833 files / 42,015 lines 833 files / 42,015 lines files ⚪ ▲ = 0 • code ⚪ ▲ = 0
total 2,609 files / 281,853 lines 2,609 files / 281,914 lines files ⚪ ▲ = 0 • code 🔴 ▲ +61

Frontend Bundle

Metric Base Head Δ
gzip total 843.1 KiB 843.1 KiB ⚪ ▲ = 0
gzip JavaScript 827.2 KiB 827.2 KiB ⚪ ▲ = 0
gzip CSS 15.0 KiB 15.0 KiB ⚪ ▲ = 0
gzip HTML 947 B 947 B ⚪ ▲ = 0
tracked files 35 35 ⚪ ▲ = 0

Dependencies

Metric Base Head Δ
locked packages 859 859 ⚪ ▲ = 0
direct dependencies 43 43 ⚪ ▲ = 0
direct devDependencies 41 41 ⚪ ▲ = 0
workspace manifests 5 5 ⚪ ▲ = 0

Tests

Single full-suite pass (unit + integration, from the coverage run).

Base Head Δ passed
6,564 passed (root 6,564 / frontend 0 / api 0 / shared 0 / runtime 0) · exit 0 · 409s 6,566 passed (root 6,566 / frontend 0 / api 0 / shared 0 / runtime 0) · exit 0 · 417s 🟢 ▲ +2

Code Duplication (jscpd)

Metric Base Head Δ
clones 1,233 1,233 ⚪ ▲ = 0
duplicated lines 14,477 14,476 🟢 ▼ -1
percentage 4.12% 4.12% 🟢 ▼ -0pp

Repo Tooling

Metric Base Head Δ
Full repo check pass pass ⚪ ▲ = 0
TS errors (total) 0 0 ⚪ ▲ = 0
Circular dependencies 0 0 ⚪ ▲ = 0

Every measurement of the epoll_ctl abort comes from one WSL2 host, and
epoll_ctl is kernel surface, so "the unit lane cannot take worker parallelism"
was a wider claim than the evidence carries. Say what was measured and where,
and mark the runner case as untested rather than implied.

Signed-off-by: Julio Polycarpo <julio@polycarpo.dev>
The previous commit marked the runner case untested. It has now been tested: a
non-blocking matrix on ubuntu-latest reproduced the abort in 2 of 36 runs of the
unit lane at four workers, on a 4-core Azure kernel, so it is a property of the
lane rather than of one WSL2 box. The same matrix ran the integration lane 12
times without a failure at 50.6s, which is the other half of the answer — that
lane is ready, the unit lane is not.

Signed-off-by: Julio Polycarpo <julio@polycarpo.dev>
It is oven-sh/bun#37968, already reproduced and root-caused by Bun — a leaked
epoll registration on a reused stdio descriptor at the isolate global swap —
with a fix open at oven-sh/bun#38008. Naming it saves the next reader the
investigation, and gives them the one thing worth checking first: whether the
fix has shipped.

Its recommended workaround, regular files instead of pipes, made the failure
likelier rather than rarer in this suite, so the note says to measure before
adopting it.

Signed-off-by: Julio Polycarpo <julio@polycarpo.dev>
@juliopolycarpo juliopolycarpo changed the title test(api): remove the cross-file state that fails under worker isolation test(api): isolate loopback ports across worker processes Aug 19, 2026
@juliopolycarpo
juliopolycarpo merged commit 2eab2f2 into main Aug 19, 2026
59 of 61 checks passed
@juliopolycarpo
juliopolycarpo deleted the test/cross-worker-races branch August 19, 2026 09:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: api Elysia routes, API modules, middleware, or backend services. area: build Binary build, runtime paths, packaging, or standalone output. area: connectors API key connectors, secret persistence, connector configuration, or sync. area: docs Documentation tree, guides, reference docs, or contributor docs. type: test Test coverage, test infrastructure, or test reliability work.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test lanes hide cross-file races behind their parallelism settings

1 participant