Repository navigation
fix(instrumentation): await instrumentation in RouteModule.prepare - #94306
Merged
Merged
Conversation
…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.
…-module-prepare-await-instrumentation
This reverts commit 0e7d808.
Contributor
Tests PassedCommit: 4b4116a |
Contributor
Stats cancelledCommit: 4b4116a |
timneutkens
approved these changes
Jun 1, 2026
mischnic
added a commit
that referenced
this pull request
Jul 6, 2026
#94306 fixes this for pages, but middleware was still broken when deployed via adapters Coming from getsentry/sentry-javascript#21713 Now the instrumentation test suite takes 11m to run in deployment mode though....
m-kawafuji
pushed a commit
to m-kawafuji/next.js
that referenced
this pull request
Aug 8, 2026
…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]>
m-kawafuji
pushed a commit
to m-kawafuji/next.js
that referenced
this pull request
Aug 8, 2026
…l#95357) vercel#94306 fixes this for pages, but middleware was still broken when deployed via adapters Coming from getsentry/sentry-javascript#21713 Now the instrumentation test suite takes 11m to run in deployment mode though....
nextly-agent Bot
pushed a commit
to nextlyhq/nextly
that referenced
this pull request
Oct 16, 2026
…d keep its log best-effort A route can run before instrumentation.register() has finished, as Next.js on Vercel allowed before vercel/next.js#94306. The health route now waits for a start already under way, through getCachedNextly(), rather than answering 503 for a server seconds from serving; with none in progress it answers as before. The once-per-process warning is best-effort, so a logger that cannot write leaves the answer a 503 rather than a 500. The existing-app generator looks for the hook under Next's default page extensions, ts, tsx, js and jsx, which are the names Next loads it from.
mobeenabdullah
added a commit
to nextlyhq/nextly
that referenced
this pull request
Oct 16, 2026
… health check answers (#2146) * fix(nextly): start Nextly when the server starts, so a fresh server's health check answers The health route is re-exported by a project with no config, so it can only report on a Nextly something else started. On the blank scaffold nothing did until the admin's API served a request, so a newly started server answered /api/health with 503 until other traffic arrived. The blank and blog templates now ship an instrumentation.ts whose register() starts Nextly with the project's config; Next.js finishes it before the instance serves a request and skips it during next build. Adding Nextly to an existing app writes the same hook, or, when the app already has one, leaves it alone and prints the lines to add. Before Nextly has started, the health route answers 503 with the generic error without probing, and logs once how to start it. * fix(create-nextly-app): give a hook Next.js will run, and say what to add when it cannot On Next.js 14 the hook runs only with experimental.instrumentationHook, so the generator reports that line for next.config, which it leaves as it is. When the app sets pageExtensions, Next.js reads the hook only under a name with one of them, so none is written and the whole hook is reported to add under that name. The lines for an app's own hook are now a whole async register(), which compiles when pasted into a hook that was synchronous. The health route answers a not-started server's 503 without throwing, so a monitor polling it logs once per process rather than once a poll. * fix(nextly): wait for a start in progress before answering health, and keep its log best-effort A route can run before instrumentation.register() has finished, as Next.js on Vercel allowed before vercel/next.js#94306. The health route now waits for a start already under way, through getCachedNextly(), rather than answering 503 for a server seconds from serving; with none in progress it answers as before. The once-per-process warning is best-effort, so a logger that cannot write leaves the answer a 503 rather than a 500. The existing-app generator looks for the hook under Next's default page extensions, ts, tsx, js and jsx, which are the names Next loads it from. --------- Co-authored-by: Mobeen Abdullah <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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