mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
[Fizz] Prevent uncloned large precomputed chunks without relying on render-time assertions (#28568)
A while back we implemented a heuristic that if a chunk was large it was assumed to be produced by the render and thus was safe to stream which results in transferring the underlying object memory. Later we ran into an issue where a precomputed chunk grew large enough to trigger this hueristic and it started causing renders to fail because once a second render had occurred the precomputed chunk would not have an underlying buffer of bytes to send and these bytes would be omitted from the stream. We implemented a technique to detect large precomputed chunks and we enforced that these always be cloned before writing. Unfortunately our test coverage was not perfect and there has been for a very long time now a usage pattern where if you complete a boundary in one flush and then complete a boundary that has stylehsheet dependencies in another flush you can get a large precomputed chunk that was not being cloned to be sent twice causing streaming errors. I've thought about why we even went with this solution in the first place and I think it was a mistake. It relies on a dev only check to catch paired with potentially version specific order of operations on the streaming side. This is too unreliable. Additionally the low limit of view size for Edge is not used in Node.js but there is not real justification for this. In this change I updated the view size for edge streaming to match Node at 2048 bytes which is still relatively small and we have no data one way or another to preference 512 over this. Then I updated the assertion logic to error anytime a precomputed chunk exceeds the size. This eliminates the need to clone these chunks by just making sure our view size is always larger than the largest precomputed chunk we can possibly write. I'm generally in favor of this for a few reasons. First, we'll always know during testing whether we've violated the limit as long as we exercise each stream config because the precomputed chunks are created in module scope. Second, we can always split up large chunks so making sure the precomptued chunk is smaller than whatever view size we actually desire is relatively trivial.
This commit is contained in:
@@ -58,12 +58,6 @@ export function typedArrayToBinaryChunk(
|
|||||||
throw new Error('Not implemented.');
|
throw new Error('Not implemented.');
|
||||||
}
|
}
|
||||||
|
|
||||||
export function clonePrecomputedChunk(
|
|
||||||
chunk: PrecomputedChunk,
|
|
||||||
): PrecomputedChunk {
|
|
||||||
return chunk;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function byteLengthOfChunk(chunk: Chunk | PrecomputedChunk): number {
|
export function byteLengthOfChunk(chunk: Chunk | PrecomputedChunk): number {
|
||||||
throw new Error('Not implemented.');
|
throw new Error('Not implemented.');
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,7 +50,6 @@ import {
|
|||||||
writeChunkAndReturn,
|
writeChunkAndReturn,
|
||||||
stringToChunk,
|
stringToChunk,
|
||||||
stringToPrecomputedChunk,
|
stringToPrecomputedChunk,
|
||||||
clonePrecomputedChunk,
|
|
||||||
} from 'react-server/src/ReactServerStreamConfig';
|
} from 'react-server/src/ReactServerStreamConfig';
|
||||||
import {
|
import {
|
||||||
resolveRequest,
|
resolveRequest,
|
||||||
@@ -4267,15 +4266,13 @@ export function writeCompletedBoundaryInstruction(
|
|||||||
) {
|
) {
|
||||||
resumableState.instructions |=
|
resumableState.instructions |=
|
||||||
SentStyleInsertionFunction | SentCompleteBoundaryFunction;
|
SentStyleInsertionFunction | SentCompleteBoundaryFunction;
|
||||||
writeChunk(
|
writeChunk(destination, completeBoundaryWithStylesScript1FullBoth);
|
||||||
destination,
|
|
||||||
clonePrecomputedChunk(completeBoundaryWithStylesScript1FullBoth),
|
|
||||||
);
|
|
||||||
} else if (
|
} else if (
|
||||||
(resumableState.instructions & SentStyleInsertionFunction) ===
|
(resumableState.instructions & SentStyleInsertionFunction) ===
|
||||||
NothingSent
|
NothingSent
|
||||||
) {
|
) {
|
||||||
resumableState.instructions |= SentStyleInsertionFunction;
|
resumableState.instructions |= SentStyleInsertionFunction;
|
||||||
|
|
||||||
writeChunk(destination, completeBoundaryWithStylesScript1FullPartial);
|
writeChunk(destination, completeBoundaryWithStylesScript1FullPartial);
|
||||||
} else {
|
} else {
|
||||||
writeChunk(destination, completeBoundaryWithStylesScript1Partial);
|
writeChunk(destination, completeBoundaryWithStylesScript1Partial);
|
||||||
|
|||||||
@@ -731,6 +731,65 @@ describe('ReactDOMFloat', () => {
|
|||||||
).toEqual(['<script src="src-of-external-runtime" async=""></script>']);
|
).toEqual(['<script src="src-of-external-runtime" async=""></script>']);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// @gate enableFloat
|
||||||
|
it('can send style insertion implementation independent of boundary commpletion instruction implementation', async () => {
|
||||||
|
await act(() => {
|
||||||
|
renderToPipeableStream(
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<Suspense fallback="loading foo...">
|
||||||
|
<BlockedOn value="foo">foo</BlockedOn>
|
||||||
|
</Suspense>
|
||||||
|
<Suspense fallback="loading bar...">
|
||||||
|
<BlockedOn value="bar">
|
||||||
|
<link rel="stylesheet" href="bar" precedence="bar" />
|
||||||
|
bar
|
||||||
|
</BlockedOn>
|
||||||
|
</Suspense>
|
||||||
|
</body>
|
||||||
|
</html>,
|
||||||
|
).pipe(writable);
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(getMeaningfulChildren(document)).toEqual(
|
||||||
|
<html>
|
||||||
|
<head />
|
||||||
|
<body>
|
||||||
|
{'loading foo...'}
|
||||||
|
{'loading bar...'}
|
||||||
|
</body>
|
||||||
|
</html>,
|
||||||
|
);
|
||||||
|
|
||||||
|
await act(() => {
|
||||||
|
resolveText('foo');
|
||||||
|
});
|
||||||
|
expect(getMeaningfulChildren(document)).toEqual(
|
||||||
|
<html>
|
||||||
|
<head />
|
||||||
|
<body>
|
||||||
|
foo
|
||||||
|
{'loading bar...'}
|
||||||
|
</body>
|
||||||
|
</html>,
|
||||||
|
);
|
||||||
|
await act(() => {
|
||||||
|
resolveText('bar');
|
||||||
|
});
|
||||||
|
expect(getMeaningfulChildren(document)).toEqual(
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<link rel="stylesheet" href="bar" data-precedence="bar" />
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
foo
|
||||||
|
{'loading bar...'}
|
||||||
|
<link rel="preload" href="bar" as="style" />
|
||||||
|
</body>
|
||||||
|
</html>,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
// @gate enableFloat
|
// @gate enableFloat
|
||||||
it('can avoid inserting a late stylesheet if it already rendered on the client', async () => {
|
it('can avoid inserting a late stylesheet if it already rendered on the client', async () => {
|
||||||
await act(() => {
|
await act(() => {
|
||||||
|
|||||||
@@ -46,9 +46,6 @@ const ReactNoopFlightServer = ReactFlightServer({
|
|||||||
stringToPrecomputedChunk(content: string): Uint8Array {
|
stringToPrecomputedChunk(content: string): Uint8Array {
|
||||||
return textEncoder.encode(content);
|
return textEncoder.encode(content);
|
||||||
},
|
},
|
||||||
clonePrecomputedChunk(chunk: Uint8Array): Uint8Array {
|
|
||||||
return chunk;
|
|
||||||
},
|
|
||||||
isClientReference(reference: Object): boolean {
|
isClientReference(reference: Object): boolean {
|
||||||
return reference.$$typeof === Symbol.for('react.client.reference');
|
return reference.$$typeof === Symbol.for('react.client.reference');
|
||||||
},
|
},
|
||||||
|
|||||||
+6
-23
@@ -22,7 +22,7 @@ export function flushBuffered(destination: Destination) {
|
|||||||
// transform streams. https://github.com/whatwg/streams/issues/960
|
// transform streams. https://github.com/whatwg/streams/issues/960
|
||||||
}
|
}
|
||||||
|
|
||||||
const VIEW_SIZE = 512;
|
const VIEW_SIZE = 2048;
|
||||||
let currentView = null;
|
let currentView = null;
|
||||||
let writtenBytes = 0;
|
let writtenBytes = 0;
|
||||||
|
|
||||||
@@ -40,15 +40,6 @@ export function writeChunk(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (chunk.byteLength > VIEW_SIZE) {
|
if (chunk.byteLength > VIEW_SIZE) {
|
||||||
if (__DEV__) {
|
|
||||||
if (precomputedChunkSet.has(chunk)) {
|
|
||||||
console.error(
|
|
||||||
'A large precomputed chunk was passed to writeChunk without being copied.' +
|
|
||||||
' Large chunks get enqueued directly and are not copied. This is incompatible with precomputed chunks because you cannot enqueue the same precomputed chunk twice.' +
|
|
||||||
' Use "cloneChunk" to make a copy of this large precomputed chunk before writing it. This is a bug in React.',
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// this chunk may overflow a single view which implies it was not
|
// this chunk may overflow a single view which implies it was not
|
||||||
// one that is cached by the streaming renderer. We will enqueu
|
// one that is cached by the streaming renderer. We will enqueu
|
||||||
// it directly and expect it is not re-used
|
// it directly and expect it is not re-used
|
||||||
@@ -120,15 +111,15 @@ export function stringToChunk(content: string): Chunk {
|
|||||||
return textEncoder.encode(content);
|
return textEncoder.encode(content);
|
||||||
}
|
}
|
||||||
|
|
||||||
const precomputedChunkSet: Set<Chunk | BinaryChunk> = __DEV__
|
|
||||||
? new Set()
|
|
||||||
: (null: any);
|
|
||||||
|
|
||||||
export function stringToPrecomputedChunk(content: string): PrecomputedChunk {
|
export function stringToPrecomputedChunk(content: string): PrecomputedChunk {
|
||||||
const precomputedChunk = textEncoder.encode(content);
|
const precomputedChunk = textEncoder.encode(content);
|
||||||
|
|
||||||
if (__DEV__) {
|
if (__DEV__) {
|
||||||
precomputedChunkSet.add(precomputedChunk);
|
if (precomputedChunk.byteLength > VIEW_SIZE) {
|
||||||
|
console.error(
|
||||||
|
'precomputed chunks must be smaller than the view size configured for this host. This is a bug in React.',
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return precomputedChunk;
|
return precomputedChunk;
|
||||||
@@ -151,14 +142,6 @@ export function typedArrayToBinaryChunk(
|
|||||||
return content.byteLength > VIEW_SIZE ? buffer.slice() : buffer;
|
return content.byteLength > VIEW_SIZE ? buffer.slice() : buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function clonePrecomputedChunk(
|
|
||||||
precomputedChunk: PrecomputedChunk,
|
|
||||||
): PrecomputedChunk {
|
|
||||||
return precomputedChunk.byteLength > VIEW_SIZE
|
|
||||||
? precomputedChunk.slice()
|
|
||||||
: precomputedChunk;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function byteLengthOfChunk(chunk: Chunk | PrecomputedChunk): number {
|
export function byteLengthOfChunk(chunk: Chunk | PrecomputedChunk): number {
|
||||||
return chunk.byteLength;
|
return chunk.byteLength;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -70,12 +70,6 @@ export function typedArrayToBinaryChunk(
|
|||||||
return content;
|
return content;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function clonePrecomputedChunk(
|
|
||||||
chunk: PrecomputedChunk,
|
|
||||||
): PrecomputedChunk {
|
|
||||||
return chunk;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function byteLengthOfChunk(chunk: Chunk | PrecomputedChunk): number {
|
export function byteLengthOfChunk(chunk: Chunk | PrecomputedChunk): number {
|
||||||
return Buffer.byteLength(chunk, 'utf8');
|
return Buffer.byteLength(chunk, 'utf8');
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-23
@@ -22,7 +22,7 @@ export function flushBuffered(destination: Destination) {
|
|||||||
// transform streams. https://github.com/whatwg/streams/issues/960
|
// transform streams. https://github.com/whatwg/streams/issues/960
|
||||||
}
|
}
|
||||||
|
|
||||||
const VIEW_SIZE = 512;
|
const VIEW_SIZE = 2048;
|
||||||
let currentView = null;
|
let currentView = null;
|
||||||
let writtenBytes = 0;
|
let writtenBytes = 0;
|
||||||
|
|
||||||
@@ -40,15 +40,6 @@ export function writeChunk(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (chunk.byteLength > VIEW_SIZE) {
|
if (chunk.byteLength > VIEW_SIZE) {
|
||||||
if (__DEV__) {
|
|
||||||
if (precomputedChunkSet.has(chunk)) {
|
|
||||||
console.error(
|
|
||||||
'A large precomputed chunk was passed to writeChunk without being copied.' +
|
|
||||||
' Large chunks get enqueued directly and are not copied. This is incompatible with precomputed chunks because you cannot enqueue the same precomputed chunk twice.' +
|
|
||||||
' Use "cloneChunk" to make a copy of this large precomputed chunk before writing it. This is a bug in React.',
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// this chunk may overflow a single view which implies it was not
|
// this chunk may overflow a single view which implies it was not
|
||||||
// one that is cached by the streaming renderer. We will enqueu
|
// one that is cached by the streaming renderer. We will enqueu
|
||||||
// it directly and expect it is not re-used
|
// it directly and expect it is not re-used
|
||||||
@@ -120,15 +111,15 @@ export function stringToChunk(content: string): Chunk {
|
|||||||
return textEncoder.encode(content);
|
return textEncoder.encode(content);
|
||||||
}
|
}
|
||||||
|
|
||||||
const precomputedChunkSet: Set<Chunk | BinaryChunk> = __DEV__
|
|
||||||
? new Set()
|
|
||||||
: (null: any);
|
|
||||||
|
|
||||||
export function stringToPrecomputedChunk(content: string): PrecomputedChunk {
|
export function stringToPrecomputedChunk(content: string): PrecomputedChunk {
|
||||||
const precomputedChunk = textEncoder.encode(content);
|
const precomputedChunk = textEncoder.encode(content);
|
||||||
|
|
||||||
if (__DEV__) {
|
if (__DEV__) {
|
||||||
precomputedChunkSet.add(precomputedChunk);
|
if (precomputedChunk.byteLength > VIEW_SIZE) {
|
||||||
|
console.error(
|
||||||
|
'precomputed chunks must be smaller than the view size configured for this host. This is a bug in React.',
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return precomputedChunk;
|
return precomputedChunk;
|
||||||
@@ -151,14 +142,6 @@ export function typedArrayToBinaryChunk(
|
|||||||
return content.byteLength > VIEW_SIZE ? buffer.slice() : buffer;
|
return content.byteLength > VIEW_SIZE ? buffer.slice() : buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function clonePrecomputedChunk(
|
|
||||||
precomputedChunk: PrecomputedChunk,
|
|
||||||
): PrecomputedChunk {
|
|
||||||
return precomputedChunk.byteLength > VIEW_SIZE
|
|
||||||
? precomputedChunk.slice()
|
|
||||||
: precomputedChunk;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function byteLengthOfChunk(chunk: Chunk | PrecomputedChunk): number {
|
export function byteLengthOfChunk(chunk: Chunk | PrecomputedChunk): number {
|
||||||
return chunk.byteLength;
|
return chunk.byteLength;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -60,12 +60,6 @@ export function typedArrayToBinaryChunk(
|
|||||||
throw new Error('Not implemented.');
|
throw new Error('Not implemented.');
|
||||||
}
|
}
|
||||||
|
|
||||||
export function clonePrecomputedChunk(
|
|
||||||
chunk: PrecomputedChunk,
|
|
||||||
): PrecomputedChunk {
|
|
||||||
return chunk;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function byteLengthOfChunk(chunk: Chunk | PrecomputedChunk): number {
|
export function byteLengthOfChunk(chunk: Chunk | PrecomputedChunk): number {
|
||||||
throw new Error('Not implemented.');
|
throw new Error('Not implemented.');
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-21
@@ -99,15 +99,6 @@ function writeViewChunk(
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (chunk.byteLength > VIEW_SIZE) {
|
if (chunk.byteLength > VIEW_SIZE) {
|
||||||
if (__DEV__) {
|
|
||||||
if (precomputedChunkSet && precomputedChunkSet.has(chunk)) {
|
|
||||||
console.error(
|
|
||||||
'A large precomputed chunk was passed to writeChunk without being copied.' +
|
|
||||||
' Large chunks get enqueued directly and are not copied. This is incompatible with precomputed chunks because you cannot enqueue the same precomputed chunk twice.' +
|
|
||||||
' Use "cloneChunk" to make a copy of this large precomputed chunk before writing it. This is a bug in React.',
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// this chunk may overflow a single view which implies it was not
|
// this chunk may overflow a single view which implies it was not
|
||||||
// one that is cached by the streaming renderer. We will enqueu
|
// one that is cached by the streaming renderer. We will enqueu
|
||||||
// it directly and expect it is not re-used
|
// it directly and expect it is not re-used
|
||||||
@@ -201,14 +192,14 @@ export function stringToChunk(content: string): Chunk {
|
|||||||
return content;
|
return content;
|
||||||
}
|
}
|
||||||
|
|
||||||
const precomputedChunkSet = __DEV__ ? new Set<PrecomputedChunk>() : null;
|
|
||||||
|
|
||||||
export function stringToPrecomputedChunk(content: string): PrecomputedChunk {
|
export function stringToPrecomputedChunk(content: string): PrecomputedChunk {
|
||||||
const precomputedChunk = textEncoder.encode(content);
|
const precomputedChunk = textEncoder.encode(content);
|
||||||
|
|
||||||
if (__DEV__) {
|
if (__DEV__) {
|
||||||
if (precomputedChunkSet) {
|
if (precomputedChunk.byteLength > VIEW_SIZE) {
|
||||||
precomputedChunkSet.add(precomputedChunk);
|
console.error(
|
||||||
|
'precomputed chunks must be smaller than the view size configured for this host. This is a bug in React.',
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -222,14 +213,6 @@ export function typedArrayToBinaryChunk(
|
|||||||
return new Uint8Array(content.buffer, content.byteOffset, content.byteLength);
|
return new Uint8Array(content.buffer, content.byteOffset, content.byteLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function clonePrecomputedChunk(
|
|
||||||
precomputedChunk: PrecomputedChunk,
|
|
||||||
): PrecomputedChunk {
|
|
||||||
return precomputedChunk.length > VIEW_SIZE
|
|
||||||
? precomputedChunk.slice()
|
|
||||||
: precomputedChunk;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function byteLengthOfChunk(chunk: Chunk | PrecomputedChunk): number {
|
export function byteLengthOfChunk(chunk: Chunk | PrecomputedChunk): number {
|
||||||
return typeof chunk === 'string'
|
return typeof chunk === 'string'
|
||||||
? Buffer.byteLength(chunk, 'utf8')
|
? Buffer.byteLength(chunk, 'utf8')
|
||||||
|
|||||||
@@ -41,7 +41,6 @@ export const closeWithError = $$$config.closeWithError;
|
|||||||
export const stringToChunk = $$$config.stringToChunk;
|
export const stringToChunk = $$$config.stringToChunk;
|
||||||
export const stringToPrecomputedChunk = $$$config.stringToPrecomputedChunk;
|
export const stringToPrecomputedChunk = $$$config.stringToPrecomputedChunk;
|
||||||
export const typedArrayToBinaryChunk = $$$config.typedArrayToBinaryChunk;
|
export const typedArrayToBinaryChunk = $$$config.typedArrayToBinaryChunk;
|
||||||
export const clonePrecomputedChunk = $$$config.clonePrecomputedChunk;
|
|
||||||
export const byteLengthOfChunk = $$$config.byteLengthOfChunk;
|
export const byteLengthOfChunk = $$$config.byteLengthOfChunk;
|
||||||
export const byteLengthOfBinaryChunk = $$$config.byteLengthOfBinaryChunk;
|
export const byteLengthOfBinaryChunk = $$$config.byteLengthOfBinaryChunk;
|
||||||
export const createFastHash = $$$config.createFastHash;
|
export const createFastHash = $$$config.createFastHash;
|
||||||
|
|||||||
Reference in New Issue
Block a user