Repository navigation
Conversation
0d4b713 to
2eca35a
Compare
…odule.prepare The per-request call to ensureInstrumentationRegistered() inside RouteModule.prepare was missing an await, so when an adapter dispatches the compiled app-route template's handler without going through BaseServer.handleRequest (where prepareImpl awaits it via runInstrumentationHookIfAvailable), a cold-start request could begin executing the userland route handler before instrumentation.ts#register() resolved. The other two call sites (next-server.ts and web/adapter.ts) already await; this aligns the third. The function memoizes a single promise per process, so the added await is a microtask no-op on every request after the first cold start. Adds a with-async-node-app-route mirror of the existing with-async-node-page case to lock in the invariant for app-route handlers.
2eca35a to
9d3cef9
Compare
|
Thanks, @timneutkens. I looked into this a bit more and decided not to bump the sleep to 5s for now. The existing Instead I added a I used the direct compiled-handler pattern from |
…-module-prepare-await-instrumentation
This reverts commit 0e7d808.
timneutkens
left a comment
There was a problem hiding this comment.
I'll take over getting this PR merged 🙏
|
Thanks for taking this over, @timneutkens 🙏 That makes sense. I went down the direct-handler route mostly because I was struggling to make the local Appreciate you taking it from here. |
Stats skippedCommit: 2d29e2e |
Failing test suitesCommit: 2d29e2e | About building and testing Next.js
Expand output● Instrumentation Hook › with-async-node-app-route › with-async-node-app-route should run the instrumentation hook before the app-route handler
Expand output● Instrumentation Hook › with-async-node-app-route › with-async-node-app-route should run the instrumentation hook before the app-route handler |
|
Unfortunately, our tests currently need some secrets to run deploy tests, and those are not available on external PRs, so CI will always fail here. |
…94306) Closes #93994 Unfortunately deploy tests can't pass on external PRs like #93994 currently due to missing secrets ---- The per-request call to ensureInstrumentationRegistered() inside RouteModule.prepare was missing an await, so when an adapter dispatches the compiled app-route template's handler without going through BaseServer.handleRequest (where prepareImpl awaits it via runInstrumentationHookIfAvailable), a cold-start request could begin executing the userland route handler before instrumentation.ts#register() resolved. The other two call sites (next-server.ts and web/adapter.ts) already await; this aligns the third. This only caused a problem with adapters --------- Co-authored-by: Maurits Misana <[email protected]> Co-authored-by: Tim Neutkens <[email protected]>
…ercel#94306) Closes vercel#93994 Unfortunately deploy tests can't pass on external PRs like vercel#93994 currently due to missing secrets ---- The per-request call to ensureInstrumentationRegistered() inside RouteModule.prepare was missing an await, so when an adapter dispatches the compiled app-route template's handler without going through BaseServer.handleRequest (where prepareImpl awaits it via runInstrumentationHookIfAvailable), a cold-start request could begin executing the userland route handler before instrumentation.ts#register() resolved. The other two call sites (next-server.ts and web/adapter.ts) already await; this aligns the third. This only caused a problem with adapters --------- Co-authored-by: Maurits Misana <[email protected]> Co-authored-by: Tim Neutkens <[email protected]>
The per-request call to ensureInstrumentationRegistered() inside RouteModule.prepare was missing an await, so when an adapter dispatches the compiled app-route template's handler without going through BaseServer.handleRequest (where prepareImpl awaits it via runInstrumentationHookIfAvailable), a cold-start request could begin executing the userland route handler before instrumentation.ts#register() resolved. The other two call sites (next-server.ts and web/adapter.ts) already await; this aligns the third.
The function memoizes a single promise per process, so the added await is a microtask no-op on every request after the first cold start.
Adds a with-async-node-app-route mirror of the existing with-async-node-page case to lock in the invariant for app-route handlers.
What?
RouteModule.preparecallsensureInstrumentationRegistered(...)fromlib/router-utils/instrumentation-globals.externalwithoutawaiting it. This PR adds the missingawaitso that adapters which dispatch the compiledapp-route.tstemplate's handler directly (bypassingBaseServer.handleRequest) still observeinstrumentation.ts#register()resolving before the userland route handler runs.Why?
The
ensureInstrumentationRegisteredhelper is awaited from the other two call sites:packages/next/src/server/next-server.ts:373(viaBaseServer.handleRequest→prepareImpl)packages/next/src/server/web/adapter.ts:114packages/next/src/server/route-modules/route-module.ts:674When an adapter invokes the compiled app-route template's
handlerdirectly (e.g. on a freshly cold-started Vercel serverless function under concurrent burst load) theRouteModule.preparepath is the only chance to register. The unawaited call lets the userland POST handler begin executing beforeregister()finishes its top-levelawait import(...)chain. Module-scoped state thatregister()is supposed to populate (OTel patches, app-bootstrap globals, DB pools) reads asnull/undefinedon the first request; retries on the now-warm lambda succeed.The function memoizes a single promise per process, so the added
awaitis a microtask no-op on every request after the first cold start.How?
One-line
awaitadded at the call site. Addswith-async-node-app-route, mirroring the existingwith-async-node-pageregression case:register()sleeps 1000ms and setsglobalThis.instrumentationFinished; the app-route handler reads it and asserts{ finished: true }.Note on test coverage: under
next startthe request still enters viaBaseServer.handleRequest, which already awaitsprepare(), so the new test passes both before and after this change. It documents the intended invariant for the App Router route-handler path and locks it in against future regressions. A test that fails-without-fix would require exercising an adapter-level dispatch path (e.g. Vercel's@vercel/next) that bypassesBaseServer, which isn't covered by the existing test infrastructure.Repro: https://github.com/mgm1313/next-instrumentation-race-repro
Fixes #93993