Skip to content

[pull] main from facebook:main #182

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

Merged
merged 1 commit into from
Jul 17, 2025
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
87 changes: 79 additions & 8 deletions packages/react-server/src/ReactFlightServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,49 @@ function devirtualizeURL(url: string): string {
return url;
}

function isPromiseCreationInternal(url: string, functionName: string): boolean {
// Various internals of the JS VM can create Promises but the call frame of the
// internals are not very interesting for our purposes so we need to skip those.
if (url === 'node:internal/async_hooks') {
// Ignore the stack frames from the async hooks themselves.
return true;
}
if (url !== '') {
return false;
}
switch (functionName) {
case 'new Promise':
case 'Function.withResolvers':
case 'Function.reject':
case 'Function.resolve':
case 'Function.all':
case 'Function.allSettled':
case 'Function.race':
case 'Function.try':
return true;
default:
return false;
}
}

function stripLeadingPromiseCreationFrames(
stack: ReactStackTrace,
): ReactStackTrace {
for (let i = 0; i < stack.length; i++) {
const callsite = stack[i];
const functionName = callsite[0];
const url = callsite[1];
if (!isPromiseCreationInternal(url, functionName)) {
if (i > 0) {
return stack.slice(i);
} else {
return stack;
}
}
}
return [];
}

function findCalledFunctionNameFromStackTrace(
request: Request,
stack: ReactStackTrace,
Expand All @@ -207,11 +250,7 @@ function findCalledFunctionNameFromStackTrace(
const url = devirtualizeURL(callsite[1]);
const lineNumber = callsite[2];
const columnNumber = callsite[3];
if (functionName === 'new Promise') {
// Ignore Promise constructors.
} else if (url === 'node:internal/async_hooks') {
// Ignore the stack frames from the async hooks themselves.
} else if (filterStackFrame(url, functionName, lineNumber, columnNumber)) {
if (filterStackFrame(url, functionName, lineNumber, columnNumber)) {
if (bestMatch === '') {
// If we had no good stack frames for internal calls, just use the last
// first party function name.
Expand Down Expand Up @@ -275,13 +314,44 @@ function hasUnfilteredFrame(request: Request, stack: ReactStackTrace): boolean {
return false;
}

function isPromiseAwaitInternal(url: string, functionName: string): boolean {
// Various internals of the JS VM can await internally on a Promise. If those are at
// the top of the stack then we don't want to consider them as internal frames. The
// true "await" conceptually is the thing that called the helper.
// Ideally we'd also include common third party helpers for this.
if (url === 'node:internal/async_hooks') {
// Ignore the stack frames from the async hooks themselves.
return true;
}
if (url !== '') {
return false;
}
switch (functionName) {
case 'Promise.then':
case 'Promise.catch':
case 'Promise.finally':
case 'Function.reject':
case 'Function.resolve':
case 'Function.all':
case 'Function.allSettled':
case 'Function.race':
case 'Function.try':
return true;
default:
return false;
}
}

export function isAwaitInUserspace(
request: Request,
stack: ReactStackTrace,
): boolean {
let firstFrame = 0;
while (stack.length > firstFrame && stack[firstFrame][0] === 'Promise.then') {
// Skip Promise.then frame itself.
while (
stack.length > firstFrame &&
isPromiseAwaitInternal(stack[firstFrame][1], stack[firstFrame][0])
) {
// Skip the internal frame that awaits itself.
firstFrame++;
}
if (stack.length > firstFrame) {
Expand Down Expand Up @@ -4213,7 +4283,8 @@ function serializeIONode(
let stack = null;
let name = '';
if (ioNode.stack !== null) {
const fullStack = ioNode.stack;
// The stack can contain some leading internal frames for the construction of the promise that we skip.
const fullStack = stripLeadingPromiseCreationFrames(ioNode.stack);
stack = filterStackTrace(request, fullStack);
name = findCalledFunctionNameFromStackTrace(request, fullStack);
// The name can include the object that this was called on but sometimes that's
Expand Down
Loading
Loading