Skip to content

Commit 601e5c3

Browse files
authored
[Fizz][Float] Do not write after closing the stream (#27541)
Float methods can hang on to a reference to a Request after the request is closed due to AsyncLocalStorage. If a Float method is called at this point we do not want to attempt to flush anything. This change updates the closing logic to also call `stopFlowing` which will ensure that any checks against the destination properly reflect that we cannot do any writes. In addition it updates the enqueueFlush logic to existence check the destination inside the work function since it can change across the work scheduling gap if it is async. fixes: #27540
1 parent 20c91b6 commit 601e5c3

File tree

3 files changed

+88
-2
lines changed

3 files changed

+88
-2
lines changed

packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3650,6 +3650,46 @@ describe('ReactDOMFizzServer', () => {
36503650
);
36513651
});
36523652

3653+
// https://github.com/facebook/react/issues/27540
3654+
// This test is not actually asserting much because there is possibly a bug in the closeing logic for the
3655+
// Node implementation of Fizz. The close leads to an abort which sets the destination to null before the Float
3656+
// method has an opportunity to schedule a write. We should fix this probably and once we do this test will start
3657+
// to fail if the underyling issue of writing after stream completion isn't fixed
3658+
it('does not try to write to the stream after it has been closed', async () => {
3659+
async function preloadLate() {
3660+
await 1;
3661+
ReactDOM.preconnect('foo');
3662+
}
3663+
3664+
function Preload() {
3665+
preloadLate();
3666+
return null;
3667+
}
3668+
3669+
function App() {
3670+
return (
3671+
<html>
3672+
<body>
3673+
<main>hello</main>
3674+
<Preload />
3675+
</body>
3676+
</html>
3677+
);
3678+
}
3679+
await act(() => {
3680+
renderToPipeableStream(<App />).pipe(writable);
3681+
});
3682+
3683+
expect(getVisibleChildren(document)).toEqual(
3684+
<html>
3685+
<head />
3686+
<body>
3687+
<main>hello</main>
3688+
</body>
3689+
</html>,
3690+
);
3691+
});
3692+
36533693
describe('error escaping', () => {
36543694
it('escapes error hash, message, and component stack values in directly flushed errors (html escaping)', async () => {
36553695
window.__outlet = {};

packages/react-dom/src/__tests__/ReactDOMFizzServerBrowser-test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ global.ReadableStream =
1515
global.TextEncoder = require('util').TextEncoder;
1616

1717
let React;
18+
let ReactDOM;
1819
let ReactDOMFizzServer;
1920
let Suspense;
2021

2122
describe('ReactDOMFizzServerBrowser', () => {
2223
beforeEach(() => {
2324
jest.resetModules();
2425
React = require('react');
26+
ReactDOM = require('react-dom');
2527
ReactDOMFizzServer = require('react-dom/server.browser');
2628
Suspense = React.Suspense;
2729
});
@@ -547,4 +549,37 @@ describe('ReactDOMFizzServerBrowser', () => {
547549
// However, it does error the shell.
548550
expect(caughtError.message).toEqual('testing postpone');
549551
});
552+
553+
// https://github.com/facebook/react/issues/27540
554+
// This test is not actually asserting much because in our test environment the Float method cannot find the request after
555+
// an await and thus is a noop. If we fix our test environment to support AsyncLocalStorage we can assert that the
556+
// stream does not write after closing.
557+
it('does not try to write to the stream after it has been closed', async () => {
558+
async function preloadLate() {
559+
await 1;
560+
ReactDOM.preconnect('foo');
561+
}
562+
563+
function Preload() {
564+
preloadLate();
565+
return null;
566+
}
567+
568+
function App() {
569+
return (
570+
<html>
571+
<body>
572+
<main>hello</main>
573+
<Preload />
574+
</body>
575+
</html>
576+
);
577+
}
578+
const stream = await ReactDOMFizzServer.renderToReadableStream(<App />);
579+
const result = await readResult(stream);
580+
581+
expect(result).toMatchInlineSnapshot(
582+
`"<!DOCTYPE html><html><head></head><body><main>hello</main></body></html>"`,
583+
);
584+
});
550585
});

packages/react-server/src/ReactFizzServer.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3986,6 +3986,9 @@ function flushCompletedQueues(
39863986
}
39873987
// We're done.
39883988
close(destination);
3989+
// We need to stop flowing now because we do not want any async contexts which might call
3990+
// float methods to initiate any flushes after this point
3991+
stopFlowing(request);
39893992
} else {
39903993
completeWriting(destination);
39913994
flushBuffered(destination);
@@ -4011,9 +4014,17 @@ function enqueueFlush(request: Request): void {
40114014
// happen when we start flowing again
40124015
request.destination !== null
40134016
) {
4014-
const destination = request.destination;
40154017
request.flushScheduled = true;
4016-
scheduleWork(() => flushCompletedQueues(request, destination));
4018+
scheduleWork(() => {
4019+
// We need to existence check destination again here because it might go away
4020+
// in between the enqueueFlush call and the work execution
4021+
const destination = request.destination;
4022+
if (destination) {
4023+
flushCompletedQueues(request, destination);
4024+
} else {
4025+
request.flushScheduled = false;
4026+
}
4027+
});
40174028
}
40184029
}
40194030

0 commit comments

Comments
 (0)