Skip to content

Commit d915891

Browse files
gnoffunstubbable
andauthored
[Flight] Prune debug info when chunks error (react#36782)
Flight filters debug information by the consumer end time when a model initializes successfully. If the stream errors while the model is pending, already parsed debug information previously remained unfiltered and could produce stacks for work after the cutoff. Apply the same cutoff when transitioning a chunk to the errored state. Truncate the existing debug info array in place because the suspended Lazy already references that array, and Fizz reads the Lazy's debug info during abort. --------- Co-authored-by: Hendrik Liebau <mail@hendrik-liebau.de>
1 parent bf76955 commit d915891

2 files changed

Lines changed: 177 additions & 3 deletions

File tree

packages/react-client/src/ReactFlightClient.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ type Response = {
361361
_debugRootStack?: null | Error, // DEV-only
362362
_debugRootTask?: null | ConsoleTask, // DEV-only
363363
_debugStartTime: number, // DEV-only
364-
_debugEndTime?: number, // DEV-only
364+
_debugEndTime: null | number, // DEV-only
365365
_debugIOStarted: boolean, // DEV-only
366366
_debugFindSourceMapURL?: void | FindSourceMapURLCallback, // DEV-only
367367
_debugChannel?: void | DebugChannel, // DEV-only
@@ -499,7 +499,6 @@ function filterDebugInfo(
499499
response: Response,
500500
value: {_debugInfo: ReactDebugInfo, ...},
501501
) {
502-
// $FlowFixMe[invalid-compare]
503502
if (response._debugEndTime === null) {
504503
// No end time was defined, so we keep all debug info entries.
505504
return;
@@ -523,6 +522,29 @@ function filterDebugInfo(
523522
value._debugInfo = debugInfo;
524523
}
525524

525+
function pruneDebugInfoAfterError(
526+
response: Response,
527+
chunk: ErroredChunk<any>,
528+
): void {
529+
if (response._debugEndTime === null) {
530+
return;
531+
}
532+
533+
const relativeEndTime =
534+
response._debugEndTime -
535+
// $FlowFixMe[prop-missing]
536+
performance.timeOrigin;
537+
const debugInfo = chunk._debugInfo;
538+
for (let i = 0; i < debugInfo.length; i++) {
539+
const info = debugInfo[i];
540+
if (typeof info.time === 'number' && info.time > relativeEndTime) {
541+
// This array may already be attached to the Lazy suspended in Fizz.
542+
debugInfo.length = i;
543+
return;
544+
}
545+
}
546+
}
547+
526548
function moveDebugInfoFromChunkToInnerValue<T>(
527549
chunk: InitializedChunk<T> | InitializedStreamChunk<any>,
528550
value: T,
@@ -764,6 +786,9 @@ function triggerErrorOnChunk<T>(
764786
const erroredChunk: ErroredChunk<T> = chunk as any;
765787
erroredChunk.status = ERRORED;
766788
erroredChunk.reason = error;
789+
if (__DEV__) {
790+
pruneDebugInfoAfterError(response, erroredChunk);
791+
}
767792
if (listeners !== null) {
768793
rejectChunk(response, listeners, error);
769794
}
@@ -2762,7 +2787,7 @@ function ResponseInstance(
27622787
// and is not considered I/O required to load the stream.
27632788
setTimeout(markIOStarted.bind(this), 0);
27642789
}
2765-
this._debugEndTime = debugEndTime == null ? null : debugEndTime;
2790+
this._debugEndTime = debugEndTime === undefined ? null : debugEndTime;
27662791
this._debugFindSourceMapURL = findSourceMapURL;
27672792
this._debugChannel = debugChannel;
27682793
this._blockedConsole = null;

packages/react-server-dom-webpack/src/__tests__/ReactFlightDOMNode-test.js

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2055,6 +2055,155 @@ describe('ReactFlightDOMNode', () => {
20552055
'ssr-abort',
20562056
);
20572057
});
2058+
2059+
// @gate __DEV__
2060+
it('filters parsed debug info when the Flight stream errors', async () => {
2061+
let resolveInitialData;
2062+
const laterDataResolvers = [];
2063+
2064+
async function getInitialData() {
2065+
return new Promise(resolve => {
2066+
resolveInitialData = resolve;
2067+
});
2068+
}
2069+
2070+
async function loadInitialData() {
2071+
return await getInitialData();
2072+
}
2073+
2074+
async function loadLaterData() {
2075+
for (let i = 0; i < 40; i++) {
2076+
await new Promise(resolve => {
2077+
laterDataResolvers[i] = resolve;
2078+
});
2079+
}
2080+
}
2081+
2082+
async function Dynamic() {
2083+
await loadInitialData();
2084+
await loadLaterData();
2085+
return ReactServer.createElement('p', null, 'Done');
2086+
}
2087+
2088+
function App() {
2089+
return ReactServer.createElement(
2090+
'html',
2091+
null,
2092+
ReactServer.createElement(
2093+
'body',
2094+
null,
2095+
ReactServer.createElement(Dynamic),
2096+
),
2097+
);
2098+
}
2099+
2100+
let staticEndTime = -1;
2101+
const chunks = [];
2102+
2103+
await new Promise(resolve => {
2104+
setTimeout(() => {
2105+
const flightStream = ReactServerDOMServer.renderToPipeableStream(
2106+
ReactServer.createElement(App),
2107+
webpackMap,
2108+
{
2109+
filterStackFrame,
2110+
},
2111+
);
2112+
2113+
const passThrough = new Stream.PassThrough(streamOptions);
2114+
flightStream.pipe(passThrough);
2115+
passThrough.on('data', chunk => {
2116+
chunks.push(chunk);
2117+
});
2118+
passThrough.on('end', resolve);
2119+
});
2120+
2121+
setTimeout(() => {
2122+
staticEndTime = performance.now() + performance.timeOrigin;
2123+
resolveInitialData();
2124+
2125+
let index = 0;
2126+
function resolveNext() {
2127+
setTimeout(() => {
2128+
laterDataResolvers[index++]();
2129+
if (index < 40) {
2130+
resolveNext();
2131+
}
2132+
});
2133+
}
2134+
setTimeout(resolveNext);
2135+
});
2136+
});
2137+
2138+
const contentStream = new Stream.Readable({
2139+
...streamOptions,
2140+
read() {},
2141+
});
2142+
const response = ReactServerDOMClient.createFromNodeStream(
2143+
contentStream,
2144+
{
2145+
moduleMap: null,
2146+
moduleLoading: null,
2147+
serverModuleMap: null,
2148+
},
2149+
{
2150+
endTime: staticEndTime,
2151+
},
2152+
);
2153+
// The final write contains the completed model. The preceding writes
2154+
// contain the debug rows produced while rendering it.
2155+
for (let i = 0; i < chunks.length - 1; i++) {
2156+
contentStream.push(chunks[i]);
2157+
}
2158+
2159+
const decoded = await response;
2160+
2161+
function ClientRoot() {
2162+
return decoded;
2163+
}
2164+
2165+
const flightError = new Error('Flight stream errored');
2166+
const fizzAbortController = new AbortController();
2167+
let caughtError;
2168+
let ownerStack;
2169+
const {prelude} = await new Promise(resolve => {
2170+
let result;
2171+
2172+
setTimeout(() => {
2173+
result = ReactDOMFizzStatic.prerenderToNodeStream(
2174+
React.createElement(ClientRoot),
2175+
{
2176+
signal: fizzAbortController.signal,
2177+
onError(error) {
2178+
caughtError = error;
2179+
ownerStack = React.captureOwnerStack
2180+
? React.captureOwnerStack()
2181+
: null;
2182+
},
2183+
},
2184+
);
2185+
});
2186+
2187+
setTimeout(() => {
2188+
contentStream.emit('error', flightError);
2189+
contentStream.push(null);
2190+
fizzAbortController.abort(new Error('Fizz aborted'));
2191+
resolve(result);
2192+
});
2193+
});
2194+
2195+
expect(await readResult(prelude)).toBe('');
2196+
expect(caughtError).toBe(flightError);
2197+
expect(normalizeCodeLocInfo(ownerStack)).toBe(
2198+
'\n' +
2199+
gate(flags =>
2200+
flags.enableAsyncDebugInfo
2201+
? ' in loadInitialData (at **)\n' + ' in Dynamic (at **)\n'
2202+
: '',
2203+
) +
2204+
' in App (at **)',
2205+
);
2206+
});
20582207
});
20592208

20602209
it('warns with a tailored message if eval is not available in dev', async () => {

0 commit comments

Comments
 (0)