Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Flight] don't emit chunks for rejected thenables after abort #31169

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3084,4 +3084,54 @@ describe('ReactFlightDOM', () => {
</div>,
);
});

it('rejecting a thenable after an abort before flush should not lead to a frozen readable', async () => {
const ClientComponent = clientExports(function (props: {
promise: Promise<void>,
}) {
return 'hello world';
});

let reject;
const promise = new Promise((_, re) => {
reject = re;
});

function App() {
return (
<div>
<Suspense fallback="loading...">
<ClientComponent promise={promise} />
</Suspense>
</div>
);
}

const errors = [];
const {writable, readable} = getTestStream();
const {pipe, abort} = await serverAct(() =>
ReactServerDOMServer.renderToPipeableStream(<App />, webpackMap, {
onError(x) {
errors.push(x);
},
}),
);
await serverAct(() => {
abort('STOP');
reject('STOP');
});
pipe(writable);

const reader = readable.getReader();
while (true) {
const {done} = await reader.read();
if (done) {
break;
}
}

expect(errors).toEqual(['STOP']);

// We expect it to get to the end here rather than hang on the reader.
});
});
35 changes: 20 additions & 15 deletions packages/react-server/src/ReactFlightServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -693,22 +693,27 @@ function serializeThenable(
pingTask(request, newTask);
},
reason => {
if (
enablePostpone &&
typeof reason === 'object' &&
reason !== null &&
(reason: any).$$typeof === REACT_POSTPONE_TYPE
) {
const postponeInstance: Postpone = (reason: any);
logPostpone(request, postponeInstance.message, newTask);
emitPostponeChunk(request, newTask.id, postponeInstance);
} else {
const digest = logRecoverableError(request, reason, newTask);
emitErrorChunk(request, newTask.id, digest, reason);
if (newTask.status === PENDING) {
// We expect that the only status it might be otherwise is ABORTED.
// When we abort we emit chunks in each pending task slot and don't need
// to do so again here.
if (
enablePostpone &&
typeof reason === 'object' &&
reason !== null &&
(reason: any).$$typeof === REACT_POSTPONE_TYPE
) {
const postponeInstance: Postpone = (reason: any);
logPostpone(request, postponeInstance.message, newTask);
emitPostponeChunk(request, newTask.id, postponeInstance);
} else {
const digest = logRecoverableError(request, reason, newTask);
emitErrorChunk(request, newTask.id, digest, reason);
}
newTask.status = ERRORED;
request.abortableTasks.delete(newTask);
enqueueFlush(request);
}
newTask.status = ERRORED;
request.abortableTasks.delete(newTask);
enqueueFlush(request);
},
);

Expand Down
Loading