Repository navigation
test(sql): share one mock server per file in postgres fault-injection tests - #33962
Conversation
… tests postgres-binary-array-bounds.test.ts and postgres-invalid-message-length.test.ts were creating a fresh net.Server per test case (7 and 10 respectively) and closing it in finally. On Windows CI lanes both files started intermittently failing with ERR_POSTGRES_CONNECTION_REFUSED from the 2nd or 3rd case on, across every retry of the test process, then recovering after ~30-50s. In the failing shards both files run a few positions after node-http-uaf.test.ts, whose fixture issues ~22000 localhost connections and leaves tens of thousands of TIME_WAIT sockets. bun disables SYN retransmission for loopback connects on Windows (SIO_TCP_INITIAL_RTO in packages/bun-usockets/src/bsd.c), so a lost SYN in that state becomes an immediate connect failure, which the SQL client surfaces as ERR_POSTGRES_CONNECTION_REFUSED without retry. Both files now start a single long-lived listener at module scope and latch the per-test payload into a module-level variable that the connection handler reads at accept time. The assertions are unchanged. Each file needs one listen port and a handful of client ports for the whole run, removing the per-test listen()+close() churn that tripped the loopback path. The combined run also drops from ~395ms to ~150ms on Windows release. Refs #33622 (LPT sharding placed these after node-http-uaf on Windows).
|
Updated 9:06 PM PT - Jul 10th, 2026
⏳ @robobun, your commit 8c1de5d is still building in |
WalkthroughChangesPostgres mock lifecycle
🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@test/js/sql/postgres-binary-array-bounds.test.ts`:
- Around line 43-51: The rationale comment above the shared `current` state is
overly detailed. Trim it to three lines: retain that one mock server is shared
for the file and that each connection reads the `current` payload set before
connecting, while removing the Windows, SYN retransmission, and historical
flakiness explanation.
In `@test/js/sql/postgres-invalid-message-length.test.ts`:
- Around line 21-28: Trim the comment above the shared `current` mock-server
state to three lines, retaining only that one server is reused for the file and
each test sets `current` before connecting to avoid Windows CI
connection-refused flakes.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: 42bec14b-34fc-45f3-a0f4-19da07efe1f9
📒 Files selected for processing (2)
test/js/sql/postgres-binary-array-bounds.test.tstest/js/sql/postgres-invalid-message-length.test.ts
There was a problem hiding this comment.
LGTM — test-only de-flake that hoists the mock server to module scope; assertions unchanged.
What was reviewed:
currentis destructured into locals at accept time, so each connection latches its own payload; tests run serially so the module-level mutation is safe.connectError's switch fromsocket.onceto the sharedstartup-flag handler is behaviorally equivalent (noatQuery→ later data is ignored).afterAll(server.close)mirrors the prior per-test close ordering (afterdb.close()), so no new hang risk.- Comment blocks are long but document a non-obvious Windows-specific mechanism (SIO_TCP_INITIAL_RTO / no SYN retransmit) — treated as acceptable rationale, not bug-history noise.
Extended reasoning...
Overview
This PR refactors two postgres fault-injection test files to share a single long-lived net.Server per file instead of creating/closing one per test case. Per-test payload is stored in a module-level current variable that the connection handler destructures at accept time. No runtime code is touched; all test assertions are byte-for-byte unchanged.
Security risks
None. Test-only change to mock-server lifecycle in test/js/sql/. No production code, no auth/crypto/permissions paths.
Level of scrutiny
Low. This is a mechanical test-infrastructure refactor to reduce listen()/close() churn that was causing Windows CI flakes under ephemeral-port pressure. The key correctness question is whether sharing state via a module-level let current is race-safe: it is, because (a) bun:test runs test() cases serially by default and neither file uses test.concurrent, and (b) the handler captures current into connection-local consts at accept time, so a lingering socket from a prior test can't observe a later test's payload. I also verified listeningServer in wire-frames.ts uses port: 0 and that top-level await at module scope is the established pattern.
Other factors
The PR description gives a detailed and plausible root-cause analysis (LPT sharding + TIME_WAIT exhaustion + TCP_INITIAL_RTO_NO_SYN_RETRANSMISSIONS on Windows loopback). Even if the exact mechanism isn't proven locally, the fix is independently good: fewer ephemeral ports, faster tests, simpler cleanup. The new explanatory comments run longer than the repo's 3-line guideline, but a verifier already considered and dismissed this — the content is the kind of non-obvious platform rationale the guidelines say belongs in a comment. Not worth blocking on.
There was a problem hiding this comment.
LGTM — test-only de-flake that hoists the mock server to module scope; assertions unchanged.
What was reviewed:
- Verified
currentis set before eachnew SQL()and latched per-connection at accept time, so serial tests can't cross-contaminate. - Checked the merged handler in invalid-message-length still matches old
once/onsemantics (atQueryundefined → ignore post-startup data for connect-error cases). - Confirmed
listeningServerreturns a plainnet.Serverand top-level await +afterAllclose is sound; 17/17 tests pass in the PR evidence.
Extended reasoning...
Overview
Refactors two Postgres fault-injection test files to share one long-lived net.Server per file instead of creating/closing a fresh server per test case. Per-test payload is latched into a module-level current variable that the connection handler destructures at accept time. Server is closed in afterAll. No production code touched; all existing assertions and test cases preserved verbatim.
Security risks
None. Test-only change to mock server lifecycle in test/js/sql/. No auth, crypto, or user-facing code paths involved.
Level of scrutiny
Low. This is a mechanical test-infrastructure refactor to reduce Windows CI flakiness caused by per-test listen()/close() churn under TIME_WAIT pressure. The connection-handler logic is byte-identical to before except for reading col/oid/atStartup/atQuery from a latched local instead of a closure parameter. Tests run serially (no test.concurrent), so the module-level current mutation is race-free by construction.
Other factors
- PR evidence shows 17/17 pass on debug/ASAN.
- CodeRabbit's two comment-length nits were addressed in 70cb959 and threads are resolved.
- Checked
wire-frames.ts:listeningServer— simplenet.createServer+listen(0), compatible with top-level await andafterAllclose. - The
afterAllclose relies on each test'ssql.close()/db.close({timeout:0})having dropped client connections soserver.close()fires its callback; verified this holds for every test path (all wrap the SQL instance in try/finally with close).
There was a problem hiding this comment.
LGTM — test-only refactor hoisting the mock server to file scope; assertions are unchanged.
What was reviewed:
- Confirmed
currentis set before eachnew SQL()connect and latched into locals at accept time, so serial tests don't bleed state. - Checked
afterAllcleanup pairs with the module-scopelisteningServer; per-testsql.close()still runs infinally. - Verified the
connectError/queryError/last-test paths in the invalid-message-length file all route through the shared handler with equivalent frame sequences. - CodeRabbit's comment-length nits were addressed in 70cb959 and threads are resolved.
Extended reasoning...
Overview
This PR refactors two Postgres fault-injection test files (test/js/sql/postgres-binary-array-bounds.test.ts and test/js/sql/postgres-invalid-message-length.test.ts) to create one net.Server per file at module scope instead of one per test case. Each test writes its payload into a module-level current variable before connecting; the accept handler destructures current into connection-local variables. Cleanup moves from per-test finally to afterAll. No production code is touched, and no test assertions are changed.
Security risks
None. This is test infrastructure only — the mock servers bind to 127.0.0.1:0, the wire-frame helpers are unchanged, and no runtime/src code paths are modified.
Level of scrutiny
Low. Test-only, mechanical lifecycle refactor with a well-documented rationale (Windows CI ERR_POSTGRES_CONNECTION_REFUSED flakes from per-test listen/close churn under TIME_WAIT pressure). The 17 assertions are byte-identical to before; only where the server is created and closed moved.
Other factors
- Tests run serially (no
test.concurrent), so the sharedcurrentmutable is safe. The handler latches it into locals at accept time, so a latedataevent on a prior connection can't observe the next test's payload. - Each test still closes its SQL client in
finally, soserver.close()inafterAllwon't hang on live connections. - All 17 tests pass in the PR's debug/ASAN evidence block.
- CodeRabbit's two minor comment-length requests were addressed in commit 70cb959 and confirmed resolved.
- No prior
claude[bot]reviews on this PR.
|
Both modified tests pass on all lanes in builds 71813 and 71822. The remaining red is |
main added its own pgParseComplete/pgBindComplete/pgParameterDescription and a two-arg pgReadFrontendMessages in #33962; drop the duplicates this PR had introduced (keeping pgCloseComplete and pgReadCString, which main does not have) and rewrite the mock server to handle StartupMessage inline like the other fault-injection fixtures now do. Switch the real-server test to describeWithContainer (harness), matching the shape #33986 moved sql-prepare-false to so the test skips correctly on agents where docker is unavailable.
Problem
postgres-binary-array-bounds.test.tsandpostgres-invalid-message-length.test.tshave been flaking on Windows CI lanes (2019 x64, 2019 x64-baseline, 11 aarch64) withERR_POSTGRES_CONNECTION_REFUSED. The first 1-2 cases in the file pass, then every remaining case fails, and the failure persists across every retry of the test process for ~30-50s before recovering. Seen in builds 70081 onward (e.g. 71781, 71702, 71632, 71614, 71607, 71604, 71587, 71582, 71563).Cause
Both files create a fresh
net.Serverper test case (7 and 10 respectively) and close it infinally. Since #33622 switched the runner to LPT sharding, on Windows both files land in the same shard a few positions afternode-http-uaf.test.ts, whose fixture issues ~22000 localhost connections and leaves tens of thousands of TIME_WAIT sockets (44k measured locally).Bun disables SYN retransmission for loopback connects on Windows (
SIO_TCP_INITIAL_RTOwithTCP_INITIAL_RTO_NO_SYN_RETRANSMISSIONSinpackages/bun-usockets/src/bsd.c), so a lost SYN under that load becomes an immediate connect failure, and the SQL client surfaces it asERR_POSTGRES_CONNECTION_REFUSEDwithout retry (by design: "nothing is listening" fails fast).I could not reproduce the dropped SYN locally on an idle Windows Server 2019 box even with 44k TIME_WAIT sockets in place, but the CI pattern (Windows-only, first-N-then-all-fail, cross-process, ~30-50s recovery, exclusively in the shard that runs after the 22k-connection fixture) is consistent with this mechanism and nothing else in the process carries state across a fresh
bun testinvocation.Fix
Start one long-lived mock listener per file at module scope and latch the per-test payload into a module-level variable that the connection handler reads at accept time. Each file now needs one listen port and a handful of client ports for the whole run, removing the per-test
listen()+close()churn. Assertions are unchanged; the combined run also drops from ~395ms to ~150ms on Windows release.Verification
On Windows, 7 consecutive runs immediately after
node-http-uaf.test.ts(with thousands of TIME_WAIT sockets in place) all pass.Refs #33622.
[stamp-90s] gate passed · iteration 1 · 2 files touched
passes on PR (with fix)
diff hotspot
gate history · 3 passed · 0 rejected · iteration 1
evidence per changed file