Skip to content

Warn about async infinite useEffect loop - #15180

Merged
gaearon merged 2 commits into
react:masterfrom
gaearon:usefx-wa
Mar 22, 2019
Merged

Warn about async infinite useEffect loop#15180
gaearon merged 2 commits into
react:masterfrom
gaearon:usefx-wa

Conversation

@gaearon

@gaearongaearon commented Mar 21, 2019

Copy link
Copy Markdown
Collaborator

This surfaces problems like #14920 (comment) where the same root keeps scheduling updates inside useEffect, and never manages to "rest". It only catches the "synchronous" case (setState directly in useEffect).

Screen Shot 2019-03-21 at 4 54 45 PM

I've made it DEV-only because a crash would be a breaking change. The heuristic might also not be perfect yet and I'd like to give it some time to see if there are edge false positives.

while (error === null) {
Scheduler.unstable_flushNumberOfYields(1);
await Promise.resolve();
}

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

This is my shoddy attempt to faithfully emulate deferreduseEffect case. (I don't want to flush it all synchronously.)

idontknowwhatimdoing.jpg

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Try this

ReactDOM.render(<App/>,container,()=>Scheduler.yieldValue('Did commit'));expect(Scheduler).toFlushAndYieldThrough(['Did commit']);// Synchronous effect has run, but not passive effects.// Now flush the effects:Scheduler.flushAll();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Actually I forgot this is sync mode. Might be easier to use a boolean since the effect runs an arbitrary number of times before erroring.

ReactDOM.render(<App/>,container);expect(container.textContent).toEqual(whatever);// Synchronous effect has run, but not passive effects.expect(didFlushPassiveEffect).toBe(false);// Now flush the effects:Scheduler.flushAll();

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

I wanted to simulate a real deferred loop between them to make sure the warning still fires. Not sure if it matters.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

To do that you need an assertion in between the synchronous commit phase and the effect phase. As is, your test would pass even if the passive effects were totally sync.

}

