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

[Bugfix] Prevent infinite update loop caused by a synchronous update in a passive effect #22277

Merged
merged 2 commits into from
Sep 9, 2021
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
32 changes: 32 additions & 0 deletions packages/react-dom/src/__tests__/ReactUpdates-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1594,6 +1594,7 @@ describe('ReactUpdates', () => {
});
});

// TODO: Replace this branch with @gate pragmas
if (__DEV__) {
it('warns about a deferred infinite update loop with useEffect', () => {
function NonTerminating() {
Expand Down Expand Up @@ -1684,4 +1685,35 @@ describe('ReactUpdates', () => {
expect(container.textContent).toBe('1000');
});
}

it('prevents infinite update loop triggered by synchronous updates in useEffect', () => {
// Ignore flushSync warning
spyOnDev(console, 'error');

function NonTerminating() {
const [step, setStep] = React.useState(0);
React.useEffect(() => {
// Other examples of synchronous updates in useEffect are imperative
// event dispatches like `el.focus`, or `useSyncExternalStore`, which
// may schedule a synchronous update upon subscribing if it detects
// that the store has been mutated since the initial render.
//
// (Originally I wrote this test using `el.focus` but those errors
// get dispatched in a JSDOM event and I don't know how to "catch" those
// so that they don't fail the test.)
ReactDOM.flushSync(() => {
setStep(step + 1);
});
}, [step]);
return step;
}

const container = document.createElement('div');
const root = ReactDOM.createRoot(container);
expect(() => {
ReactDOM.flushSync(() => {
root.render(<NonTerminating />);
});
}).toThrow('Maximum update depth exceeded');
});
});
45 changes: 28 additions & 17 deletions packages/react-reconciler/src/ReactFiberWorkLoop.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -1897,6 +1897,15 @@ function commitRootImpl(root, renderPriorityLevel) {
remainingLanes = root.pendingLanes;

// Check if there's remaining work on this root
// TODO: This is part of the `componentDidCatch` implementation. Its purpose
// is to detect whether something might have called setState inside
// `componentDidCatch`. The mechanism is known to be flawed because `setState`
// inside `componentDidCatch` is itself flawed — that's why we recommend
// `getDerivedStateFromError` instead. However, it could be improved by
// checking if remainingLanes includes Sync work, instead of whether there's
// any work remaining at all (which would also include stuff like Suspense
// retries or transitions). It's been like this for a while, though, so fixing
// it probably isn't that urgent.
Copy link
Collaborator Author

@acdlite acdlite Sep 9, 2021

Choose a reason for hiding this comment

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

Unrelated to fix, just noticed it while I was here and left a comment for later

if (remainingLanes === NoLanes) {
// If there's no remaining work, we can clear the set of already failed
// error boundaries.
Expand All @@ -1909,23 +1918,6 @@ function commitRootImpl(root, renderPriorityLevel) {
}
}

if (includesSomeLane(remainingLanes, (SyncLane: Lane))) {
if (enableProfilerTimer && enableProfilerNestedUpdatePhase) {
markNestedUpdateScheduled();
}

// Count the number of times the root synchronously re-renders without
// finishing. If there are too many, it indicates an infinite update loop.
if (root === rootWithNestedUpdates) {
nestedUpdateCount++;
} else {
nestedUpdateCount = 0;
rootWithNestedUpdates = root;
}
} else {
nestedUpdateCount = 0;
}

onCommitRootDevTools(finishedWork.stateNode, renderPriorityLevel);

if (enableUpdaterTracking) {
Expand Down Expand Up @@ -1964,6 +1956,25 @@ function commitRootImpl(root, renderPriorityLevel) {
flushPassiveEffects();
}

// Read this again, since a passive effect might have updated it
remainingLanes = root.pendingLanes;
if (includesSomeLane(remainingLanes, (SyncLane: Lane))) {
if (enableProfilerTimer && enableProfilerNestedUpdatePhase) {
markNestedUpdateScheduled();
}

// Count the number of times the root synchronously re-renders without
// finishing. If there are too many, it indicates an infinite update loop.
if (root === rootWithNestedUpdates) {
nestedUpdateCount++;
} else {
nestedUpdateCount = 0;
rootWithNestedUpdates = root;
}
} else {
nestedUpdateCount = 0;
}

// If layout work was scheduled, flush it now.
flushSyncCallbacks();

Expand Down
45 changes: 28 additions & 17 deletions packages/react-reconciler/src/ReactFiberWorkLoop.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -1897,6 +1897,15 @@ function commitRootImpl(root, renderPriorityLevel) {
remainingLanes = root.pendingLanes;

// Check if there's remaining work on this root
// TODO: This is part of the `componentDidCatch` implementation. Its purpose
// is to detect whether something might have called setState inside
// `componentDidCatch`. The mechanism is known to be flawed because `setState`
// inside `componentDidCatch` is itself flawed — that's why we recommend
// `getDerivedStateFromError` instead. However, it could be improved by
// checking if remainingLanes includes Sync work, instead of whether there's
// any work remaining at all (which would also include stuff like Suspense
// retries or transitions). It's been like this for a while, though, so fixing
// it probably isn't that urgent.
if (remainingLanes === NoLanes) {
// If there's no remaining work, we can clear the set of already failed
// error boundaries.
Expand All @@ -1909,23 +1918,6 @@ function commitRootImpl(root, renderPriorityLevel) {
}
}

if (includesSomeLane(remainingLanes, (SyncLane: Lane))) {
if (enableProfilerTimer && enableProfilerNestedUpdatePhase) {
markNestedUpdateScheduled();
}

// Count the number of times the root synchronously re-renders without
// finishing. If there are too many, it indicates an infinite update loop.
if (root === rootWithNestedUpdates) {
nestedUpdateCount++;
} else {
nestedUpdateCount = 0;
rootWithNestedUpdates = root;
}
} else {
nestedUpdateCount = 0;
}

onCommitRootDevTools(finishedWork.stateNode, renderPriorityLevel);

if (enableUpdaterTracking) {
Expand Down Expand Up @@ -1964,6 +1956,25 @@ function commitRootImpl(root, renderPriorityLevel) {
flushPassiveEffects();
}

// Read this again, since a passive effect might have updated it
remainingLanes = root.pendingLanes;
if (includesSomeLane(remainingLanes, (SyncLane: Lane))) {
if (enableProfilerTimer && enableProfilerNestedUpdatePhase) {
markNestedUpdateScheduled();
}

// Count the number of times the root synchronously re-renders without
// finishing. If there are too many, it indicates an infinite update loop.
if (root === rootWithNestedUpdates) {
nestedUpdateCount++;
} else {
nestedUpdateCount = 0;
rootWithNestedUpdates = root;
}
} else {
nestedUpdateCount = 0;
}

// If layout work was scheduled, flush it now.
flushSyncCallbacks();

Expand Down