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

Replace console.error() with a throw in setTimeout() as last resort exception logging #13310

Merged
merged 5 commits into from
Aug 2, 2018

Conversation

gaearon
Copy link
Collaborator

@gaearon gaearon commented Aug 2, 2018

This is a curious bug: #13188.

On 16.4.1, this test fails with

Invariant Violation: Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops.

on the final update (which should have been gracefully rendered).

On master, this test fails by ignoring the last update and not rendering anything.

This test has a combination of interesting circumstances:

  • jsdom loaded after React
  • overriden console.error that throws

which might contribute to why the bug occurs. We should fix it regardless though.

Copy link
Contributor

@nhunzaker nhunzaker left a comment

Choose a reason for hiding this comment

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

Pretty interesting edge case, 👍

// It reproduces a combination of conditions that led to a problem.

if (global.window) {
throw new Error('This test must run in a Node environment.');
Copy link
Contributor

Choose a reason for hiding this comment

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

👍


// Unlike other tests, we want to enable error logging.
// Note that we'll later override console.error.
Error.prototype.suppressReactErrorLogging = false;
Copy link
Contributor

Choose a reason for hiding this comment

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

For other onlookers, I thought this meant that React extended the Error prototype. This only happens in the React test suite:

// By default React console.error()'s any errors, caught or uncaught.
// However it is annoying to assert that a warning fired each time
// we assert that there is an exception in our tests. This lets us
// opt out of extra console error reporting for most tests except
// for the few that specifically test the logging by shadowing this
// property. In real apps, it would usually not be defined at all.
Error.prototype.suppressReactErrorLogging = true;

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually looks like this raises a warning on CI:

24:1 warning Error prototype is read only, properties should not be added no-extend-native

Can you add: // eslint-disable-next-line no-extend-native?

* @jest-environment node
*/

// This is a regression test for https://github.com/facebook/react/issues/13188.
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: what do you think about dropping the period after this link so that it's easier to copy/paste from Github?

expect(didCatch).toBe(true);
ReactDOM.render(<span>Hello</span>, div);
expect(div.firstChild.textContent).toBe('Hello');
});
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think about using expect.assertions(2), instead of assigning a didCatch variable? Or expect().toThrow, like:

expect(() => {
  ReactDOM.render(<Bad />, div);
  ReactDOM.render(<Bad />, div);
}).toThrow('Maximum update depth exceeded') // maybe this needs to be a regex?

@gaearon gaearon changed the title Add a regression test for #13188 Replace console.error() with a throw in setTimeout() as last resort exception logging Aug 2, 2018
@gaearon gaearon requested a review from acdlite August 2, 2018 15:30
@gaearon
Copy link
Collaborator Author

gaearon commented Aug 2, 2018

I pushed a second commit that changes the last resort reporting strategy from console.error to throwing in a setTimeout. This way we guarantee that:

  • Error will not be silenced if console.error is broken for some reason
  • Error will not break the reconciler if console.error throws

@pull-bot
Copy link

pull-bot commented Aug 2, 2018

ReactDOM: size: 🔺+0.3%, gzip: 🔺+0.3%

Details of bundled changes.

Comparing: b3b80a4...f703123

react-dom

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-dom.development.js 0.0% +0.1% 641.5 KB 641.77 KB 150.54 KB 150.67 KB UMD_DEV
react-dom.production.min.js 🔺+0.3% 🔺+0.3% 95.9 KB 96.17 KB 31.08 KB 31.18 KB UMD_PROD
react-dom.development.js 0.0% +0.1% 637.64 KB 637.91 KB 149.37 KB 149.49 KB NODE_DEV
react-dom.production.min.js 🔺+0.3% 🔺+0.4% 95.89 KB 96.16 KB 30.61 KB 30.72 KB NODE_PROD
ReactDOM-dev.js 0.0% +0.1% 644.75 KB 645.01 KB 147.7 KB 147.81 KB FB_WWW_DEV
ReactDOM-prod.js 0.0% 0.0% 277.69 KB 277.82 KB 52.1 KB 52.13 KB FB_WWW_PROD
react-dom.profiling.min.js +0.3% +0.3% 97.02 KB 97.29 KB 31.01 KB 31.11 KB NODE_PROFILING
ReactDOM-profiling.js 0.0% +0.1% 280.98 KB 281.11 KB 52.82 KB 52.85 KB FB_WWW_PROFILING

