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

Re-throw errors thrown by the renderer at the root in the complete phase #18029

Merged
merged 2 commits into from
Feb 18, 2020

Conversation

acdlite
Copy link
Collaborator

@acdlite acdlite commented Feb 12, 2020

React treats errors thrown at the root as a fatal because there's no parent component that can capture it. (This is distinct from an "uncaught error" that isn't wrapped in an error boundary, because in that case we can fall back to deleting the whole tree – not great, but at least the error is contained to a single root, and React is left in a consistent state.)

It turns out we didn't have a test case for this path. The only way it can happen is if the renderer's host config throws. We had similar test cases for host components, but none for the host root.

This adds a new test case and fixes a bug where React would keep retrying the root because the workInProgress pointer was not advanced to the next fiber. (Which in this case is null, since it's the root.)

We could consider in the future trying to gracefully exit from certain types of root errors without leaving React in an inconsistent state. For example, we should be able to gracefully exit from errors thrown in the begin phase. For now, I'm treating it like an internal invariant and immediately exiting.

React treats errors thrown at the root as a fatal because there's no
parent component that can capture it. (This is distinct from an
"uncaught error" that isn't wrapped in an error boundary, because in
that case we can fall back to deleting the whole tree -- not great, but
at least the error is contained to a single root, and React is left in a
consistent state.)

It turns out we didn't have a test case for this path. The only way it
can happen is if the renderer's host config throws. We had similar test
cases for host components, but none for the host root.

This adds a new test case and fixes a bug where React would keep
retrying the root because the `workInProgress` pointer was not advanced
to the next fiber. (Which in this case is `null`, since it's the root.)

We could consider in the future trying to gracefully exit from certain
types of root errors without leaving React in an inconsistent state. For
example, we should be able to gracefully exit from errors thrown in the
begin phase. For now, I'm treating it like an internal invariant and
immediately exiting.
@facebook-github-bot facebook-github-bot added CLA Signed React Core Team Opened by a member of the React Core Team labels Feb 12, 2020
@acdlite acdlite changed the title Re-throw errors thrown by the renderer at the root Re-throw errors thrown by the renderer at the root in the complete phase Feb 12, 2020
@sizebot
Copy link

sizebot commented Feb 12, 2020

Details of bundled changes.

Comparing: 58b8797...348302e

react-art

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-art.development.js +0.1% +0.1% 611.85 KB 612.3 KB 129.09 KB 129.22 KB NODE_DEV
react-art.production.min.js 0.0% -0.0% 69.98 KB 69.99 KB 20.99 KB 20.99 KB NODE_PROD
react-art.development.js +0.1% +0.1% 681.17 KB 681.63 KB 146.52 KB 146.65 KB UMD_DEV
react-art.production.min.js 0.0% -0.0% 104.98 KB 104.98 KB 31.84 KB 31.84 KB UMD_PROD

react-native-renderer

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
ReactFabric-prod.js 0.0% -0.0% 267.07 KB 267.09 KB 45.74 KB 45.74 KB RN_OSS_PROD
ReactNativeRenderer-dev.js +0.1% +0.1% 753.13 KB 753.58 KB 158.31 KB 158.44 KB RN_OSS_DEV
ReactFabric-profiling.js 0.0% 0.0% 278.25 KB 278.27 KB 47.88 KB 47.88 KB RN_OSS_PROFILING
ReactFabric-dev.js +0.1% +0.1% 743.74 KB 744.2 KB 156.1 KB 156.23 KB RN_OSS_DEV
ReactNativeRenderer-prod.js 0.0% 0.0% 274.76 KB 274.78 KB 47.03 KB 47.03 KB RN_OSS_PROD
ReactNativeRenderer-profiling.js 0.0% -0.0% 285.98 KB 286 KB 49.21 KB 49.21 KB RN_OSS_PROFILING

react-dom

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-dom-test-utils.development.js 0.0% -0.0% 53.65 KB 53.65 KB 15.27 KB 15.27 KB NODE_DEV
react-dom.production.min.js 0.0% -0.0% 116.08 KB 116.09 KB 37.28 KB 37.28 KB UMD_PROD
react-dom-testing.development.js 0.0% +0.1% 960.41 KB 960.87 KB 215.03 KB 215.16 KB NODE_DEV
react-dom.profiling.min.js 0.0% -0.0% 119.61 KB 119.61 KB 38.45 KB 38.45 KB UMD_PROFILING
react-dom-testing.production.min.js 0.0% -0.0% 117.22 KB 117.22 KB 37.1 KB 37.09 KB NODE_PROD
react-dom.development.js 0.0% +0.1% 962.99 KB 963.45 KB 216.03 KB 216.15 KB NODE_DEV
react-dom-testing.profiling.min.js 0.0% -0.0% 120.9 KB 120.9 KB 38.21 KB 38.21 KB NODE_PROFILING
react-dom.production.min.js 0.0% -0.0% 116.15 KB 116.15 KB 36.61 KB 36.61 KB NODE_PROD
react-dom-unstable-fizz.browser.production.min.js 0.0% -0.1% 1.2 KB 1.2 KB 704 B 703 B UMD_PROD
react-dom.profiling.min.js 0.0% -0.0% 119.82 KB 119.82 KB 37.74 KB 37.74 KB NODE_PROFILING
react-dom-server.browser.development.js 0.0% -0.0% 139.3 KB 139.3 KB 36.94 KB 36.94 KB UMD_DEV
react-dom-unstable-fizz.browser.development.js 0.0% -0.1% 3.7 KB 3.7 KB 1.5 KB 1.5 KB NODE_DEV
react-dom-unstable-fizz.browser.production.min.js 0.0% -0.2% 1.04 KB 1.04 KB 635 B 634 B NODE_PROD
react-dom-server.browser.development.js 0.0% -0.0% 135.23 KB 135.23 KB 35.92 KB 35.91 KB NODE_DEV
react-dom-test-utils.development.js 0.0% -0.0% 55.38 KB 55.38 KB 15.59 KB 15.59 KB UMD_DEV
react-dom-test-utils.production.min.js 0.0% 🔺+0.1% 10.97 KB 10.97 KB 4.09 KB 4.1 KB NODE_PROD
react-dom-unstable-native-dependencies.development.js 0.0% -0.0% 60.92 KB 60.92 KB 16.07 KB 16.07 KB UMD_DEV
react-dom-unstable-fizz.node.production.min.js 0.0% -0.1% 1.2 KB 1.2 KB 690 B 689 B NODE_PROD
react-dom-unstable-native-dependencies.production.min.js 0.0% -0.0% 10.24 KB 10.24 KB 3.46 KB 3.46 KB UMD_PROD
react-dom-server.node.development.js 0.0% -0.0% 136.34 KB 136.34 KB 36.14 KB 36.14 KB NODE_DEV
react-dom-testing.development.js 0.0% +0.1% 966.33 KB 966.78 KB 216.72 KB 216.85 KB UMD_DEV
react-dom-testing.production.min.js 0.0% -0.0% 117.12 KB 117.12 KB 37.76 KB 37.75 KB UMD_PROD
react-dom-unstable-native-dependencies.development.js 0.0% -0.0% 60.62 KB 60.62 KB 15.99 KB 15.99 KB NODE_DEV
react-dom.development.js 0.0% +0.1% 968.91 KB 969.37 KB 217.69 KB 217.82 KB UMD_DEV
react-dom-testing.profiling.min.js 0.0% -0.0% 120.64 KB 120.65 KB 38.91 KB 38.91 KB UMD_PROFILING
react-dom-unstable-native-dependencies.production.min.js 0.0% -0.0% 9.98 KB 9.98 KB 3.37 KB 3.37 KB NODE_PROD

react-reconciler

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-reconciler-reflection.development.js 0.0% -0.0% 19.93 KB 19.93 KB 6.56 KB 6.56 KB NODE_DEV
react-reconciler-persistent.development.js +0.1% +0.1% 614.75 KB 615.21 KB 128.34 KB 128.47 KB NODE_DEV
react-reconciler-persistent.production.min.js 0.0% 0.0% 73.75 KB 73.75 KB 21.78 KB 21.78 KB NODE_PROD
react-reconciler.development.js +0.1% +0.1% 617.97 KB 618.43 KB 129.68 KB 129.81 KB NODE_DEV
react-reconciler.production.min.js 0.0% -0.0% 73.73 KB 73.74 KB 21.77 KB 21.77 KB NODE_PROD

react-test-renderer

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-test-renderer-shallow.development.js 0.0% 0.0% 37.88 KB 37.88 KB 9.83 KB 9.83 KB UMD_DEV
react-test-renderer-shallow.production.min.js 0.0% -0.0% 11.64 KB 11.64 KB 3.59 KB 3.59 KB UMD_PROD
react-test-renderer-shallow.development.js 0.0% -0.0% 32.42 KB 32.42 KB 8.52 KB 8.52 KB NODE_DEV
react-test-renderer.development.js +0.1% +0.1% 625.54 KB 626 KB 131.85 KB 131.99 KB UMD_DEV
react-test-renderer.production.min.js 0.0% 0.0% 72.13 KB 72.13 KB 21.94 KB 21.94 KB UMD_PROD
react-test-renderer.development.js +0.1% +0.1% 620.81 KB 621.27 KB 130.67 KB 130.8 KB NODE_DEV
react-test-renderer.production.min.js 0.0% -0.0% 71.91 KB 71.91 KB 21.61 KB 21.61 KB NODE_PROD

ReactDOM: size: 0.0%, gzip: -0.0%

Size changes (stable)

Generated by 🚫 dangerJS against 348302e

@sizebot
Copy link

sizebot commented Feb 12, 2020

Details of bundled changes.

Comparing: 58b8797...348302e

react-native-renderer

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
ReactFabric-dev.js +0.1% +0.1% 743.75 KB 744.21 KB 156.11 KB 156.24 KB RN_OSS_DEV
ReactNativeRenderer-dev.js +0.1% +0.1% 753.14 KB 753.59 KB 158.31 KB 158.44 KB RN_OSS_DEV
ReactNativeRenderer-prod.js 0.0% 0.0% 274.77 KB 274.79 KB 47.04 KB 47.04 KB RN_OSS_PROD
ReactFabric-dev.js +0.1% +0.1% 743.94 KB 744.39 KB 156.19 KB 156.32 KB RN_FB_DEV
ReactNativeRenderer-profiling.js 0.0% -0.0% 285.99 KB 286.01 KB 49.22 KB 49.22 KB RN_OSS_PROFILING
ReactFabric-prod.js 0.0% -0.0% 267.43 KB 267.45 KB 45.82 KB 45.82 KB RN_FB_PROD
ReactFabric-profiling.js 0.0% 0.0% 278.6 KB 278.62 KB 47.95 KB 47.95 KB RN_FB_PROFILING
ReactNativeRenderer-dev.js +0.1% +0.1% 753.31 KB 753.77 KB 158.41 KB 158.54 KB RN_FB_DEV
ReactFabric-prod.js 0.0% -0.0% 267.08 KB 267.1 KB 45.75 KB 45.75 KB RN_OSS_PROD
ReactNativeRenderer-prod.js 0.0% 0.0% 275.16 KB 275.18 KB 47.1 KB 47.1 KB RN_FB_PROD
ReactFabric-profiling.js 0.0% 0.0% 278.26 KB 278.28 KB 47.88 KB 47.88 KB RN_OSS_PROFILING
ReactNativeRenderer-profiling.js 0.0% -0.0% 286.38 KB 286.4 KB 49.29 KB 49.29 KB RN_FB_PROFILING

react-test-renderer

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-test-renderer.development.js +0.1% +0.1% 625.57 KB 626.03 KB 131.87 KB 132 KB UMD_DEV
react-test-renderer.production.min.js 0.0% 0.0% 72.15 KB 72.16 KB 21.96 KB 21.96 KB UMD_PROD
react-test-renderer.development.js +0.1% +0.1% 620.83 KB 621.29 KB 130.68 KB 130.82 KB NODE_DEV
react-test-renderer.production.min.js 0.0% -0.0% 71.93 KB 71.93 KB 21.63 KB 21.63 KB NODE_PROD
ReactTestRenderer-dev.js +0.1% +0.1% 636.27 KB 636.72 KB 131.49 KB 131.63 KB FB_WWW_DEV
react-test-renderer-shallow.development.js 0.0% 0.0% 37.89 KB 37.89 KB 9.84 KB 9.84 KB UMD_DEV
react-test-renderer-shallow.production.min.js 0.0% -0.0% 11.66 KB 11.66 KB 3.6 KB 3.6 KB UMD_PROD
react-test-renderer-shallow.development.js 0.0% -0.0% 32.43 KB 32.43 KB 8.53 KB 8.53 KB NODE_DEV

react-reconciler

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-reconciler-persistent.development.js +0.1% +0.1% 614.76 KB 615.22 KB 128.35 KB 128.47 KB NODE_DEV
react-reconciler-reflection.development.js 0.0% -0.0% 19.95 KB 19.95 KB 6.56 KB 6.56 KB NODE_DEV
react-reconciler-persistent.production.min.js 0.0% 0.0% 73.76 KB 73.76 KB 21.79 KB 21.79 KB NODE_PROD
react-reconciler-reflection.production.min.js 0.0% -0.1% 2.86 KB 2.86 KB 1.24 KB 1.24 KB NODE_PROD
react-reconciler.development.js +0.1% +0.1% 617.98 KB 618.44 KB 129.68 KB 129.81 KB NODE_DEV
react-reconciler.production.min.js 0.0% -0.0% 76.51 KB 76.52 KB 22.44 KB 22.44 KB NODE_PROD

react-dom

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-dom.profiling.min.js 0.0% 0.0% 123.87 KB 123.87 KB 38.77 KB 38.77 KB NODE_PROFILING
react-dom-unstable-fizz.browser.production.min.js 0.0% -0.1% 1.21 KB 1.21 KB 712 B 711 B UMD_PROD
react-dom-test-utils.development.js 0.0% -0.0% 55.39 KB 55.39 KB 15.6 KB 15.59 KB UMD_DEV
react-dom-testing.profiling.min.js 0.0% -0.0% 121.42 KB 121.43 KB 38.35 KB 38.35 KB NODE_PROFILING
ReactDOMTesting-prod.js 0.0% -0.0% 341.24 KB 341.26 KB 62.28 KB 62.28 KB FB_WWW_PROD
react-dom-server.browser.development.js 0.0% -0.0% 139.32 KB 139.32 KB 36.94 KB 36.94 KB UMD_DEV
ReactDOMTesting-profiling.js 0.0% 0.0% 352.04 KB 352.06 KB 64.23 KB 64.23 KB FB_WWW_PROFILING
react-dom-server.browser.development.js 0.0% -0.0% 135.26 KB 135.26 KB 35.92 KB 35.92 KB NODE_DEV
react-dom-test-utils.production.min.js 0.0% 🔺+0.1% 10.99 KB 10.99 KB 4.1 KB 4.1 KB NODE_PROD
react-dom-server.browser.production.min.js 0.0% -0.0% 20.39 KB 20.39 KB 7.47 KB 7.47 KB NODE_PROD
react-dom.development.js 0.0% +0.1% 968.93 KB 969.39 KB 217.71 KB 217.84 KB UMD_DEV
react-dom-unstable-native-dependencies.development.js 0.0% -0.0% 60.93 KB 60.93 KB 16.08 KB 16.08 KB UMD_DEV
react-dom.production.min.js 0.0% 0.0% 120 KB 120 KB 38.39 KB 38.39 KB UMD_PROD
react-dom-unstable-native-dependencies.production.min.js 0.0% -0.0% 10.25 KB 10.25 KB 3.47 KB 3.47 KB UMD_PROD
react-dom.profiling.min.js 0.0% -0.0% 123.63 KB 123.63 KB 39.59 KB 39.59 KB UMD_PROFILING
react-dom.development.js 0.0% +0.1% 963.01 KB 963.47 KB 216.04 KB 216.17 KB NODE_DEV
react-dom-unstable-native-dependencies.development.js 0.0% -0.0% 60.63 KB 60.63 KB 16 KB 16 KB NODE_DEV
react-dom.production.min.js 0.0% -0.0% 120.09 KB 120.09 KB 37.65 KB 37.65 KB NODE_PROD
react-dom-unstable-native-dependencies.production.min.js 0.0% -0.0% 9.99 KB 9.99 KB 3.38 KB 3.38 KB NODE_PROD
ReactDOMServer-dev.js 0.0% -0.0% 140.41 KB 140.41 KB 35.54 KB 35.54 KB FB_WWW_DEV
ReactDOM-dev.js 0.0% +0.1% 990.33 KB 990.79 KB 218.5 KB 218.63 KB FB_WWW_DEV
react-dom-unstable-fizz.browser.production.min.js 0.0% -0.2% 1.05 KB 1.05 KB 643 B 642 B NODE_PROD
ReactDOM-prod.js 0.0% 0.0% 395.6 KB 395.62 KB 71.89 KB 71.89 KB FB_WWW_PROD
react-dom-testing.development.js 0.0% +0.1% 966.35 KB 966.81 KB 216.73 KB 216.86 KB UMD_DEV
ReactDOM-profiling.js 0.0% -0.0% 406.93 KB 406.95 KB 73.99 KB 73.98 KB FB_WWW_PROFILING
react-dom-testing.production.min.js 0.0% 0.0% 117.65 KB 117.65 KB 37.91 KB 37.91 KB UMD_PROD
react-dom-testing.profiling.min.js 0.0% 0.0% 121.17 KB 121.18 KB 39.04 KB 39.05 KB UMD_PROFILING
react-dom-testing.development.js 0.0% +0.1% 960.43 KB 960.89 KB 215.04 KB 215.17 KB NODE_DEV
react-dom-server.node.development.js 0.0% -0.0% 136.37 KB 136.37 KB 36.15 KB 36.15 KB NODE_DEV
react-dom-testing.production.min.js 0.0% -0.0% 117.75 KB 117.75 KB 37.24 KB 37.24 KB NODE_PROD
ReactDOMTesting-dev.js 0.0% +0.1% 989.03 KB 989.48 KB 218.09 KB 218.22 KB FB_WWW_DEV
react-dom-server.node.production.min.js 0.0% -0.0% 20.79 KB 20.79 KB 7.62 KB 7.62 KB NODE_PROD

react-art

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-art.development.js +0.1% +0.1% 681.2 KB 681.65 KB 146.52 KB 146.65 KB UMD_DEV
react-art.production.min.js 0.0% -0.0% 107.54 KB 107.54 KB 32.53 KB 32.53 KB UMD_PROD
react-art.development.js +0.1% +0.1% 611.87 KB 612.33 KB 129.1 KB 129.22 KB NODE_DEV
react-art.production.min.js 0.0% -0.0% 72.5 KB 72.5 KB 21.67 KB 21.66 KB NODE_PROD
ReactART-dev.js +0.1% +0.1% 624.68 KB 625.13 KB 128.99 KB 129.13 KB FB_WWW_DEV
ReactART-prod.js 0.0% -0.0% 240.07 KB 240.08 KB 40.4 KB 40.4 KB FB_WWW_PROD

ReactDOM: size: 0.0%, gzip: -0.1%

Size changes (experimental)

Generated by 🚫 dangerJS against 348302e

@codesandbox-ci
Copy link

codesandbox-ci bot commented Feb 12, 2020

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@acdlite acdlite force-pushed the rootfatalerror branch 2 times, most recently from a5a0366 to 2299d1d Compare February 12, 2020 21:04
@sebmarkbage
Copy link
Collaborator

sebmarkbage commented Feb 12, 2020

Your description doesn’t describe what this fix does in the code?

@@ -1285,6 +1285,7 @@ function handleError(root, thrownValue) {
// boundary.
workInProgressRootExitStatus = RootFatalErrored;
workInProgressRootFatalError = thrownValue;
workInProgress = null;
Copy link
Collaborator

Choose a reason for hiding this comment

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

What is happening here? Why does this work?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This adds a new test case and fixes a bug where React would keep retrying the root because the workInProgress pointer was not advanced to the next fiber. (Which in this case is null, since it's the root.)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In other words, it falls into an infinite loop because workInProgress is stuck on the the host root fiber forever. Usually workInProgress gets advanced during either beingWork or completeUnitOfWork but since it keeps erroring that never happens.

Copy link
Collaborator

@sebmarkbage sebmarkbage Feb 13, 2020

Choose a reason for hiding this comment

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

Ah. Subtle. A comment saying that this represents moving to the next sibling would be nice but not required.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ok I'll add one! I think there's a potential follow up to also call unwindWork here to pop the contexts. I originally didn't bother because it's not impossible even that call will throw, but if layout errors are common enough it might be worth doing more to prevent corrupted internal state.

@acdlite acdlite merged commit 4d9f850 into facebook:master Feb 18, 2020
elicwhite pushed a commit to elicwhite/react that referenced this pull request Feb 26, 2020
…ase (facebook#18029)

* Re-throw errors thrown by the renderer at the root

React treats errors thrown at the root as a fatal because there's no
parent component that can capture it. (This is distinct from an
"uncaught error" that isn't wrapped in an error boundary, because in
that case we can fall back to deleting the whole tree -- not great, but
at least the error is contained to a single root, and React is left in a
consistent state.)

It turns out we didn't have a test case for this path. The only way it
can happen is if the renderer's host config throws. We had similar test
cases for host components, but none for the host root.

This adds a new test case and fixes a bug where React would keep
retrying the root because the `workInProgress` pointer was not advanced
to the next fiber. (Which in this case is `null`, since it's the root.)

We could consider in the future trying to gracefully exit from certain
types of root errors without leaving React in an inconsistent state. For
example, we should be able to gracefully exit from errors thrown in the
begin phase. For now, I'm treating it like an internal invariant and
immediately exiting.

* Add comment
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.

5 participants