if (__DEV__) {
if (rootWithPendingPassiveEffects === root) {

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Only keep incrementing if flushing root's deferred effects schedules the same root again.

@sizebot

sizebot commented Mar 21, 2019

Copy link
Copy Markdown
Details of bundled changes.

Comparing: 3151813...9cdb8c4

react-dom

FileFilesize DiffGzip DiffPrev SizeCurrent SizePrev GzipCurrent GzipENV
react-dom.development.js+0.1%+0.1%803.96 KB804.91 KB182.29 KB182.54 KBUMD_DEV
react-dom.profiling.min.js0.0%-0.0%108.07 KB108.07 KB34.62 KB34.61 KBUMD_PROFILING
react-dom.development.js+0.1%+0.1%798.35 KB799.3 KB180.68 KB180.93 KBNODE_DEV
react-dom.production.min.js0.0%-0.0%105.07 KB105.07 KB33.46 KB33.46 KBNODE_PROD
react-dom.profiling.min.js0.0%-0.0%108.17 KB108.17 KB34.08 KB34.07 KBNODE_PROFILING
ReactDOM-dev.js+0.1%+0.2%822.74 KB823.76 KB182.25 KB182.53 KBFB_WWW_DEV
react-dom-unstable-fire.development.js+0.1%+0.1%804.31 KB805.26 KB182.43 KB182.69 KBUMD_DEV
react-dom-unstable-fire.production.min.js0.0%-0.0%105.11 KB105.11 KB33.99 KB33.99 KBUMD_PROD
react-dom-unstable-fire.development.js+0.1%+0.1%798.69 KB799.65 KB180.81 KB181.08 KBNODE_DEV
react-dom-unstable-fire.production.min.js0.0%-0.0%105.08 KB105.08 KB33.47 KB33.47 KBNODE_PROD
react-dom-unstable-fire.profiling.min.js0.0%-0.0%108.18 KB108.18 KB34.08 KB34.08 KBNODE_PROFILING
ReactFire-dev.js+0.1%+0.2%821.95 KB822.97 KB182.11 KB182.39 KBFB_WWW_DEV
ReactFire-profiling.js0.0%0.0%324.76 KB324.76 KB59.4 KB59.4 KBFB_WWW_PROFILING
react-dom-test-utils.development.js0.0%-0.0%47.49 KB47.49 KB12.98 KB12.98 KBUMD_DEV
react-dom-test-utils.production.min.js0.0%-0.1%9.73 KB9.73 KB3.59 KB3.58 KBNODE_PROD
react-dom-server.browser.development.js0.0%-0.0%133.68 KB133.68 KB35.37 KB35.37 KBUMD_DEV
react-dom-server.browser.production.min.js0.0%-0.0%18.93 KB18.93 KB7.14 KB7.14 KBUMD_PROD
react-dom-server.browser.development.js0.0%-0.0%129.81 KB129.81 KB34.43 KB34.43 KBNODE_DEV
react-dom-server.browser.production.min.js0.0%-0.0%18.86 KB18.86 KB7.14 KB7.14 KBNODE_PROD
ReactDOMServer-dev.js0.0%-0.0%132.15 KB132.15 KB34.19 KB34.19 KBFB_WWW_DEV
ReactDOMServer-prod.js0.0%-0.0%45.67 KB45.67 KB10.57 KB10.56 KBFB_WWW_PROD
react-dom-server.node.development.js0.0%-0.0%131.75 KB131.75 KB34.97 KB34.97 KBNODE_DEV
react-dom-server.node.production.min.js0.0%-0.0%19.72 KB19.72 KB7.44 KB7.44 KBNODE_PROD
react-dom-unstable-fizz.browser.development.js0.0%-0.1%3.66 KB3.66 KB1.45 KB1.45 KBUMD_DEV
react-dom-unstable-fizz.browser.development.js0.0%-0.1%3.49 KB3.49 KB1.41 KB1.41 KBNODE_DEV
react-dom-unstable-fizz.node.development.js0.0%-0.1%3.74 KB3.74 KB1.43 KB1.43 KBNODE_DEV

react-art

FileFilesize DiffGzip DiffPrev SizeCurrent SizePrev GzipCurrent GzipENV
react-art.development.js+0.2%+0.2%565.39 KB566.34 KB122.15 KB122.39 KBUMD_DEV
react-art.production.min.js0.0%-0.0%96.76 KB96.76 KB29.67 KB29.67 KBUMD_PROD
react-art.development.js+0.2%+0.2%496.3 KB497.26 KB104.76 KB105 KBNODE_DEV
react-art.production.min.js0.0%-0.0%61.77 KB61.77 KB18.89 KB18.89 KBNODE_PROD
ReactART-dev.js+0.2%+0.2%506.22 KB507.25 KB104.07 KB104.31 KBFB_WWW_DEV

react-native-renderer

FileFilesize DiffGzip DiffPrev SizeCurrent SizePrev GzipCurrent GzipENV
ReactNativeRenderer-dev.js+0.2%+0.2%626.3 KB627.33 KB133.69 KB133.94 KBRN_FB_DEV
ReactNativeRenderer-profiling.js0.0%0.0%251.85 KB251.85 KB44.3 KB44.3 KBRN_FB_PROFILING
ReactNativeRenderer-dev.js+0.2%+0.2%626.22 KB627.24 KB133.65 KB133.9 KBRN_OSS_DEV
ReactNativeRenderer-profiling.js0.0%0.0%251.87 KB251.87 KB44.3 KB44.3 KBRN_OSS_PROFILING
ReactFabric-dev.js+0.2%+0.2%616.73 KB617.76 KB131.33 KB131.59 KBRN_FB_DEV
ReactFabric-prod.js0.0%0.0%239.68 KB239.68 KB41.79 KB41.79 KBRN_FB_PROD
ReactFabric-dev.js+0.2%+0.2%616.64 KB617.66 KB131.3 KB131.55 KBRN_OSS_DEV

react-test-renderer

FileFilesize DiffGzip DiffPrev SizeCurrent SizePrev GzipCurrent GzipENV
react-test-renderer.development.js+0.2%+0.2%507.68 KB508.63 KB106.91 KB107.15 KBUMD_DEV
react-test-renderer.development.js+0.2%+0.2%503.2 KB504.15 KB105.77 KB106 KBNODE_DEV
react-test-renderer.production.min.js0.0%-0.0%62.45 KB62.45 KB18.95 KB18.95 KBNODE_PROD
ReactTestRenderer-dev.js+0.2%+0.2%514.26 KB515.29 KB105.5 KB105.76 KBFB_WWW_DEV
react-test-renderer-shallow.development.js0.0%0.0%38.98 KB38.98 KB9.81 KB9.81 KBUMD_DEV
react-test-renderer-shallow.development.js0.0%-0.0%33.2 KB33.2 KB8.42 KB8.41 KBNODE_DEV

react-reconciler

FileFilesize DiffGzip DiffPrev SizeCurrent SizePrev GzipCurrent GzipENV
react-reconciler.development.js+0.2%+0.3%496.41 KB497.37 KB103.57 KB103.84 KBNODE_DEV
react-reconciler-persistent.development.js+0.2%+0.3%494.3 KB495.25 KB102.71 KB102.98 KBNODE_DEV

Generated by 🚫 dangerJS

const container = document.createElement('div');
ReactDOM.render(<App />, container);
while (error === null) {
Scheduler.unstable_flushNumberOfYields(1);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Since the first yieldValue call happens in a passive effect, this actually will flush the effects synchronously

ReactDOM.render(<App />, container);
while (error === null) {
Scheduler.unstable_flushNumberOfYields(1);
await Promise.resolve();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Don't think this matters

This was referenced Mar 10, 2020
mrizwanashiq pushed a commit to mrizwanashiq/react that referenced this pull request Jun 25, 2026
* Warn about async infinite useEffect loop
* Make tests sync
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@gaearon@sizebot@acdlite@facebook-github-bot