Skip to content

fix(instrumentation): await instrumentation in RouteModule.prepare - #94306

Merged
timneutkens merged 9 commits into
canaryfrom
mischnic/await-instrumentation
Jun 1, 2026
Merged

timneutkens merged 9 commits into
canaryfrom
mischnic/await-instrumentation

Conversation

@mischnic

@mischnic mischnic commented Jun 1, 2026 •

Copy link
Copy Markdown
Member

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

mgm1313 and others added 9 commits May 20, 2026 23:29
…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.
@mischnic mischnic changed the title Mischnic/await instrumentation fix(instrumentation): await ensureInstrumentationRegistered in RouteModule.prepare Jun 1, 2026
@mischnic mischnic changed the title fix(instrumentation): await ensureInstrumentationRegistered in RouteModule.prepare fix(instrumentation): await instrumentation in RouteModule.prepare Jun 1, 2026
@github-actions

github-actions Bot commented Jun 1, 2026 •

Copy link
Copy Markdown
Contributor

Tests Passed

Commit: 4b4116a

@mischnic
mischnic requested a review from timneutkens June 1, 2026 14:18
@github-actions

github-actions Bot commented Jun 1, 2026 •

Copy link
Copy Markdown
Contributor

Stats cancelled

Commit: 4b4116a
View workflow run

@timneutkens
timneutkens merged commit 62467ee into canary Jun 1, 2026
295 of 298 checks passed
@timneutkens
timneutkens deleted the mischnic/await-instrumentation branch June 1, 2026 15:19
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants