next.js
33cb005f - test: fix flaky vary-params-base-dynamic prefetch collection (#101531)

Commit
21 days ago
test: fix flaky vary-params-base-dynamic prefetch collection (#101531) ### What? Fixes two separate root causes of flakes in `test/e2e/app-dir/segment-cache/vary-params-base-dynamic/vary-params-base-dynamic.test.ts`. On canary the test often needed an in-job retry (`test prod`, `test turbopack production`, `test cache components prod`). 1. **Uncontrolled prefetches (test fixture).** `collectSegmentPrefetchResponses` now starts from a new `/prefetch-hub` page that only renders the `LinkAccordion`s. The unused accordions are removed from `/`. The back/forward scenarios still run on `/` with its visible nav links. 2. **`read ECONNRESET` in `router-act` (shared test harness).** Requests that `act` forwards through `page.request` now use a fresh connection (`connection: close`) instead of reusing idle keep-alive sockets. ### Why? #### 1. `Timed out waiting for a request to be initiated.` `/` renders always-visible `<Link>`s to the same routes the accordions target. Those links prefetch on page load, outside the `act` scope. When those uncontrolled segment prefetches are already in flight at toggle time, the router dedupes the accordion's prefetch against them, so no new request starts inside `act`. This happens on a slow runner, or while the server regenerates after the previous test's revalidation. Adding a 300 ms delay before the toggle (diagnostic only) reproduced the error 6/6 on the old start page. It passed 6/6 with `/prefetch-hub`. #### 2. `apiRequestContext.fetch: read ECONNRESET` `router-act` fulfils intercepted requests by re-sending them with `page.request.fetch`. That request context sends through Playwright's process-wide keep-alive agent. The failures were always in the first `act` of a test that directly follows a back/forward test. So the pooled sockets had been idle for the whole previous test, which takes about as long as the server's keep-alive timeout. - **Measured reuse gaps:** server-side connection logging showed `act` requests reusing pooled sockets after 5.2–6.0 s idle. That is right at Node's idle close: 5 s on Node 20 (used in CI); 5 s plus a 1 s `keepAliveTimeoutBuffer` on Node 24. - **Why the server resets the connection:** when the timeout comes due while the server's event loop is busy (e.g. rendering the page the next test just loaded), the next loop iteration runs the timers phase before it polls the socket. The keep-alive timeout therefore `destroy()`s the "idle" socket even though a request on it has already arrived. Destroying a socket with unread data makes the kernel send an RST, and the client sees `read ECONNRESET`. - **Reproduction:** a standalone script reproduces this deterministically with a plain Node server and Playwright's `APIRequestContext`. The client reuses a pooled socket while the server is busy for 200 ms across its keep-alive expiry. Result: 5/5 `apiRequestContext.fetch: read ECONNRESET` with the default keep-alive, 0/5 with `connection: close`. Browsers don't hit this because they transparently retry a request that fails on a reused connection. Playwright's request context doesn't retry. This isn't a Next.js server bug: it's how Node's HTTP server keep-alive timeout behaves. Node's newer `keepAliveTimeoutBuffer` only narrows the window. ### How? - Add `app/prefetch-hub/page.tsx`, which contains only the two `LinkAccordion`s. `collectSegmentPrefetchResponses` loads it, and its unused `startPath` parameter is removed. Assertions are unchanged. - `router-act` forwards intercepted requests with `headers: { ...request.headers(), connection: 'close' }`. Without `headers`, Playwright already uses `request.headers()`, so the only change is that the socket isn't returned to the pool. A forwarded request can then never land on a socket the server is about to close. Opening a localhost connection per request is cheap. ### Verification The suite was run with `node run-tests.js --type production --retries 0` and the CI job env: | Mode | Result | |---|---| | Webpack production | 3/3 pass | | Turbopack production | 3/3 pass | | Cache Components production | 2/2 pass | - Server-side connection logging with the fix: all 120 `act`-forwarded requests used fresh connections (0 reuses). The remaining keep-alive reuses are Chrome's own requests. - Other `router-act` suites pass (turbopack production): `segment-cache/basic`, `revalidation`, `headers-keyed-caches`, `staleness`, `deployment-skew`, `actions-discarded-navigation-revert`. - `pnpm typescript` passes. <!-- NEXT_JS_LLM --> <!-- fleet 3f4853bf-3a4d-4517-b4b5-10029d6f36dd --> --------- Co-authored-by: vercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com> Co-authored-by: Tobias Koppers <1365881+sokra@users.noreply.github.com>
Author
Parents
Loading