react-art

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-art.development.js +0.1% +0.1% 434.31 KB 434.58 KB 98.29 KB 98.41 KB UMD_DEV
react-art.production.min.js 🔺+0.3% 🔺+0.4% 84.9 KB 85.17 KB 26.31 KB 26.42 KB UMD_PROD
react-art.development.js +0.1% +0.2% 366.83 KB 367.09 KB 81.22 KB 81.34 KB NODE_DEV
react-art.production.min.js 🔺+0.5% 🔺+0.7% 49.89 KB 50.16 KB 15.69 KB 15.79 KB NODE_PROD
ReactART-dev.js +0.1% +0.2% 357.16 KB 357.43 KB 76.09 KB 76.2 KB FB_WWW_DEV
ReactART-prod.js 🔺+0.1% 🔺+0.1% 152.82 KB 152.95 KB 26.34 KB 26.36 KB FB_WWW_PROD

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.2% 362.72 KB 362.99 KB 79.62 KB 79.75 KB UMD_DEV
react-test-renderer.production.min.js 🔺+0.6% 🔺+0.7% 48.76 KB 49.03 KB 15.05 KB 15.16 KB UMD_PROD
react-test-renderer.development.js +0.1% +0.2% 358.85 KB 359.12 KB 78.65 KB 78.77 KB NODE_DEV
react-test-renderer.production.min.js 🔺+0.6% 🔺+0.7% 48.47 KB 48.74 KB 14.9 KB 15.01 KB NODE_PROD
ReactTestRenderer-dev.js +0.1% +0.1% 363.82 KB 364.09 KB 77.57 KB 77.69 KB FB_WWW_DEV

react-reconciler

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-reconciler.development.js +0.1% +0.2% 347.84 KB 348.11 KB 75.25 KB 75.38 KB NODE_DEV
react-reconciler.production.min.js 🔺+0.6% 🔺+0.7% 47.44 KB 47.71 KB 14.37 KB 14.47 KB NODE_PROD
react-reconciler-persistent.development.js +0.1% +0.2% 346.46 KB 346.73 KB 74.7 KB 74.82 KB NODE_DEV
react-reconciler-persistent.production.min.js 🔺+0.6% 🔺+0.7% 47.45 KB 47.72 KB 14.37 KB 14.47 KB NODE_PROD

react-native-renderer

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
ReactNativeRenderer-dev.js +0.1% +0.1% 481.99 KB 482.26 KB 106.43 KB 106.55 KB RN_FB_DEV
ReactNativeRenderer-prod.js 🔺+0.2% 🔺+0.3% 213.76 KB 214.2 KB 37.44 KB 37.54 KB RN_FB_PROD
ReactNativeRenderer-dev.js +0.1% +0.1% 481.72 KB 481.99 KB 106.38 KB 106.49 KB RN_OSS_DEV
ReactNativeRenderer-prod.js 🔺+0.2% 🔺+0.3% 203.8 KB 204.24 KB 35.82 KB 35.92 KB RN_OSS_PROD
ReactFabric-dev.js +0.1% +0.1% 472.2 KB 472.47 KB 104 KB 104.11 KB RN_FB_DEV
ReactFabric-prod.js 🔺+0.2% 🔺+0.2% 194.92 KB 195.36 KB 34.27 KB 34.36 KB RN_FB_PROD
ReactFabric-dev.js +0.1% +0.1% 472.24 KB 472.5 KB 104.01 KB 104.13 KB RN_OSS_DEV
ReactFabric-prod.js 🔺+0.2% 🔺+0.2% 194.95 KB 195.39 KB 34.29 KB 34.37 KB RN_OSS_PROD
ReactNativeRenderer-profiling.js +0.2% +0.3% 207.31 KB 207.75 KB 36.51 KB 36.62 KB RN_OSS_PROFILING
ReactFabric-profiling.js +0.2% +0.3% 197.97 KB 198.41 KB 34.91 KB 35 KB RN_OSS_PROFILING
ReactNativeRenderer-profiling.js +0.2% +0.3% 217.23 KB 217.67 KB 38.13 KB 38.24 KB RN_FB_PROFILING
ReactFabric-profiling.js +0.2% +0.3% 197.93 KB 198.37 KB 34.89 KB 34.98 KB RN_FB_PROFILING

Generated by 🚫 dangerJS

// https://github.com/facebook/react/issues/13188
setTimeout(() => {
throw e;
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Since you set suppressReactErrorLogging to false, will this execute after the test above? Should you use jest.useFakeTimers() and force it to run with something like jest.runAllTimers()?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I didn't really intend to test this part although I guess I could. I only wanted to ensure that we don't blow up the reconciler by rethrowing.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added a check.

Copy link
Contributor

@nhunzaker nhunzaker Aug 2, 2018

Choose a reason for hiding this comment

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

Cool, my primary concern was just confusion in test reporting if the error showed up randomly in the test suite because Node or Jest saw an uncaught error.

No need to chase this down too much.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah cool. Posted too late! I didn't realize that fake timers were already setup, so this wouldn't have run anyway.

Copy link
Collaborator

@acdlite acdlite left a comment

Choose a reason for hiding this comment

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

Seems fine

@gaearon gaearon merged commit 46d5afc into facebook:master Aug 2, 2018
@gaearon gaearon deleted the nest-up-bug branch August 2, 2018 17:17
// Rethrow it from a clean stack because this function is assumed to never throw.
// We can't safely call console.error() here because it could *also* throw if overriden.
// https://github.com/facebook/react/issues/13188
setTimeout(() => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I guess setTimeout could also be buggy. Let's not think about this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants