Skip to content

Commit 07dde64

Browse files
authored
[Fizz] Postpone a task aborted while rendering from where it started (#37710)
## Summary Fixes #37709 When a prerender is aborted while a retried render task is on the stack, `retryRenderTask` resets the segment to where the attempt started, but `task.keyPath` and `task.childIndex` are left at the innermost child that was rendering. `abortUnwoundTask` then postpones the task at that child's slot under its parent element, and the resume renders only that child into the boundary's segment. This restores the key path and child index the attempt started from before finishing the abort, so the postponed hole covers the whole discarded attempt. `task.node` stays as is, since DEV async debug info reads it for the owner stack. ## How did you test this change? The first commit adds a test to `ReactDOMFizzStaticBrowser-test` that aborts the prerender from a component rendered by a retried task and resumes it, asserting the current output (`<div>World</div>`). The second commit is the fix and updates the assertion to the whole `<section>`. `yarn test packages/react-dom/src/__tests__/ReactDOMFizz packages/react-server` passes with and without `-r=experimental`. With `--prod`, `ReactServer › has Owner Stacks in DEV when aborted` fails the same way on main. Prettier, ESLint and `yarn flow dom-node` are clean.
1 parent 9f2e1f8 commit 07dde64

2 files changed

Lines changed: 72 additions & 0 deletions

File tree

‎packages/react-dom/src/__tests__/ReactDOMFizzStaticBrowser-test.js‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,74 @@ describe('ReactDOMFizzStaticBrowser', () => {
861861
expect(errors).toEqual(['resume abort']);
862862
});
863863

864+
it('resumes a task that was aborted while rendering from where it started', async () => {
865+
let resolve;
866+
const promise = new Promise(r => (resolve = r));
867+
let prerendering = true;
868+
const controller = new AbortController();
869+
870+
function Abort() {
871+
if (prerendering) {
872+
controller.abort();
873+
}
874+
return 'World';
875+
}
876+
877+
function Content() {
878+
React.use(promise);
879+
return (
880+
<section>
881+
<span>Hello</span>
882+
<Abort />
883+
</section>
884+
);
885+
}
886+
887+
function App() {
888+
return (
889+
<div>
890+
<Suspense fallback="Loading...">
891+
<Content />
892+
</Suspense>
893+
</div>
894+
);
895+
}
896+
897+
const errors = [];
898+
let pendingResult;
899+
await serverAct(() => {
900+
pendingResult = ReactDOMFizzStatic.prerender(<App />, {
901+
signal: controller.signal,
902+
onError(error) {
903+
errors.push(error.message);
904+
},
905+
});
906+
});
907+
await serverAct(() => resolve());
908+
const prerendered = await pendingResult;
909+
910+
expect(errors).toEqual(['This operation was aborted']);
911+
await readIntoContainer(prerendered.prelude);
912+
expect(getVisibleChildren(container)).toEqual(<div>Loading...</div>);
913+
914+
prerendering = false;
915+
const resumed = await serverAct(() =>
916+
ReactDOMFizzServer.resume(
917+
<App />,
918+
JSON.parse(JSON.stringify(prerendered.postponed)),
919+
),
920+
);
921+
922+
await readIntoContainer(resumed);
923+
expect(getVisibleChildren(container)).toEqual(
924+
<div>
925+
<section>
926+
<span>Hello</span>World
927+
</section>
928+
</div>,
929+
);
930+
});
931+
864932
it('can prerender a preamble', async () => {
865933
const errors = [];
866934

‎packages/react-server/src/ReactFizzServer.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5368,6 +5368,8 @@ function retryRenderTask(
53685368
const chunkLength = segment.chunks.length;
53695369
// Used to detect forward progress if we hit a stack overflow below.
53705370
const startNode = task.node;
5371+
const startKeyPath = task.keyPath;
5372+
const startChildIndex = task.childIndex;
53715373
try {
53725374
// We call the destructive form that mutates this task. That way if something
53735375
// suspends again, we can reuse the same task instead of spawning a new one.
@@ -5414,6 +5416,8 @@ function retryRenderTask(
54145416
// Restore the parent instead of clearing this field because finishing
54155417
// can reenter Fizz and abort an outer render that is still on the stack.
54165418
request.currentTask = prevTask;
5419+
task.keyPath = startKeyPath;
5420+
task.childIndex = startChildIndex;
54175421
abortUnwoundTask(task, request);
54185422
return;
54195423
}

0 commit comments

Comments
 (0)