Repository navigation
Description
Summary
In React Server Components, passing a circular object or array that contains an unserializable field (such as a function) to renderToReadableStream overflows the call stack (RangeError: Maximum call stack size exceeded) inside describeObjectForErrorMessage — in both production (NODE_ENV=production) and development builds — masking React's real serialization error.
Affected version
[email protected],[email protected], Node.jsv22.22.2(NODE_ENV=productionanddevelopment)- Verified unchanged on
mainatedae846a9a17442c00429f2b15375afbfa0376c9
Reproduction
In an empty directory:
npm install [email protected] [email protected] [email protected]
NODE_ENV=production node --conditions=react-server <<'JS'
const {renderToReadableStream} = require('react-server-dom-webpack/server.node');
async function run(label, payload) {
let err;
const stream = renderToReadableStream(payload, {}, {onError(e) { err = e; }});
try {
for await (const _ of stream) {}
} catch (e) {
if (!err) err = e;
}
console.log(label, '=>', err ? `${err.name}: ${err.message}` : 'OK');
}
(async () => {
const plain = {fn: () => {}};
const circObj = {fn: () => {}};
circObj.self = circObj;
const circArr = [() => {}];
circArr.push(circArr);
await run('Control (non-circular):', plain);
await run('Circular object: ', circObj);
await run('Circular array: ', circArr);
})();
JSActual output
Control (non-circular): => Error: Functions cannot be passed directly to Client Components unless you explicitly expose it by marking it with "use server". Or maybe you meant to call this function rather than return it.
{fn: function fn}
^^^^^^^^^^^
Circular object: => RangeError: Maximum call stack size exceeded
Circular array: => RangeError: Maximum call stack size exceeded
(The exact top frame of the RangeError stack depends on V8's stack limit — e.g. describeObjectForErrorMessage or objectName / Object.toString inside describeObjectForErrorMessage.) Circular objects and arrays without the invalid function serialize successfully, confirming the failure is specifically in error formatting once an unserializable value triggers the error path.
Expected behavior
Both circular cases should report the normal serialization Error (e.g., "Functions cannot be passed directly to Client Components...") with a cycle-safe object preview, rather than failing inside error formatting with RangeError: Maximum call stack size exceeded.
Root cause
describeObjectForErrorMessagerecursively calls itself on nested object and array properties (#L208,#L234,#L277,#L309) with no active-ancestor cycle guard or depth limit. The existing 10/15/40-character truncation thresholds are applied after the recursive call returns, so they do not bound traversal.- Although Flight itself supports circular objects and arrays via outline references,
ReactFlightServer.jsinvokesdescribeObjectForErrorMessage(parent, parentPropertyName)on production-capable error paths (#L4748,#L4859,#L4883,#L4904,#L4924) with no local guard around the formatter; the surrounding task handler (#L4173-L4175) catches the resultingRangeErrorrather than recovering the intended serialization error.
Suggested fix
Track active ancestors (add on entry, remove in a try / finally block so early returns or throwing getters cannot leak entries — rather than a global seen set, which would mislabel shared-but-acyclic references) inside describeObjectForErrorMessage, returning '...' (or '[Circular]') when encountering a cycle, and optionally adding a small depth/work cap if bounded traversal is desired for deep acyclic structures. Regression tests covering circular objects and circular arrays containing an unserializable field will lock in the fix.
Activity