Unverified Commit 62467ee5 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

fix(instrumentation): await instrumentation in RouteModule.prepare (#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: 's avatarMaurits Misana <maurits@techmaus.nl>
Co-authored-by: 's avatarTim Neutkens <tim@timneutkens.nl>
parent 2ae01a84
......@@ -670,8 +670,11 @@ export abstract class RouteModule<
'../lib/router-utils/instrumentation-globals.external.js'
)
// ensure instrumentation is registered and pass
// onRequestError below
ensureInstrumentationRegistered(absoluteProjectDir, this.distDir)
// onRequestError below. Awaited so any caller of `RouteModule.prepare`
// that bypasses `BaseServer.handleRequest` (where this is also awaited
// via `prepareImpl`) still observes the instrumentation hook completing
// before the userland route handler runs.
await ensureInstrumentationRegistered(absoluteProjectDir, this.distDir)
}
const manifests = this.loadManifests(srcPage, absoluteProjectDir)
const { routesManifest, prerenderManifest, serverFilesManifest } = manifests
......
import { nextTestSetup } from 'e2e-utils'
import { isNextDeploy, nextTestSetup } from 'e2e-utils'
import { check } from 'next-test-utils'
import path from 'path'
const describeCase = (
caseName: string,
callback: (context: ReturnType<typeof nextTestSetup>) => void
callback: (context: ReturnType<typeof nextTestSetup>) => void,
{ skipDeployment = true }: { skipDeployment?: boolean } = {}
) => {
if (skipDeployment && isNextDeploy) return
describe(caseName, () => {
const context = nextTestSetup({
files: path.join(__dirname, caseName),
skipDeployment: true,
skipDeployment,
})
if (context.skipped) return
......@@ -74,6 +77,18 @@ describe('Instrumentation Hook', () => {
})
})
describeCase(
'with-async-node-app-route',
({ next }) => {
it('with-async-node-app-route should run the instrumentation hook before the app-route handler', async () => {
const res = await next.fetch('/api/check')
const body = await res.json()
expect(body).toEqual({ finished: true })
})
},
{ skipDeployment: false }
)
describeCase('general', ({ next, isNextDev }) => {
it('should not overlap with a instrumentation page', async () => {
const page = await next.render('/instrumentation')
......
import { cookies } from 'next/headers'
export async function GET() {
const cookieStore = await cookies()
console.log(cookieStore)
return Response.json({
finished: Boolean((globalThis as any).instrumentationFinished),
})
}
export async function register() {
if (process.env.NEXT_RUNTIME === 'edge') {
console.log('instrumentation hook on the edge')
} else if (process.env.NEXT_RUNTIME === 'nodejs') {
console.log('instrumentation hook on nodejs')
const { register } = await import('./instrumentation.node')
await register()
} else {
await require('this should fail')
}
}
/*global globalThis*/
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
export async function register() {
await sleep(1000)
globalThis.instrumentationFinished = true
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment