Unverified Commit 81d0e851 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Fix instant validation blocking navigations (#95939)

### What?

- Run development instant validation only after the navigation response
has finished.
- Let a newer request supersede stale validation work and yield between
validation render attempts.
- Forward the cancellation signal into React validation prerenders so
asynchronous validation work can stop promptly.
- Add focused scheduler/response tests and assert that development
validation starts after the response finishes.

### Why?

Instant validation was detached from the response promise, but its
additional React renders still ran on the same Node.js event loop. A
subsequent navigation could therefore wait hundreds of milliseconds for
stale diagnostic work from the previous request.

Instant validation still runs when the server is idle. When another
navigation arrives, foreground request work now takes priority and the
obsolete validation is discarded.

### How?

Each Cache Components development request receives a validation
generation signal. Starting a newer request aborts the previous
generation. Validation waits for the Node response `finish` event,
yields through the event-loop poll phase between attempts, and combines
the generation signal with React's existing prerender abort signals.

Tests:

- `pnpm --filter=next build`
- `pnpm --filter=next types`
- `pnpm exec jest --runTestsByPath
packages/next/src/server/app-render/dev-validation-scheduler.test.ts
packages/next/src/server/app-render/wait-for-response.test.ts`
- `NEXT_SKIP_ISOLATE=1 NEXT_TEST_PREFER_OFFLINE=1 pnpm test-dev-turbo
test/e2e/app-dir/instant-validation/suspense-boundaries.test.ts -t
"valid - static prefetch - suspense around runtime and dynamic"`

<!-- NEXT_JS_LLM_PR -->
parent 390eff3b
......@@ -1420,5 +1420,6 @@
"1419": "TypeScript CLI interrupted by %s",
"1420": "shouldTrackSyncInterrupt allowed a sync IO interrupt in an unexpected stage: %s",
"1421": "The lazy module is still loading. It must be awaited with `waitUntilLoaded()` before it can be accessed.",
"1422": "MIN_PREFETCHABLE_STALE must not exceed MIN_SHELL_STALE."
"1422": "MIN_PREFETCHABLE_STALE must not exceed MIN_SHELL_STALE.",
"1423": "DevValidationScheduler requires at least one active validation"
}
......@@ -178,6 +178,11 @@ import {
wrapClientComponentLoader,
} from '../client-component-renderer-logger'
import { isNodeNextRequest, isNodeNextResponse } from '../base-http/helpers'
import { waitForResponseToFinish } from './wait-for-response'
import {
beginDevValidation,
yieldToForegroundRequest,
} from './dev-validation-scheduler'
import { signalFromNodeResponse } from '../web/spec-extension/adapters/next-request'
import {
parseRelativeUrl,
......@@ -1305,6 +1310,7 @@ async function generateDynamicFlightRenderResultWithStagesInDev(
// instant configs exist, since we render all the layouts necessary to perform
// the validation in those cases.
const shouldValidate =
!ctx.isPrefetch &&
!isBypassingCachesInDev(initialRequestStore, workStore) &&
(initialRequestStore.isHmrRefresh === true ||
(await anySegmentNeedsInstantValidationInDev(loaderTree)))
......@@ -4410,8 +4416,8 @@ function forwardInvalidDynamicUsageError(
}
/**
* Runs Cache Components validation in the background once the streamed render
* has finished (the response has already streamed).
* Runs Cache Components validation in the background once the response has
* finished.
*/
function runDevValidationInBackground(
prefetchMode: PrefetchingMode,
......@@ -4427,18 +4433,35 @@ function runDevValidationInBackground(
createRequestStore: () => RequestStore,
getPayload: (requestStore: RequestStore) => Promise<RSCPayload>,
onError: (error: unknown) => void,
requestAbortSignal: AbortSignal | undefined
validationAbortSignal: AbortSignal,
onSettled: () => void
): void {
void consoleAsyncStorage
.run({ dim: true }, async () => {
const result = await resultPromise
// If the request was aborted while its render was in flight, skip
// validation entirely: the render's result is being discarded, so
// re-rendering and analyzing it would only waste server work. In-flight
// `'use cache'` fills are left running (we don't await `cacheReady`
// below).
if (requestAbortSignal?.aborted) {
// If the request was aborted, or a newer render superseded its
// validation, re-rendering and analyzing it would only waste server
// work. In-flight `'use cache'` fills are left running (we don't await
// `cacheReady` below).
if (validationAbortSignal?.aborted) {
logValidationAborted(ctx)
return
}
// Validation is detached diagnostic work, but the renders and error
// formatting it performs can still monopolize the event loop. Wait until
// the response has finished so that work cannot delay Flight delivery or
// the client navigation.
if (
isNodeNextResponse(ctx.res) &&
!(await waitForResponseToFinish(ctx.res.originalResponse))
) {
logValidationAborted(ctx)
return
}
if (!(await yieldToForegroundRequest(validationAbortSignal))) {
logValidationAborted(ctx)
return
}
......@@ -4455,6 +4478,11 @@ function runDevValidationInBackground(
await cacheSignal.cacheReady()
}
if (validationAbortSignal?.aborted) {
logValidationAborted(ctx)
return
}
// If the initial render recorded invalid dynamic usage errors
// (e.g. from caught errors inside "use cache"), we skip validation altogether.
if (
......@@ -4477,7 +4505,7 @@ function runDevValidationInBackground(
createRequestStore,
getPayload,
onError,
requestAbortSignal
validationAbortSignal
)
// If we need to do multiple renders, do them in parallel.
......@@ -4499,10 +4527,9 @@ function runDevValidationInBackground(
return
}
// The request may have been aborted while we prepared the validation
// inputs above (which can itself render). Skip validation before it
// begins rather than run it for a discarded request.
if (requestAbortSignal?.aborted) {
// A newer render may have superseded this work while we prepared the
// validation inputs above (which can itself render).
if (validationAbortSignal?.aborted) {
logValidationAborted(ctx)
return
}
......@@ -4514,16 +4541,15 @@ function runDevValidationInBackground(
ctx,
fallbackRouteParams,
devRenderDidError,
requestAbortSignal
validationAbortSignal
)
})
// The catch keeps a failed render, or anything thrown inside validation,
// from surfacing as an unhandled rejection.
.catch((err) => {
// If this request was aborted, its render and detached validation are
// being torn down and their result discarded. We don't want to log these
// errors (including the abort signal reason itself).
if (requestAbortSignal?.aborted) {
// Superseded validation is intentionally torn down. Don't log errors
// caused by its abort signal.
if (validationAbortSignal?.aborted) {
return
}
console.error(
......@@ -4532,6 +4558,7 @@ function runDevValidationInBackground(
})
)
})
.finally(onSettled)
}
/**
......@@ -4590,7 +4617,7 @@ async function prepareValidationInputs(
createRequestStore: () => RequestStore,
getPayload: (requestStore: RequestStore) => Promise<RSCPayload>,
onError: (error: unknown) => void,
requestAbortSignal: AbortSignal | undefined
validationAbortSignal: AbortSignal | undefined
): Promise<PrepareValidationInputsResult> {
// Check if we can re-use the main render for validation.
let inputsFromNavigation: DevValidationInputs | null
......@@ -4620,7 +4647,7 @@ async function prepareValidationInputs(
getPayload,
onError,
inputsFromNavigation,
requestAbortSignal
validationAbortSignal
)
} else {
return prepareValidationInputsInLegacyPrefetching(
......@@ -4630,7 +4657,7 @@ async function prepareValidationInputs(
getPayload,
onError,
inputsFromNavigation,
requestAbortSignal
validationAbortSignal
)
}
}
......@@ -4644,7 +4671,7 @@ async function prepareValidationInputsInPartialPrefetching(
getPayload: (requestStore: RequestStore) => Promise<RSCPayload>,
onError: (error: unknown) => void,
inputsFromNavigation: DevValidationInputs | null,
requestAbortSignal: AbortSignal | undefined
validationAbortSignal: AbortSignal | undefined
): Promise<PrepareValidationInputsResult> {
const loaderTree = ctx.componentMod.routeModule.userland.loaderTree
const needsInstantValidation =
......@@ -4669,7 +4696,7 @@ async function prepareValidationInputsInPartialPrefetching(
prerenderResumeDataCache,
prefetchMode,
shouldRenderWithAppShell,
requestAbortSignal
validationAbortSignal
)
if (forwardErrorsFromWarmRender(inputs, ctx)) {
return VALIDATION_BAILOUT
......@@ -4684,7 +4711,7 @@ async function prepareValidationInputsInPartialPrefetching(
getPayload,
onError,
prerenderResumeDataCache,
requestAbortSignal
validationAbortSignal
)
if (forwardErrorsFromWarmRender(inputs, ctx)) {
return VALIDATION_BAILOUT
......@@ -4738,7 +4765,7 @@ async function prepareValidationInputsInLegacyPrefetching(
getPayload: (requestStore: RequestStore) => Promise<RSCPayload>,
onError: (error: unknown) => void,
inputsFromNavigation: DevValidationInputs | null,
requestAbortSignal: AbortSignal | undefined
validationAbortSignal: AbortSignal | undefined
): Promise<PrepareValidationInputsResult> {
const loaderTree = ctx.componentMod.routeModule.userland.loaderTree
const needsInstantValidation =
......@@ -4764,7 +4791,7 @@ async function prepareValidationInputsInLegacyPrefetching(
prerenderResumeDataCache,
prefetchMode,
shouldRenderWithAppShell,
requestAbortSignal
validationAbortSignal
)
if (forwardErrorsFromWarmRender(inputs, ctx)) {
return VALIDATION_BAILOUT
......@@ -4779,7 +4806,7 @@ async function prepareValidationInputsInLegacyPrefetching(
getPayload,
onError,
prerenderResumeDataCache,
requestAbortSignal
validationAbortSignal
)
if (forwardErrorsFromWarmRender(inputs, ctx)) {
return VALIDATION_BAILOUT
......@@ -4960,9 +4987,10 @@ interface StagedDevRenderOptions {
requestStore: RequestStore
onError: (error: unknown) => void
navigationKind: DevNavigationKind
// When this aborts, the staged dev render is torn down (its in-flight `'use
// cache'` fills keep running) and the detached validation is skipped.
// `undefined` runs both to completion.
// This is defined only for foreground HMR requests. When it aborts, the
// staged render is torn down while its in-flight `'use cache'` fills keep
// running. Detached validation uses its per-document generation signal;
// `waitForResponseToFinish` handles response closure before it begins.
requestAbortSignal: AbortSignal | undefined
}
......@@ -5240,7 +5268,7 @@ async function renderWithWarmCachesForValidationInDev(
prerenderResumeDataCache: ReturnType<typeof createPrerenderResumeDataCache>,
prefetchMode: PrefetchingMode,
shouldRenderWithAppShell: boolean,
requestAbortSignal: AbortSignal | undefined
validationAbortSignal: AbortSignal | undefined
): Promise<DevValidationInputs> {
const { ComponentMod, setReactDebugChannel } = ctx.renderOpts
const { clientModules } = getClientReferenceManifest()
......@@ -5290,14 +5318,14 @@ async function renderWithWarmCachesForValidationInDev(
startTime,
filterStackFrame,
debugChannel: debugChannel?.serverSide,
signal: requestAbortSignal,
signal: validationAbortSignal,
}
) as Readable
return accumulateStreamChunks(
sourceStream,
stageController,
requestAbortSignal ?? null
validationAbortSignal ?? null
)
},
() => stageController.advanceStage(RenderStage.Static),
......@@ -5331,7 +5359,7 @@ async function prerenderWithWarmCachesForStaticValidationInDev(
getPayload: (requestStore: RequestStore) => Promise<RSCPayload>,
onError: (error: unknown) => void,
prerenderResumeDataCache: ReturnType<typeof createPrerenderResumeDataCache>,
requestAbortSignal: AbortSignal | undefined
validationAbortSignal: AbortSignal | undefined
): Promise<DevValidationInputs> {
// This function is currently only used in partialPrefetching.
const prefetchMode = PrefetchingMode.Partial
......@@ -5345,8 +5373,8 @@ async function prerenderWithWarmCachesForStaticValidationInDev(
const finalReactController = new AbortController()
const finalDataController = new AbortController()
if (requestAbortSignal) {
abortWhenSignalAborts(requestAbortSignal, finalReactController)
if (validationAbortSignal) {
abortWhenSignalAborts(validationAbortSignal, finalReactController)
}
const stageController = new StagedRenderingController({
......@@ -5510,78 +5538,92 @@ async function stagedRenderWithCachesInDev({
stream: Readable
debugChannel: NodeDebugChannelPair | undefined
}> {
const { setReactDebugChannel } = ctx.renderOpts
const validationGeneration = shouldValidate
? beginDevValidation(ctx.htmlRequestId)
: undefined
let validationWasScheduled = false
const {
cacheSignal,
prerenderResumeDataCache,
stageController,
environmentName,
} = setUpStagedDevRender(prefetchMode, navigationKind, requestStore)
try {
const { setReactDebugChannel } = ctx.renderOpts
let validationDebugChannel: AnyStream | undefined
const debugChannel = setReactDebugChannel && createNodeDebugChannel()
if (shouldValidate && debugChannel) {
const debugChannelReplay = new ReplayableNodeStream(
debugChannel.clientSide.readable
)
debugChannel.clientSide.readable = debugChannelReplay.createReplayStream()
validationDebugChannel = debugChannelReplay.createReplayStream()
}
const {
cacheSignal,
prerenderResumeDataCache,
stageController,
environmentName,
} = setUpStagedDevRender(prefetchMode, navigationKind, requestStore)
// The stage controller starts in the `Before` stage, where sync IO doesn't
// abort, so it's fine if it happens while creating the payload.
const rscPayload = await getPayload(requestStore)
let validationDebugChannel: AnyStream | undefined
const debugChannel = setReactDebugChannel && createNodeDebugChannel()
if (validationGeneration !== undefined && debugChannel) {
const debugChannelReplay = new ReplayableNodeStream(
debugChannel.clientSide.readable
)
debugChannel.clientSide.readable = debugChannelReplay.createReplayStream()
validationDebugChannel = debugChannelReplay.createReplayStream()
}
const { stream, resultPromise } = await streamStagedRenderInDev({
prefetchMode,
ctx,
requestStore,
rscPayload,
stageController,
cacheSignal,
environmentName,
onError,
debugChannel,
navigationKind,
requestAbortSignal,
})
// The stage controller starts in the `Before` stage, where sync IO doesn't
// abort, so it's fine if it happens while creating the payload.
const rscPayload = await getPayload(requestStore)
if (shouldValidate) {
runDevValidationInBackground(
const { stream, resultPromise } = await streamStagedRenderInDev({
prefetchMode,
navigationKind,
resultPromise,
ctx,
requestStore,
validationDebugChannel,
rscPayload,
stageController,
cacheSignal,
ctx,
fallbackRouteParams,
prerenderResumeDataCache,
getDevRenderDidError,
createRequestStore,
getPayload,
environmentName,
onError,
requestAbortSignal
)
} else {
logValidationSkipped(ctx)
// We don't validate, but the render may still record an invalid dynamic
// usage error (e.g. a request API used inside `'use cache'`). `result`
// resolves once the full stream (incl. the dynamic stage) has finished, so
// any such error is final by then; forward it to the dev overlay.
resultPromise.then(
() =>
forwardInvalidDynamicUsageError(
ctx.workStore.invalidDynamicUsageError,
ctx
),
() => {}
)
}
debugChannel,
navigationKind,
requestAbortSignal,
})
return { stream, debugChannel }
if (validationGeneration !== undefined) {
runDevValidationInBackground(
prefetchMode,
navigationKind,
resultPromise,
requestStore,
validationDebugChannel,
cacheSignal,
ctx,
fallbackRouteParams,
prerenderResumeDataCache,
getDevRenderDidError,
createRequestStore,
getPayload,
onError,
validationGeneration.signal,
validationGeneration.finish
)
validationWasScheduled = true
} else {
logValidationSkipped(ctx)
// We don't validate, but the render may still record an invalid dynamic
// usage error (e.g. a request API used inside `'use cache'`). `result`
// resolves once the full stream (incl. the dynamic stage) has finished, so
// any such error is final by then; forward it to the dev overlay.
resultPromise.then(
() =>
forwardInvalidDynamicUsageError(
ctx.workStore.invalidDynamicUsageError,
ctx
),
() => {}
)
}
return { stream, debugChannel }
} catch (err) {
if (!validationWasScheduled) {
validationGeneration?.finish()
}
throw err
}
}
interface AccumulatedStreamChunks {
......@@ -5946,19 +5988,60 @@ async function runValidationInDev(
const ctx: AppRenderContext = args[3]
const requestId = ctx.requestId
const url = ctx.url.href
const responseFinished =
!isNodeNextResponse(ctx.res) || ctx.res.originalResponse.writableFinished
console.log(
'<VALIDATION_MESSAGE>' +
JSON.stringify({ type: 'validation_start', requestId, url }) +
JSON.stringify({
type: 'validation_start',
requestId,
url,
responseFinished,
}) +
'</VALIDATION_MESSAGE>'
)
try {
const validationAbortSignal: AbortSignal | undefined = args[6]
// Keep validation in flight for scheduler E2E tests without relying on
// timing-sensitive user code. Aborts end the delay immediately.
const validationDelay = Number(
process.env.NEXT_TEST_DEV_VALIDATION_DELAY_MS
)
if (
validationAbortSignal !== undefined &&
Number.isFinite(validationDelay) &&
validationDelay > 0 &&
!validationAbortSignal.aborted
) {
await new Promise<void>((resolve) => {
let timeout: NodeJS.Timeout
const finishDelay = () => {
clearTimeout(timeout)
validationAbortSignal.removeEventListener('abort', finishDelay)
resolve()
}
timeout = setTimeout(finishDelay, validationDelay)
validationAbortSignal.addEventListener('abort', finishDelay, {
once: true,
})
})
}
if (validationAbortSignal?.aborted) {
return
}
return await runValidationInDevImpl(...args)
} finally {
console.log(
'<VALIDATION_MESSAGE>' +
JSON.stringify({ type: 'validation_end', requestId, url }) +
'</VALIDATION_MESSAGE>'
)
const validationAbortSignal: AbortSignal | undefined = args[6]
if (validationAbortSignal?.aborted) {
logValidationAborted(ctx)
} else {
console.log(
'<VALIDATION_MESSAGE>' +
JSON.stringify({ type: 'validation_end', requestId, url }) +
'</VALIDATION_MESSAGE>'
)
}
}
} else {
return await runValidationInDevImpl(...args)
......@@ -5978,7 +6061,7 @@ async function runValidationInDevImpl(
ctx: AppRenderContext,
fallbackRouteParams: OpaqueFallbackRouteParams | null,
devRenderDidError: boolean,
requestAbortSignal: AbortSignal | undefined
validationAbortSignal: AbortSignal | undefined
): Promise<void> {
const { componentMod: ComponentMod, getDynamicParamFromSegment } = ctx
const loaderTree = ComponentMod.routeModule.userland.loaderTree
......@@ -6014,10 +6097,18 @@ async function runValidationInDevImpl(
fallbackRouteParams,
ctx,
validationSamples,
validationSampleTracking
validationSampleTracking,
validationAbortSignal
)
}
// React renders used by validation can occupy an entire event-loop turn.
// Yield between them so a newer navigation can enter app rendering,
// supersede this validation, and avoid waiting for all remaining attempts.
if (!(await yieldToForegroundRequest(validationAbortSignal))) {
return
}
// instantInputs and staticInputs may be the same,
// so we have to make sure we only consume the debug channel once.
let cachedDebugChunks = new WeakMap<AnyStream, Uint8Array[]>()
......@@ -6039,7 +6130,7 @@ async function runValidationInDevImpl(
//================================
{
// The request may have been aborted during the client module warmup above.
if (requestAbortSignal?.aborted) {
if (validationAbortSignal?.aborted) {
return
}
......@@ -6056,15 +6147,18 @@ async function runValidationInDevImpl(
rootParams,
fallbackRouteParams,
debugChunks,
hmrRefreshHash
hmrRefreshHash,
validationAbortSignal
)
// The request was aborted while this validation render ran, so its result
// is being discarded. Don't surface its validation errors: they're for a
// render that won't be shown.
if (requestAbortSignal?.aborted) {
// A newer render superseded this validation while its render ran, so its
// result is stale. Don't surface errors for a page the user left.
if (validationAbortSignal?.aborted) {
return
}
if (result.length > 0) {
if (!(await yieldToForegroundRequest(validationAbortSignal))) {
return
}
return logMessagesAndSendErrorsToBrowser(result, ctx)
}
}
......@@ -6073,6 +6167,10 @@ async function runValidationInDevImpl(
// Instant validation
//================================
if (needsInstantValidation && instantInputs) {
if (!(await yieldToForegroundRequest(validationAbortSignal))) {
return
}
const inputs = instantInputs
const debugChunks = inputs.debugChannelClient
......@@ -6090,15 +6188,19 @@ async function runValidationInDevImpl(
ctx,
hmrRefreshHash,
validationSamples,
devRenderDidError
devRenderDidError,
validationAbortSignal
)
// The request was aborted while this render ran; don't surface its stale
// validation errors: they're for a render that won't be shown.
if (requestAbortSignal?.aborted) {
// A newer render superseded this work. Don't surface stale validation
// errors for a page the user left.
if (validationAbortSignal?.aborted) {
return
}
if (result.length > 0) {
if (!(await yieldToForegroundRequest(validationAbortSignal))) {
return
}
return logMessagesAndSendErrorsToBrowser(result, ctx)
}
}
......@@ -6118,7 +6220,8 @@ async function validateStaticShell(
rootParams: Params,
fallbackRouteParams: OpaqueFallbackRouteParams | null,
debugChunks: Uint8Array[] | null,
hmrRefreshHash: string | undefined
hmrRefreshHash: string | undefined,
validationAbortSignal: AbortSignal | undefined
): Promise<unknown[]> {
const debug =
process.env.NEXT_PRIVATE_DEBUG_VALIDATION === '1' ? console.log : undefined
......@@ -6146,7 +6249,8 @@ async function validateStaticShell(
allowEmptyStaticShell,
ctx,
hmrRefreshHash,
trackDynamicHoleInRuntimeShell
trackDynamicHoleInRuntimeShell,
validationAbortSignal
)
if (runtimeResult.length > 0) {
......@@ -6156,6 +6260,10 @@ async function validateStaticShell(
return runtimeResult
}
if (!(await yieldToForegroundRequest(validationAbortSignal))) {
return []
}
const staticResult = await validateStagedShell(
staticChunks,
dynamicChunks,
......@@ -6166,7 +6274,8 @@ async function validateStaticShell(
allowEmptyStaticShell,
ctx,
hmrRefreshHash,
trackDynamicHoleInStaticShell
trackDynamicHoleInStaticShell,
validationAbortSignal
)
if (staticResult.length > 0) {
......@@ -6187,7 +6296,8 @@ async function warmupClientModulesForStagedValidation(
fallbackRouteParams: OpaqueFallbackRouteParams | null,
ctx: AppRenderContext,
validationSamples: ValidationStoreClient['validationSamples'],
validationSampleTracking: ValidationStoreClient['validationSampleTracking']
validationSampleTracking: ValidationStoreClient['validationSampleTracking'],
validationAbortSignal?: AbortSignal
) {
const { implicitTags, nonce, workStore } = ctx
......@@ -6195,6 +6305,13 @@ async function warmupClientModulesForStagedValidation(
const initialClientPrerenderController = new AbortController()
const initialClientReactController = new AbortController()
const initialClientRenderController = new AbortController()
const initialClientReactSignal =
validationAbortSignal === undefined
? initialClientReactController.signal
: AbortSignal.any([
initialClientReactController.signal,
validationAbortSignal,
])
const preinitScripts = () => {}
const { ServerInsertedHTMLProvider } = createServerInsertedHTML()
......@@ -6259,7 +6376,7 @@ async function warmupClientModulesForStagedValidation(
const serverStream = createNodeStreamWithLateRelease(
partialServerChunks,
allServerChunks,
initialClientReactController.signal
initialClientReactSignal
)
const pendingInitialClientResult = workUnitAsyncStorage.run(
......@@ -6276,7 +6393,7 @@ async function warmupClientModulesForStagedValidation(
images={ctx.renderOpts.images}
/>,
{
signal: initialClientReactController.signal,
signal: initialClientReactSignal,
onError: (err: unknown) => {
const digest = getDigestForWellKnownError(err)
......@@ -6290,7 +6407,7 @@ async function warmupClientModulesForStagedValidation(
return undefined
}
if (initialClientReactController.signal.aborted) {
if (initialClientReactSignal.aborted) {
// These are expected errors that might error the prerender. we ignore them.
} else if (
process.env.NEXT_DEBUG_BUILD ||
......@@ -6313,7 +6430,7 @@ async function warmupClientModulesForStagedValidation(
// The listener to abort our own render controller must be added after React
// has added its listener, to ensure that pending I/O is not
// aborted/rejected too early.
initialClientReactController.signal.addEventListener(
initialClientReactSignal.addEventListener(
'abort',
() => {
initialClientRenderController.abort()
......@@ -6322,10 +6439,7 @@ async function warmupClientModulesForStagedValidation(
)
pendingInitialClientResult.catch((err: unknown) => {
if (
initialClientReactController.signal.aborted ||
isPrerenderInterruptedError(err)
) {
if (initialClientReactSignal.aborted || isPrerenderInterruptedError(err)) {
// These are expected errors that might error the prerender. we ignore them.
} else if (
process.env.NEXT_DEBUG_BUILD ||
......@@ -6364,7 +6478,8 @@ async function validateStagedShell(
hmrRefreshHash: string | undefined,
trackDynamicHole:
| typeof trackDynamicHoleInStaticShell
| typeof trackDynamicHoleInRuntimeShell
| typeof trackDynamicHoleInRuntimeShell,
validationAbortSignal: AbortSignal | undefined
): Promise<Array<unknown>> {
const { implicitTags, nonce, workStore } = ctx
......@@ -6373,6 +6488,10 @@ async function validateStagedShell(
)
const clientReactController = new AbortController()
const clientRenderController = new AbortController()
const clientReactSignal =
validationAbortSignal === undefined
? clientReactController.signal
: AbortSignal.any([clientReactController.signal, validationAbortSignal])
const preinitScripts = () => {}
const { ServerInsertedHTMLProvider } = createServerInsertedHTML()
......@@ -6404,14 +6523,14 @@ async function validateStagedShell(
const serverStream = createNodeStreamWithLateRelease(
stageChunks,
allServerChunks,
clientReactController.signal
clientReactSignal
)
const debugChannelClient = debugChunks
? createNodeStreamWithLateRelease(
debugChunks,
debugChunks,
clientReactController.signal
clientReactSignal
)
: undefined
......@@ -6432,11 +6551,11 @@ async function validateStagedShell(
images={ctx.renderOpts.images}
/>,
{
signal: clientReactController.signal,
signal: clientReactSignal,
onError: (err: unknown, errorInfo: ErrorInfo) => {
if (
isPrerenderInterruptedError(err) ||
clientReactController.signal.aborted
clientReactSignal.aborted
) {
const componentStack = errorInfo.componentStack
if (typeof componentStack === 'string') {
......@@ -6467,7 +6586,7 @@ async function validateStagedShell(
// The listener to abort our own render controller must be added after
// React has added its listener, to ensure that pending I/O is not
// aborted/rejected too early.
clientReactController.signal.addEventListener(
clientReactSignal.addEventListener(
'abort',
() => {
clientRenderController.abort()
......@@ -6531,7 +6650,8 @@ async function validateInstantConfigs(
ctx: AppRenderContext,
hmrRefreshHash: string | undefined,
validationSamples: ValidationStoreClient['validationSamples'] | null,
devRenderDidError: boolean
devRenderDidError: boolean,
validationAbortSignal?: AbortSignal
): Promise<Array<unknown>> {
const debug =
process.env.NEXT_PRIVATE_DEBUG_VALIDATION === '1' ? console.log : undefined
......@@ -6601,7 +6721,15 @@ async function validateInstantConfigs(
groupDepthForValidation: number,
previousBoundaryState: null | ValidationBoundaryTracking
): Promise<null | NavigationValidationResult> {
if (validationAbortSignal?.aborted) {
return null
}
const extraChunksController = new AbortController()
const extraChunksSignal =
validationAbortSignal === undefined
? extraChunksController.signal
: AbortSignal.any([extraChunksController.signal, validationAbortSignal])
const boundaryState = createValidationBoundaryTracking()
let useRuntimeStageForPartialSegments = false
......@@ -6622,7 +6750,7 @@ async function validateInstantConfigs(
ctx.query,
depth,
groupDepthForValidation,
extraChunksController.signal,
extraChunksSignal,
boundaryState,
clientReferenceManifest,
stageEndTimes,
......@@ -6635,6 +6763,10 @@ async function validateInstantConfigs(
const reactController = new AbortController()
const renderController = new AbortController()
const reactSignal =
validationAbortSignal === undefined
? reactController.signal
: AbortSignal.any([reactController.signal, validationAbortSignal])
const preinitScripts = () => {}
const { ServerInsertedHTMLProvider } = createServerInsertedHTML()
......@@ -6644,7 +6776,7 @@ async function validateInstantConfigs(
renderFlightStream,
payloadResult.payload,
extraChunksController,
reactController.signal,
reactSignal,
clientReferenceManifest,
startTime,
isDebugChannelEnabled,
......@@ -6716,12 +6848,9 @@ async function validateInstantConfigs(
images={ctx.renderOpts.images}
/>,
{
signal: reactController.signal,
signal: reactSignal,
onError: (err: unknown, errorInfo: ErrorInfo) => {
if (
isPrerenderInterruptedError(err) ||
reactController.signal.aborted
) {
if (isPrerenderInterruptedError(err) || reactSignal.aborted) {
const componentStack = errorInfo.componentStack
if (typeof componentStack === 'string') {
trackDynamicHoleInNavigation(
......@@ -6735,7 +6864,7 @@ async function validateInstantConfigs(
)
}
return
} else if (!reactController.signal.aborted) {
} else if (!reactSignal.aborted) {
const componentStack = errorInfo.componentStack
if (typeof componentStack === 'string') {
let errorForDisplay = err
......@@ -6776,7 +6905,7 @@ async function validateInstantConfigs(
}
)
reactController.signal.addEventListener(
reactSignal.addEventListener(
'abort',
() => {
renderController.abort()
......@@ -6827,6 +6956,13 @@ async function validateInstantConfigs(
if (previousBoundaryState === null && payloadResult.hasAmbiguousErrors) {
// This is the first validation attempt. we prepared a payload where dynamic holes might be runtime data dependencies
// or dynamic data dependencies. We do a followup validation using a payload with only Runtime segments to discriminate
if (
validationAbortSignal !== undefined &&
!(await yieldToForegroundRequest(validationAbortSignal))
) {
return []
}
const dynamicOnlyResult = await validateAtDepthImpl(
prefetchKind,
depth,
......@@ -6873,6 +7009,13 @@ async function validateInstantConfigs(
: '...')
)
if (
validationAbortSignal !== undefined &&
!(await yieldToForegroundRequest(validationAbortSignal))
) {
return []
}
const result = await validateAtDepth(
prefetchKind,
depth,
......
import {
DevValidationScheduler,
yieldToForegroundRequest,
} from './dev-validation-scheduler'
describe('dev validation scheduler', () => {
it('supersedes validation from an older request in the same document', () => {
const scheduler = new DevValidationScheduler(100)
const first = scheduler.begin('document-a')
expect(first.signal.aborted).toBe(false)
const second = scheduler.begin('document-a')
expect(first.signal.aborted).toBe(true)
expect(second.signal.aborted).toBe(false)
expect(scheduler.size).toBe(1)
})
it('does not supersede validation from another document', () => {
const scheduler = new DevValidationScheduler(100)
const first = scheduler.begin('document-a')
scheduler.begin('document-b')
expect(first.signal.aborted).toBe(false)
expect(scheduler.size).toBe(2)
})
it.each(['success', 'failure'])(
'removes a generation after %s',
(outcome) => {
const scheduler = new DevValidationScheduler(100)
const generation = scheduler.begin('document-a')
try {
if (outcome === 'failure') {
throw new Error('validation failed')
}
} catch {
// Validation failures use the same terminal cleanup callback.
} finally {
generation.finish()
}
expect(generation.signal.aborted).toBe(false)
expect(scheduler.size).toBe(0)
}
)
it('does not let stale cleanup remove a replacement generation', () => {
const scheduler = new DevValidationScheduler(100)
const first = scheduler.begin('document-a')
const second = scheduler.begin('document-a')
first.finish()
expect(scheduler.size).toBe(1)
expect(second.signal.aborted).toBe(false)
second.finish()
expect(scheduler.size).toBe(0)
})
it('bounds active validations and aborts the oldest generation', () => {
const scheduler = new DevValidationScheduler(2)
const first = scheduler.begin('document-a')
const second = scheduler.begin('document-b')
const third = scheduler.begin('document-c')
expect(first.signal.aborted).toBe(true)
expect(second.signal.aborted).toBe(false)
expect(third.signal.aborted).toBe(false)
expect(scheduler.size).toBe(2)
first.finish()
second.finish()
third.finish()
expect(scheduler.size).toBe(0)
})
it('remains bounded after more than 100 unique documents', () => {
const scheduler = new DevValidationScheduler(100)
const generations = Array.from({ length: 101 }, (_, index) =>
scheduler.begin(`document-${index}`)
)
expect(generations[0].signal.aborted).toBe(true)
expect(generations[1].signal.aborted).toBe(false)
expect(scheduler.size).toBe(100)
})
it('observes supersession while yielding to incoming requests', async () => {
const scheduler = new DevValidationScheduler(100)
const first = scheduler.begin('document-a')
const canContinue = yieldToForegroundRequest(first.signal)
scheduler.begin('document-a')
await expect(canContinue).resolves.toBe(false)
})
})
import { unpatchedSetImmediate } from '../node-environment-extensions/fast-set-immediate.external'
import { InvariantError } from '../../shared/lib/invariant-error'
const MAX_ACTIVE_DEV_VALIDATIONS = 100
export interface DevValidationGeneration {
readonly signal: AbortSignal
finish(): void
}
export class DevValidationScheduler {
private readonly currentValidationByDocument = new Map<
string,
AbortController
>()
constructor(private readonly maxActiveValidations: number) {
if (maxActiveValidations < 1) {
throw new InvariantError(
'DevValidationScheduler requires at least one active validation'
)
}
}
get size(): number {
return this.currentValidationByDocument.size
}
begin(htmlRequestId: string): DevValidationGeneration {
const previousController =
this.currentValidationByDocument.get(htmlRequestId)
if (previousController !== undefined) {
this.currentValidationByDocument.delete(htmlRequestId)
previousController.abort()
}
if (this.currentValidationByDocument.size >= this.maxActiveValidations) {
const oldestEntry = this.currentValidationByDocument
.entries()
.next().value
if (oldestEntry !== undefined) {
const [oldestHtmlRequestId, oldestController] = oldestEntry
this.currentValidationByDocument.delete(oldestHtmlRequestId)
oldestController.abort()
}
}
const controller = new AbortController()
this.currentValidationByDocument.set(htmlRequestId, controller)
return {
signal: controller.signal,
finish: () => {
// A superseded generation may settle after its replacement. Only the
// current generation is allowed to remove this document's entry.
if (
this.currentValidationByDocument.get(htmlRequestId) === controller
) {
this.currentValidationByDocument.delete(htmlRequestId)
}
},
}
}
}
// Only active validation work is retained. The hard cap also bounds the
// registry if user code suspends indefinitely and the work never settles.
const devValidationScheduler = new DevValidationScheduler(
MAX_ACTIVE_DEV_VALIDATIONS
)
export function beginDevValidation(
htmlRequestId: string
): DevValidationGeneration {
return devValidationScheduler.begin(htmlRequestId)
}
/**
* Give incoming requests a chance to enter app rendering and supersede the
* current validation before another expensive render attempt starts.
*
* The regular global `setImmediate` is patched by staged rendering and can run
* inside the current timer task. The original immediate is required here so we
* actually pass through the event-loop poll phase where HTTP requests arrive.
*/
export async function yieldToForegroundRequest(
validationSignal: AbortSignal | undefined
): Promise<boolean> {
if (validationSignal?.aborted) {
return false
}
await new Promise<void>((resolve) => unpatchedSetImmediate(resolve))
return validationSignal?.aborted !== true
}
import { MockedResponse } from '../lib/mock-request'
import { waitForResponseToFinish } from './wait-for-response'
describe('waitForResponseToFinish', () => {
it('waits for the response to finish', async () => {
const response = new MockedResponse()
const onSettled = jest.fn()
const completion = waitForResponseToFinish(response)
void completion.then(onSettled)
await Promise.resolve()
expect(onSettled).not.toHaveBeenCalled()
response.end()
await expect(completion).resolves.toBe(true)
})
it('resolves immediately when the response has already finished', async () => {
const response = new MockedResponse()
const finished = new Promise<void>((resolve) => {
response.once('finish', resolve)
})
response.end()
await finished
await expect(waitForResponseToFinish(response)).resolves.toBe(true)
})
it('returns false when the connection closes before the response finishes', async () => {
const response = new MockedResponse()
const completion = waitForResponseToFinish(response)
response.destroy()
await expect(completion).resolves.toBe(false)
})
})
import type { ServerResponse } from 'node:http'
/**
* Waits for a Node response to finish normally. Returns false when the
* connection closes before the response has finished.
*/
export function waitForResponseToFinish(
response: ServerResponse
): Promise<boolean> {
if (response.writableFinished) {
return Promise.resolve(true)
}
if (response.destroyed) {
return Promise.resolve(false)
}
return new Promise((resolve) => {
function cleanup() {
response.off('finish', onFinish)
response.off('close', onClose)
}
function onFinish() {
cleanup()
resolve(true)
}
function onClose() {
cleanup()
resolve(response.writableFinished)
}
response.on('finish', onFinish)
response.on('close', onClose)
})
}
'use client'
import Link from 'next/link'
import { useState, type ReactNode } from 'react'
export function LinkAccordion({
href,
children,
prefetch,
}: {
href: string
children: ReactNode
prefetch?: boolean
}) {
const [isVisible, setIsVisible] = useState(false)
return (
<>
<input
type="checkbox"
checked={isVisible}
onChange={() => setIsVisible(!isVisible)}
data-link-accordion={href}
/>
{isVisible ? (
<Link href={href} prefetch={prefetch}>
{children}
</Link>
) : (
`${children} (link is hidden)`
)}
</>
)
}
import { LinkAccordion } from './link-accordion'
export function RoutePage({ slug }: { slug: 'a' | 'b' | 'c' }) {
return (
<main>
<p id="route">route-{slug}</p>
{slug !== 'a' && (
<LinkAccordion href="/routes/a" prefetch={false}>
Navigate to A
</LinkAccordion>
)}
{slug !== 'b' && (
<LinkAccordion href="/routes/b" prefetch={false}>
Navigate to B
</LinkAccordion>
)}
{slug !== 'c' && (
<LinkAccordion href="/routes/c" prefetch={false}>
Navigate to C
</LinkAccordion>
)}
</main>
)
}
import type { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
import { LinkAccordion } from './components/link-accordion'
export default function Page() {
return (
<main>
<h1>Document B hub</h1>
<LinkAccordion href="/routes/a" prefetch={false}>
Navigate to A
</LinkAccordion>
<LinkAccordion href="/routes/b" prefetch={false}>
Navigate to B
</LinkAccordion>
</main>
)
}
import { RoutePage } from '../../components/route-page'
export const instant = true
export default function Page() {
return <RoutePage slug="a" />
}
import { RoutePage } from '../../components/route-page'
export const instant = true
export default function Page() {
return <RoutePage slug="b" />
}
import { RoutePage } from '../../components/route-page'
export const instant = true
export default function Page() {
return <RoutePage slug="c" />
}
import { nextTestSetup } from 'e2e-utils'
import {
parseValidationMessages,
waitForValidationEnd,
waitForValidationStart,
type ValidationEvent,
} from 'e2e-utils/instant-validation'
import { retry } from 'next-test-utils'
import { createRouterAct } from 'router-act'
import type * as Playwright from 'playwright'
describe('instant-validation-scheduling', () => {
const { next } = nextTestSetup({
files: __dirname,
env: {
NEXT_TEST_LOG_VALIDATION: '1',
// Keep validation active while the next document request reaches the
// scheduler; cancellation releases this delay immediately.
NEXT_TEST_DEV_VALIDATION_DELAY_MS: '2000',
},
})
async function waitForValidationTerminal(
requestId: string,
getOutput: () => string
): Promise<ValidationEvent> {
return retry(async () => {
const terminal = parseValidationMessages(getOutput()).find(
(event) =>
event.requestId === requestId &&
(event.type === 'validation_end' ||
event.type === 'validation_aborted')
)
expect(terminal).toBeDefined()
return terminal!
})
}
async function navigate(
browser: Awaited<ReturnType<typeof next.browser>>,
act: ReturnType<typeof createRouterAct>,
href: string,
destinationText: string
) {
await act(async () => {
await browser.elementByCss(`input[data-link-accordion="${href}"]`).click()
await browser.elementByCss(`a[href="${href}"]`).click()
})
await retry(async () => {
expect(await browser.elementById('route').text()).toBe(destinationText)
})
}
it('aborts superseded validations from successive navigations in one document', async () => {
let page!: Playwright.Page
const browser = await next.browser('/', {
beforePageLoad(browserPage: Playwright.Page) {
page = browserPage
},
})
const act = createRouterAct(page)
const cliStart = next.cliOutput.length
const getOutput = () => next.cliOutput.slice(cliStart)
await navigate(browser, act, '/routes/a', 'route-a')
const validationA = await waitForValidationStart(
await browser.url(),
getOutput
)
await navigate(browser, act, '/routes/b', 'route-b')
await expect(
waitForValidationTerminal(validationA.requestId, getOutput)
).resolves.toMatchObject({ type: 'validation_aborted' })
const validationB = await waitForValidationStart(
await browser.url(),
getOutput
)
await navigate(browser, act, '/routes/c', 'route-c')
await expect(
waitForValidationTerminal(validationB.requestId, getOutput)
).resolves.toMatchObject({ type: 'validation_aborted' })
const validationC = await waitForValidationStart(
await browser.url(),
getOutput
)
await expect(
waitForValidationEnd(validationC, getOutput)
).resolves.toMatchObject({ type: 'validation_end' })
})
it('does not supersede validation from another browser document', async () => {
let pageB!: Playwright.Page
const browserB = await next.browser('/', {
beforePageLoad(browserPage: Playwright.Page) {
pageB = browserPage
},
})
const actB = createRouterAct(pageB)
const cliStart = next.cliOutput.length
const getOutput = () => next.cliOutput.slice(cliStart)
const pageA = await pageB.context().newPage()
await pageA.goto(`${next.url}/routes/a`)
const validationA = await waitForValidationStart(pageA.url(), getOutput)
await navigate(browserB, actB, '/routes/b', 'route-b')
const validationB = await waitForValidationStart(
await browserB.url(),
getOutput
)
await expect(
waitForValidationEnd(validationA, getOutput)
).resolves.toMatchObject({ type: 'validation_end' })
await expect(
waitForValidationEnd(validationB, getOutput)
).resolves.toMatchObject({ type: 'validation_end' })
await pageA.close()
})
})
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
cacheComponents: true,
}
module.exports = nextConfig
......@@ -98,7 +98,10 @@ export function runInstantValidationTests(
browser: Playwright,
url: string
): Promise<void> {
await waitForValidation(url, getCliOutputSinceMark)
const { start } = await waitForValidation(url, getCliOutputSinceMark)
if (start.responseFinished !== undefined) {
expect(start.responseFinished).toBe(true)
}
await waitForNoErrorToast(browser, NO_VALIDATION_ERRORS_WAIT)
}
......
......@@ -11,14 +11,17 @@ type ValidationStartEvent = {
type: 'validation_start'
requestId: string
url: string
/** Only reported for development validation. */
responseFinished?: boolean
}
type ValidationEndEvent = {
type: 'validation_end'
requestId: string
url: string
}
// Emitted instead of a start/end pair when a request is aborted before its
// detached validation runs (e.g. Server Components HMR cancellation).
// Emitted when detached validation is aborted. It can appear without a start
// when cancellation happens before validation runs, or after a start instead
// of an end when in-flight validation is cancelled.
type ValidationAbortedEvent = {
type: 'validation_aborted'
requestId: string
......
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