Skip to content
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

[Flight] Implement captureStackTrace and owner stacks on the Server #30197

Merged
merged 2 commits into from
Jul 4, 2024

Conversation

sebmarkbage
Copy link
Collaborator

Wire up owner stacks in Flight to the shared internals. This exposes it to captureOwnerStack().

In this case we install it permanently as we only allow one RSC renderer which then supports async contexts. Same thing we do for owner.

This also ends up adding it to errors logged by React through consoleWithStackDev. The plan is to eventually remove that but this is inline with what we do in Fizz and Fiber already.

However, at the same time we've instrumented the console so we need to strip them back out before sending to the client. This lets the client control whether to add the stack back in or allowing console.createTask to control it.

This is another reason we shouldn't append them from React but for now we hack it by removing them after the fact.

Copy link

vercel bot commented Jul 2, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
react-compiler-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 3, 2024 9:00pm

@react-sizebot
Copy link

react-sizebot commented Jul 2, 2024

Comparing: 15ca8b6...b0becf2

Critical size changes

Includes critical production bundles, as well as any change greater than 2%:

Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable/react-dom/cjs/react-dom.production.js = 6.68 kB 6.68 kB = 1.83 kB 1.83 kB
oss-stable/react-dom/cjs/react-dom-client.production.js = 497.99 kB 497.99 kB = 89.27 kB 89.27 kB
oss-experimental/react-dom/cjs/react-dom.production.js = 6.69 kB 6.69 kB = 1.83 kB 1.83 kB
oss-experimental/react-dom/cjs/react-dom-client.production.js = 502.81 kB 502.81 kB = 89.98 kB 89.97 kB
facebook-www/ReactDOM-prod.classic.js = 597.08 kB 597.08 kB = 105.33 kB 105.33 kB
facebook-www/ReactDOM-prod.modern.js = 571.42 kB 571.42 kB = 101.27 kB 101.27 kB
test_utils/ReactAllWarnings.js Deleted 62.88 kB 0.00 kB Deleted 15.69 kB 0.00 kB

Significant size changes

Includes any change greater than 0.2%:

Expand to show
Name +/- Base Current +/- gzip Base gzip Current gzip
oss-experimental/react-server/cjs/react-server-flight.development.js +1.85% 92.56 kB 94.27 kB +2.51% 17.21 kB 17.64 kB
oss-experimental/react-server-dom-esm/cjs/react-server-dom-esm-server.node.development.js +1.29% 132.59 kB 134.31 kB +1.71% 24.82 kB 25.24 kB
oss-experimental/react-server-dom-turbopack/cjs/react-server-dom-turbopack-server.browser.development.js +1.26% 136.23 kB 137.94 kB +1.67% 25.39 kB 25.81 kB
oss-experimental/react-server-dom-webpack/cjs/react-server-dom-webpack-server.browser.development.js +1.25% 136.85 kB 138.57 kB +1.66% 25.56 kB 25.98 kB
oss-experimental/react-server-dom-turbopack/cjs/react-server-dom-turbopack-server.edge.development.js +1.25% 137.68 kB 139.39 kB +1.67% 25.64 kB 26.07 kB
oss-experimental/react-server-dom-webpack/cjs/react-server-dom-webpack-server.edge.development.js +1.24% 137.83 kB 139.54 kB +1.68% 25.70 kB 26.13 kB
oss-experimental/react-server-dom-turbopack/cjs/react-server-dom-turbopack-server.node.unbundled.development.js +1.23% 138.96 kB 140.67 kB +1.62% 25.79 kB 26.21 kB
oss-experimental/react-server-dom-webpack/cjs/react-server-dom-webpack-server.node.unbundled.development.js +1.23% 139.12 kB 140.83 kB +1.63% 25.85 kB 26.27 kB
oss-experimental/react-server-dom-turbopack/cjs/react-server-dom-turbopack-server.node.development.js +1.22% 140.06 kB 141.78 kB +1.61% 26.05 kB 26.47 kB
oss-experimental/react-server-dom-webpack/cjs/react-server-dom-webpack-server.node.development.js +1.22% 140.21 kB 141.93 kB +1.61% 26.10 kB 26.52 kB
oss-experimental/react-html/cjs/react-html.react-server.development.js +0.28% 487.54 kB 488.91 kB +0.28% 88.07 kB 88.31 kB
oss-stable-rc/react-server/cjs/react-server-flight.development.js +0.24% 80.82 kB 81.02 kB +0.25% 15.03 kB 15.07 kB
oss-stable-semver/react-server/cjs/react-server-flight.development.js +0.24% 80.82 kB 81.02 kB +0.25% 15.03 kB 15.07 kB
oss-stable/react-server/cjs/react-server-flight.development.js +0.24% 80.82 kB 81.02 kB +0.25% 15.03 kB 15.07 kB
test_utils/ReactAllWarnings.js Deleted 62.88 kB 0.00 kB Deleted 15.69 kB 0.00 kB

Generated by 🚫 dangerJS against b0becf2

assertConsoleErrorDev([
'Each child in a list should have a unique "key" prop.' +
' See https://react.dev/link/warning-keys for more information.\n' +
' in Foo (at **)',
Copy link
Collaborator Author

@sebmarkbage sebmarkbage Jul 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rickhanlonii This test passes but "Foo" is not actually the owner stack trace because the stack starts from where the element was created and that's inside Bar.

#30204

This exposes it to captureOwnerStack().

In this case we install it permanently as we only allow one RSC renderer
which then supports async contexts.

It also exposes it to component stack addendums that React adds to its own
console.errors. At least for now.
Since getCurrentStack is now implemented, React appends component stacks
to its own logs. However, at the same time we've instrumented the console
so we need to strip them back out before sending to the client.

This is another reason we shouldn't append them from React.
@sebmarkbage sebmarkbage merged commit 0b5835a into facebook:main Jul 4, 2024
139 checks passed
sebmarkbage added a commit that referenced this pull request Jul 4, 2024
…ing onError/onPostpone (#30206)

Stacked on #30197.

This is similar to #30182 and #21610 in Fizz.

Track the current owner/stack/task on the task. This tracks it for
attribution when serializing child properties.

This lets us provide the right owner and createTask when we
console.error from inside Flight itself. This also affects the way we
print those logs on the client since we need the owner and stack. Now
console.errors that originate on the server gets the right stack on the
client:

<img width="760" alt="Screenshot 2024-07-03 at 6 03 13 PM"
src="https://github.com/facebook/react/assets/63648/913300f8-f364-4e66-a19d-362e8d776c64">

Unfortunately, because we don't track the stack we never pop it so it'll
keep tracking for serializing sibling properties. We rely on "children"
typically being the last property in the common case anyway. However,
this can lead to wrong attribution in some cases where the invalid
property is a next property (without a wrapping element) and there's a
previous element that doesn't. E.g. `<ClientComponent title={<div />}
invalid={nonSerializable} />` would use the div as the attribution
instead of ClientComponent.

I also wrap all of our own console.error, onError and onPostpone in the
context of the parent component. It's annoying to have to remember to do
this though.

We could always wrap the whole rendering in such as context but it would
add more overhead since this rarely actually happens. It might make
sense to track the whole current task instead to lower the overhead.
That's what we do in Fizz. We'd still have to remember to restore the
debug task though. I realize now Fizz doesn't do that neither so the
debug task isn't wrapping the console.errors that Fizz itself logs.
There's something off about that Flight and Fizz implementations don't
perfectly align.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed React Core Team Opened by a member of the React Core Team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants