Unverified Commit cd4baf82 authored by Janka Uryga's avatar Janka Uryga Committed by GitHub

[CC] Fix accumulator flushing logic (#94857)

The `waitAtLeastOnReactRenderTask()` trick we used in prerenders to wait
for flushing was subtly incorrect. It only worked if the code before it
resulted in React's `scheduleWork` being called synchronously before it
(i.e. already having its immediate scheduled when
`waitAtLeastOnReactRenderTask()` schedules its own).

This currently works, because `finishSegmentAccumulator` calls the
`then()` callbacks synchronously (which calls `pingTask` which schedules
a flush):
https://github.com/vercel/next.js/blob/40296fe53f40ffe6a0e2184879bd5e294d271ce4/packages/next/src/server/app-render/vary-params.ts#L369-L371

However, if `scheduleWork` was called in a microtask instead, then
`waitAtLeastOnReactRenderTask()` would queue its immediate first, so
it'd resolve before Flight actually flushed. This becomes the case in
#94809, where the vary params thenable is replaced with an async
iterable (which uses normal promises in the iterator, and thus always
schedules work in a microtask)

TLDR:
- replaced `await waitAtLeastOneReactRenderTask()` with a separate task
for finishing accumulators (which doesn't rely on immediate ordering,
and thus is resilient to delaying the `scheduleWork` by a couple
microtasks)
- as a drive-by cleanup, i moved the sync IO abort logic into a thing we
check on top of every task and only do once. otherwise the code was
confusing to follow, and was unnecessarily computing accumulator things
for renders that are already aborted
parent e0c7efcd
...@@ -1750,6 +1750,39 @@ async function finalRuntimeServerPrerender( ...@@ -1750,6 +1750,39 @@ async function finalRuntimeServerPrerender(
} }
} }
let didHandleUnexpectedAbort = false
/**
* @returns - whether or not the task should be skipped
* because the render was already aborted.
* */
const checkUnexpectedAbort = (): boolean => {
if (finalServerController.signal.aborted) {
// If the server controller is already aborted, then we must have encountered sync IO
if (!didHandleUnexpectedAbort) {
didHandleUnexpectedAbort = true
onUnexpectedAbort()
}
return true
}
// Not aborted.
return false
}
const onUnexpectedAbort = () => {
resultIsPartial = true
// FIXME(NAR-810): If we're already aborted due to Sync IO, there should be no need to
// finish the accumulators. However, it seems like in `--debug-prerender`
// the stream will stay open if we don't close the iterable here.
if (
process.env.NODE_ENV === 'development' &&
staleTimeIterable !== undefined
) {
staleTimeIterable.close()
}
}
await runInSequentialTasks( await runInSequentialTasks(
async () => { async () => {
// Runtime-prefetchable segments render immediately in the early stage. // Runtime-prefetchable segments render immediately in the early stage.
...@@ -1778,26 +1811,33 @@ async function finalRuntimeServerPrerender( ...@@ -1778,26 +1811,33 @@ async function finalRuntimeServerPrerender(
) )
}, },
() => { () => {
if (checkUnexpectedAbort()) return
// Resolve the promise holding back non-prefetchable segments so they can begin rendering. // Resolve the promise holding back non-prefetchable segments so they can begin rendering.
finalStageController.advanceStage(RenderStage.ShellStatic) finalStageController.advanceStage(RenderStage.ShellStatic)
}, },
() => { () => {
if (checkUnexpectedAbort()) return
finalStageController.advanceStage(RenderStage.EarlyStatic) finalStageController.advanceStage(RenderStage.EarlyStatic)
}, },
() => { () => {
if (checkUnexpectedAbort()) return
finalStageController.advanceStage(RenderStage.Static) finalStageController.advanceStage(RenderStage.Static)
}, },
() => { () => {
if (checkUnexpectedAbort()) return
// Resolve session data for runtime-prefetchable segments. // Resolve session data for runtime-prefetchable segments.
// Sync IO is NOT allowed here. // Sync IO is NOT allowed here.
finalStageController.advanceStage(RenderStage.ShellEarlyRuntime) finalStageController.advanceStage(RenderStage.ShellEarlyRuntime)
}, },
() => { () => {
if (checkUnexpectedAbort()) return
// Resolve session data for non-prefetchable segments. // Resolve session data for non-prefetchable segments.
// Sync IO is allowed here. // Sync IO is allowed here.
finalStageController.advanceStage(RenderStage.ShellRuntime) finalStageController.advanceStage(RenderStage.ShellRuntime)
}, },
() => { () => {
if (checkUnexpectedAbort()) return
if (mode.type === 'session-shell-only') { if (mode.type === 'session-shell-only') {
// We're only rendering a shell, so we do not advance to stages where link data is resolved. // We're only rendering a shell, so we do not advance to stages where link data is resolved.
return return
...@@ -1807,6 +1847,8 @@ async function finalRuntimeServerPrerender( ...@@ -1807,6 +1847,8 @@ async function finalRuntimeServerPrerender(
finalStageController.advanceStage(RenderStage.EarlyRuntime) finalStageController.advanceStage(RenderStage.EarlyRuntime)
}, },
() => { () => {
if (checkUnexpectedAbort()) return
if (mode.type === 'session-shell-only') { if (mode.type === 'session-shell-only') {
// We're only rendering a shell, so we do not advance to stages where link data is resolved. // We're only rendering a shell, so we do not advance to stages where link data is resolved.
return return
...@@ -1816,25 +1858,12 @@ async function finalRuntimeServerPrerender( ...@@ -1816,25 +1858,12 @@ async function finalRuntimeServerPrerender(
// TODO(app-shells): This is strange: we allow sync IO here, but we don't want sync IO in a fallback. // TODO(app-shells): This is strange: we allow sync IO here, but we don't want sync IO in a fallback.
finalStageController.advanceStage(RenderStage.Runtime) finalStageController.advanceStage(RenderStage.Runtime)
}, },
async () => { () => {
if (finalServerController.signal.aborted) { if (checkUnexpectedAbort()) return
// If the server controller is already aborted we must have called
// something that required aborting the prerender synchronously such
// as with new Date()
resultIsPartial = true
// FIXME(NAR-810): If we're already aborted due to Sync IO, there should be no need to
// finish the accumulators. However, it seems like in `--debug-prerender`
// the stream will stay open if we don't close the iterable here.
if (
process.env.NODE_ENV === 'development' &&
staleTimeIterable !== undefined
) {
staleTimeIterable.close()
}
return // Finish the accumulators. We need to wait for Flight to flush the result into the stream,
} // which is scheduled in a (fast) immediate, so we do this in a separate task
// (fast immediates will be drained at the end of the task, so in the next task we know we're done flushing)
// Check if session data unblocked new content in the shell. // Check if session data unblocked new content in the shell.
if ( if (
...@@ -1866,16 +1895,16 @@ async function finalRuntimeServerPrerender( ...@@ -1866,16 +1895,16 @@ async function finalRuntimeServerPrerender(
staleTimeIterable.close() staleTimeIterable.close()
finishAccumulatingVaryParams(varyParamsAccumulator) finishAccumulatingVaryParams(varyParamsAccumulator)
},
// We're using a render, not a prerender, so React schedules rendering work in fast immediates, () => {
// and we need to wait a fast immediate for the stale time/vary params chunks to flush. if (checkUnexpectedAbort()) return
await waitAtLeastOneReactRenderTask()
if (streamState.isPending) { if (streamState.isPending) {
// If the prerender is still pending then it must depend on dynamic data // If the prerender is still pending then it must depend on dynamic data
// (or, if this is a shell prefetch, link data) // (or, if this is a shell prefetch, link data)
resultIsPartial = true resultIsPartial = true
} }
workUnitAsyncStorage.run( workUnitAsyncStorage.run(
finalServerPrerenderStore, finalServerPrerenderStore,
finalServerController.abort.bind(finalServerController) finalServerController.abort.bind(finalServerController)
...@@ -7637,6 +7666,39 @@ async function prerenderToStream( ...@@ -7637,6 +7666,39 @@ async function prerenderToStream(
} }
} }
let didHandleUnexpectedAbort = false
/**
* @returns - whether or not the task should be skipped
* because the render was already aborted.
* */
const checkUnexpectedAbort = (): boolean => {
if (finalServerReactController.signal.aborted) {
// If the server controller is already aborted, then we must have encountered sync IO
if (!didHandleUnexpectedAbort) {
didHandleUnexpectedAbort = true
onUnexpectedAbort()
}
return true
}
// Not aborted.
return false
}
const onUnexpectedAbort = () => {
resultIsPartial = true
// FIXME(NAR-810): If we're already aborted due to Sync IO, there should be no need to
// finish the accumulators. However, it seems like in `--debug-prerender`
// the stream will stay open if we don't close the iterable here.
if (
process.env.NODE_ENV === 'development' &&
staleTimeIterable !== undefined
) {
staleTimeIterable.close()
}
}
let debugEndTime: number | undefined = undefined let debugEndTime: number | undefined = undefined
let didLinkDataUnblockNewContent = false let didLinkDataUnblockNewContent = false
...@@ -7692,25 +7754,15 @@ async function prerenderToStream( ...@@ -7692,25 +7754,15 @@ async function prerenderToStream(
) )
}, },
() => { () => {
if (checkUnexpectedAbort()) return
finalStageController.advanceStage(RenderStage.Static) finalStageController.advanceStage(RenderStage.Static)
}, },
async () => { () => {
if (finalServerReactController.signal.aborted) { if (checkUnexpectedAbort()) return
// If the server controller is already aborted we must have called something
// that required aborting the prerender synchronously such as with new Date()
resultIsPartial = true
// FIXME(NAR-810): If we're already aborted due to Sync IO, there should be no need to // Finish the accumulators. We need to wait for Flight to flush the result into the stream,
// finish the accumulators. However, it seems like in `--debug-prerender` // which is scheduled in a (fast) immediate, so we do this in a separate task
// the stream will stay open if we don't close the iterable here. // (fast immediates will be drained at the end of the task, so in the next task we know we're done flushing)
if (
process.env.NODE_ENV === 'development' &&
staleTimeIterable !== undefined
) {
staleTimeIterable.close()
}
return
}
// If new chunks were emitted in the static stage // If new chunks were emitted in the static stage
// (after unblocking link data, i.e. static params) // (after unblocking link data, i.e. static params)
...@@ -7744,10 +7796,9 @@ async function prerenderToStream( ...@@ -7744,10 +7796,9 @@ async function prerenderToStream(
: null : null
) )
} }
},
// We're using a render, not a prerender, so React schedules rendering work in fast immediates, () => {
// and we need to wait a fast immediate for the above accumulators to flush. if (checkUnexpectedAbort()) return
await waitAtLeastOneReactRenderTask()
if (streamState.isPending) { if (streamState.isPending) {
// If prerenderIsPending then we have blocked for longer than a Task and we assume // If prerenderIsPending then we have blocked for longer than a Task and we assume
......
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