Check for infinite update loops even if unmounted (#24697)
* [FORKED] Check for infinite update loops even if unmounted The infinite update loop check doesn't need to run if the component already unmounted, because an update to an unmounted component can't cause a re-render. But because we used to run the check in this case, anyway, I found one test in www that happens to "rely on" this behavior (accidentally). The test is a pretty messy snapshot thing that I have no interest fixing so to unblock the sync I'm just going to switch this back to how it was. * Add previous commit to forked revisions
Andrew Clark committed
Jun 8, 2022 at 22:29 UTC
8186b19378d9085fcdec32c42e2ec8845c1f8ce0
5 files changed
+27
-8
packages/react-reconciler/src/ReactFiberConcurrentUpdates.new.js
+11
-1
@@ -19,7 +19,10 @@ import type {
19
import type {Lane, Lanes} from './ReactFiberLane.new';
20
import type {OffscreenInstance} from './ReactFiberOffscreenComponent';
21
22
-import {warnAboutUpdateOnNotYetMountedFiberInDEV} from './ReactFiberWorkLoop.new';
22
+import {
23
+ warnAboutUpdateOnNotYetMountedFiberInDEV,
24
+ throwIfInfiniteUpdateLoopDetected,
25
+} from './ReactFiberWorkLoop.new';
26
import {
27
NoLane,
28
NoLanes,
@@ -207,6 +210,13 @@ function markUpdateLaneFromFiberToRoot(
210
}
211
212
function getRootForUpdatedFiber(sourceFiber: Fiber): FiberRoot | null {
213
+ // TODO: We will detect and infinite update loop and throw even if this fiber
214
+ // has already unmounted. This isn't really necessary but it happens to be the
215
+ // current behavior we've used for several release cycles. Consider not
216
+ // performing this check if the updated fiber already unmounted, since it's
217
+ // not possible for that to cause an infinite update loop.
218
+ throwIfInfiniteUpdateLoopDetected();
219
+
220
// When a setState happens, we must ensure the root is scheduled. Because
221
// update queues do not have a backpointer to the root, the only way to do
222
// this currently is to walk up the return path. This used to not be a big
packages/react-reconciler/src/ReactFiberConcurrentUpdates.old.js
+11
-1
@@ -18,7 +18,10 @@ import type {
18
} from './ReactFiberClassUpdateQueue.old';
19
import type {Lane} from './ReactFiberLane.old';
20
21
-import {warnAboutUpdateOnNotYetMountedFiberInDEV} from './ReactFiberWorkLoop.old';
21
+import {
22
+ warnAboutUpdateOnNotYetMountedFiberInDEV,
23
+ throwIfInfiniteUpdateLoopDetected,
24
+} from './ReactFiberWorkLoop.old';
25
import {mergeLanes} from './ReactFiberLane.old';
26
import {NoFlags, Placement, Hydrating} from './ReactFiberFlags';
27
import {HostRoot} from './ReactWorkTags';
@@ -143,6 +146,13 @@ function markUpdateLaneFromFiberToRoot(
146
sourceFiber: Fiber,
147
lane: Lane,
148
): FiberRoot | null {
149
+ // TODO: We will detect and infinite update loop and throw even if this fiber
150
+ // has already unmounted. This isn't really necessary but it happens to be the
151
+ // current behavior we've used for several release cycles. Consider not
152
+ // performing this check if the updated fiber already unmounted, since it's
153
+ // not possible for that to cause an infinite update loop.
154
+ throwIfInfiniteUpdateLoopDetected();
155
+
156
// Update the source fiber's lanes
157
sourceFiber.lanes = mergeLanes(sourceFiber.lanes, lane);
158
let alternate = sourceFiber.alternate;
packages/react-reconciler/src/ReactFiberWorkLoop.new.js
+3
-3
@@ -536,8 +536,6 @@ export function scheduleUpdateOnFiber(
536
lane: Lane,
537
eventTime: number,
538
) {
539
- checkForNestedUpdates();
540
-
539
if (__DEV__) {
540
if (isRunningInsertionEffect) {
541
console.error('useInsertionEffect must not schedule updates.');
@@ -2782,10 +2780,12 @@ function jnd(timeElapsed: number) {
2780
: ceil(timeElapsed / 1960) * 1960;
2781
}
2782
2785
-function checkForNestedUpdates() {
2783
+export function throwIfInfiniteUpdateLoopDetected() {
2784
if (nestedUpdateCount > NESTED_UPDATE_LIMIT) {
2785
nestedUpdateCount = 0;
2786
+ nestedPassiveUpdateCount = 0;
2787
rootWithNestedUpdates = null;
2788
+ rootWithPassiveNestedUpdates = null;
2789
2790
throw new Error(
2791
'Maximum update depth exceeded. This can happen when a component ' +
packages/react-reconciler/src/ReactFiberWorkLoop.old.js
+1
-3
@@ -531,8 +531,6 @@ export function scheduleUpdateOnFiber(
531
lane: Lane,
532
eventTime: number,
533
) {
534
- checkForNestedUpdates();
535
-
534
if (__DEV__) {
535
if (isRunningInsertionEffect) {
536
console.error('useInsertionEffect must not schedule updates.');
@@ -2771,7 +2769,7 @@ function jnd(timeElapsed: number) {
2769
: ceil(timeElapsed / 1960) * 1960;
2770
}
2771
2774
-function checkForNestedUpdates() {
2772
+export function throwIfInfiniteUpdateLoopDetected() {
2773
if (nestedUpdateCount > NESTED_UPDATE_LIMIT) {
2774
nestedUpdateCount = 0;
2775
rootWithNestedUpdates = null;
scripts/merge-fork/forked-revisions
+1
@@ -1,2 +1,3 @@
1
+58bb11764bf0bb6db47527a64f693f67cdd3b0bb [FORKED] Check for infinite update loops even if unmounted
2
31882b5dd66f34f70d341ea2781cacbe802bf4d5 [FORKED] Bugfix: Revealing a hidden update
3
17691acc071d56261d43c3cf183f287d983baa9b [FORKED] Don't update childLanes until after current render