Skip to content

Commit f4e0d4e

Browse files
authored
[enableInfiniteRenderLoopDetection] Add a flag to force throwing (#36357)
Adds a `enableInfiniteRenderLoopDetectionForceThrow` flag, which changes the detection mechanism behaviour to start throwing, when the loop is observed. By default, the value is set to `false`, also made dynamic from the start for `www`.
1 parent ad5dfc8 commit f4e0d4e

11 files changed

Lines changed: 203 additions & 8 deletions

‎packages/react-dom/src/__tests__/ReactUpdates-test.js‎

Lines changed: 151 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,7 +1793,14 @@ describe('ReactUpdates', () => {
17931793
});
17941794

17951795
it("warns about potential infinite loop if there's a synchronous render phase update on another component",async()=>{
1796-
if(!__DEV__||gate(flags=>!flags.enableInfiniteRenderLoopDetection)){
1796+
if(
1797+
!__DEV__||
1798+
gate(
1799+
flags=>
1800+
!flags.enableInfiniteRenderLoopDetection||
1801+
flags.enableInfiniteRenderLoopDetectionForceThrow,
1802+
)
1803+
){
17971804
return;
17981805
}
17991806
letsetState;
@@ -1831,7 +1838,14 @@ describe('ReactUpdates', () => {
18311838
});
18321839

18331840
it("warns about potential infinite loop if there's an async render phase update on another component",async()=>{
1834-
if(!__DEV__||gate(flags=>!flags.enableInfiniteRenderLoopDetection)){
1841+
if(
1842+
!__DEV__||
1843+
gate(
1844+
flags=>
1845+
!flags.enableInfiniteRenderLoopDetection||
1846+
flags.enableInfiniteRenderLoopDetectionForceThrow,
1847+
)
1848+
){
18351849
return;
18361850
}
18371851
letsetState;
@@ -2007,7 +2021,14 @@ describe('ReactUpdates', () => {
20072021
});
20082022

20092023
it('warns instead of throwing when infinite Suspense ping loop is detected via enableInfiniteRenderLoopDetection during commit phase',async()=>{
2010-
if(!__DEV__||gate(flags=>!flags.enableInfiniteRenderLoopDetection)){
2024+
if(
2025+
!__DEV__||
2026+
gate(
2027+
flags=>
2028+
!flags.enableInfiniteRenderLoopDetection||
2029+
flags.enableInfiniteRenderLoopDetectionForceThrow,
2030+
)
2031+
){
20112032
return;
20122033
}
20132034

@@ -2115,6 +2136,133 @@ describe('ReactUpdates', () => {
21152136
expect(errors).toEqual([]);
21162137
});
21172138

2139+
// @gate enableInfiniteRenderLoopDetection && enableInfiniteRenderLoopDetectionForceThrow
2140+
it('throws when sync render-phase update loop is detected with force-throw enabled',async()=>{
2141+
// Render-phase setState on another component's hook produces a sync
2142+
// recursive update. With ForceThrow enabled this should throw via
2143+
// throwForcedInfiniteRenderLoopError instead of only warning in DEV.
2144+
letsetState;
2145+
letshouldStop=false;
2146+
functionApp(){
2147+
const[,_setState]=React.useState(0);
2148+
setState=_setState;
2149+
return<Child/>;
2150+
}
2151+
2152+
functionChild(){
2153+
if(shouldStop){
2154+
returnnull;
2155+
}
2156+
setState(n=>n+1);
2157+
returnnull;
2158+
}
2159+
2160+
constcontainer=document.createElement('div');
2161+
consterrors=[];
2162+
constcaptureError=error=>{
2163+
errors.push(error.message);
2164+
// Stop scheduling new updates so the test (and the gate-off variant
2165+
// where the legacy error path is recoverable) can terminate cleanly
2166+
// without tripping the babel infinite-loop guard.
2167+
shouldStop=true;
2168+
};
2169+
constroot=ReactDOMClient.createRoot(container,{
2170+
onUncaughtError: captureError,
2171+
onRecoverableError: captureError,
2172+
onCaughtError: captureError,
2173+
});
2174+
2175+
// The render-phase setState path also produces a dev-only "Cannot update
2176+
// a component while rendering a different component" console.error on
2177+
// every recursion. Swallow those so the test framework doesn't require
2178+
// us to assert each one.
2179+
constoriginalConsoleError=console.error;
2180+
console.error=msg=>{
2181+
if(
2182+
typeofmsg==='string'&&
2183+
msg.startsWith('Cannot update a component')
2184+
){
2185+
return;
2186+
}
2187+
originalConsoleError(msg);
2188+
};
2189+
try{
2190+
awaitact(()=>{
2191+
root.render(<App/>);
2192+
});
2193+
}finally{
2194+
console.error=originalConsoleError;
2195+
}
2196+
2197+
expect(errors.length).toBeGreaterThanOrEqual(1);
2198+
expect(errors[0]).toContain(
2199+
'Maximum update depth exceeded. This could be an infinite loop.',
2200+
);
2201+
});
2202+
2203+
// @gate enableInfiniteRenderLoopDetection && enableInfiniteRenderLoopDetectionForceThrow
2204+
it('throws when phase-spawn update loop is detected with force-throw enabled',async()=>{
2205+
// Wrapping the initial render in startTransition makes the render-phase
2206+
// setState inherit a non-sync transition lane. After commit, the next
2207+
// render is non-sync, so the loop detector classifies the recursion as
2208+
// NESTED_UPDATE_PHASE_SPAWN (rather than SYNC_LANE). With ForceThrow
2209+
// enabled, this branch should throw via throwForcedInfiniteRenderLoopError
2210+
// instead of only warning in DEV.
2211+
letsetState;
2212+
letshouldStop=false;
2213+
// Hard cap on Child renders. Without enableInfiniteRenderLoopDetection,
2214+
// the PHASE_SPAWN branch is gated off entirely, so no throw fires and
2215+
// the loop would otherwise run until the babel infinite-loop guard.
2216+
letrenderCount=0;
2217+
constRENDER_CAP=100;
2218+
functionApp(){
2219+
const[,_setState]=React.useState(0);
2220+
setState=_setState;
2221+
return<Child/>;
2222+
}
2223+
2224+
functionChild(){
2225+
if(shouldStop||renderCount>=RENDER_CAP){
2226+
returnnull;
2227+
}
2228+
renderCount++;
2229+
setState(n=>n+1);
2230+
returnnull;
2231+
}
2232+
2233+
constcontainer=document.createElement('div');
2234+
consterrors=[];
2235+
constroot=ReactDOMClient.createRoot(container,{
2236+
onUncaughtError: error=>{
2237+
errors.push(error.message);
2238+
shouldStop=true;
2239+
},
2240+
});
2241+
2242+
constoriginalConsoleError=console.error;
2243+
console.error=msg=>{
2244+
if(
2245+
typeofmsg==='string'&&
2246+
msg.startsWith('Cannot update a component')
2247+
){
2248+
return;
2249+
}
2250+
originalConsoleError(msg);
2251+
};
2252+
try{
2253+
awaitact(()=>{
2254+
React.startTransition(()=>root.render(<App/>));
2255+
});
2256+
}finally{
2257+
console.error=originalConsoleError;
2258+
}
2259+
2260+
expect(errors.length).toBeGreaterThanOrEqual(1);
2261+
expect(errors[0]).toContain(
2262+
'Maximum update depth exceeded. This could be an infinite loop.',
2263+
);
2264+
});
2265+
21182266
it('prevents infinite update loop triggered by too many updates in ref callbacks',async()=>{
21192267
letscheduleUpdate;
21202268
functionTooManyRefUpdates(){

‎packages/react-reconciler/src/ReactFiberWorkLoop.js‎

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import {
5151
disableLegacyContext,
5252
alwaysThrottleRetries,
5353
enableInfiniteRenderLoopDetection,
54+
enableInfiniteRenderLoopDetectionForceThrow,
5455
disableLegacyMode,
5556
enableComponentPerformanceTrack,
5657
enableYieldingBeforePassive,
@@ -5175,6 +5176,27 @@ export function resolveRetryWakeable(boundaryFiber: Fiber, wakeable: Wakeable) {
51755176
retryTimedOutBoundary(boundaryFiber,retryLane);
51765177
}
51775178

5179+
functionthrowForcedInfiniteRenderLoopError(
5180+
root: FiberRoot|null,
5181+
renderLanes: Lanes,
5182+
): empty{
5183+
if(root!==null){
5184+
// Disable concurrent error recovery for the in-progress render so the thrown
5185+
// error reaches the nearest error boundary and breaks the infinite update
5186+
// loop instead of being silently retried by the recovery mechanism.
5187+
root.errorRecoveryDisabledLanes=mergeLanes(
5188+
root.errorRecoveryDisabledLanes,
5189+
renderLanes,
5190+
);
5191+
}
5192+
thrownewError(
5193+
'Maximum update depth exceeded. This could be an infinite loop. This can happen when a component '+
5194+
'repeatedly calls setState during render phase or inside useLayoutEffect, '+
5195+
'causing infinite render loop. React limits the number of nested updates to '+
5196+
'prevent infinite loops.',
5197+
);
5198+
}
5199+
51785200
exportfunctionthrowIfInfiniteUpdateLoopDetected(
51795201
isFromInfiniteRenderLoopDetectionInstrumentation: boolean,
51805202
){
@@ -5191,10 +5213,16 @@ export function throwIfInfiniteUpdateLoopDetected(
51915213
if(updateKind===NESTED_UPDATE_SYNC_LANE){
51925214
if(
51935215
isFromInfiniteRenderLoopDetectionInstrumentation||
5194-
(executionContext&RenderContext&&workInProgressRoot!==null)
5216+
(executionContext&RenderContext)!==NoContext
51955217
){
5196-
// This loop was identified only because of the instrumentation gated with enableInfiniteRenderLoopDetection, warn instead of throwing.
5197-
if(__DEV__){
5218+
// This loop was identified only because of the instrumentation gated with enableInfiniteRenderLoopDetection,
5219+
// warn instead of throwing, unless enableInfiniteRenderLoopDetectionForceThrow.
5220+
if(enableInfiniteRenderLoopDetectionForceThrow){
5221+
throwForcedInfiniteRenderLoopError(
5222+
workInProgressRoot,
5223+
workInProgressRootRenderLanes,
5224+
);
5225+
}elseif(__DEV__){
51985226
console.error(
51995227
'Maximum update depth exceeded. This could be an infinite loop. This can happen when a component '+
52005228
'repeatedly calls setState during render phase or inside useLayoutEffect, '+
@@ -5211,7 +5239,12 @@ export function throwIfInfiniteUpdateLoopDetected(
52115239
);
52125240
}
52135241
}elseif(updateKind===NESTED_UPDATE_PHASE_SPAWN){
5214-
if(__DEV__){
5242+
if(enableInfiniteRenderLoopDetectionForceThrow){
5243+
throwForcedInfiniteRenderLoopError(
5244+
workInProgressRoot,
5245+
workInProgressRootRenderLanes,
5246+
);
5247+
}elseif(__DEV__){
52155248
console.error(
52165249
'Maximum update depth exceeded. This could be an infinite loop. This can happen when a component '+
52175250
'repeatedly calls setState during render phase or inside useLayoutEffect, '+

‎packages/shared/ReactFeatureFlags.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ export const transitionLaneExpirationMs = 5000;
144144
* by setState or similar outside of the component owning the state.
145145
*/
146146
exportconstenableInfiniteRenderLoopDetection: boolean=false;
147+
/**
148+
* When `enableInfiniteRenderLoopDetection` is on, forces the detection
149+
* mechanism to throw instead of only warning in cases where it would
150+
* otherwise downgrade to a warning.
151+
*/
152+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=false;
147153

148154
exportconstenableFragmentRefs: boolean=true;
149155
exportconstenableFragmentRefsScrollIntoView: boolean=true;

‎packages/shared/forks/ReactFeatureFlags.native-fb.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export const enableCreateEventHandleAPI: boolean = false;
4747
exportconstenableMoveBefore: boolean=true;
4848
exportconstenableFizzExternalRuntime: boolean=true;
4949
exportconstenableInfiniteRenderLoopDetection: boolean=false;
50+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=false;
5051
exportconstenableLegacyCache: boolean=false;
5152
exportconstenableLegacyFBSupport: boolean=false;
5253
exportconstenableLegacyHidden: boolean=false;

‎packages/shared/forks/ReactFeatureFlags.native-oss.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export const enableCreateEventHandleAPI: boolean = false;
3131
exportconstenableMoveBefore: boolean=true;
3232
exportconstenableFizzExternalRuntime: boolean=true;
3333
exportconstenableInfiniteRenderLoopDetection: boolean=false;
34+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=false;
3435
exportconstenableLegacyCache: boolean=false;
3536
exportconstenableLegacyFBSupport: boolean=false;
3637
exportconstenableLegacyHidden: boolean=false;

‎packages/shared/forks/ReactFeatureFlags.test-renderer.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export const passChildrenWhenCloningPersistedNodes: boolean = false;
5252
exportconstdisableClientCache: boolean=true;
5353

5454
exportconstenableInfiniteRenderLoopDetection: boolean=false;
55+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=false;
5556

5657
exportconstenableEagerAlternateStateNodeCleanup: boolean=true;
5758
exportconstenableEffectEventMutationPhase: boolean=false;

‎packages/shared/forks/ReactFeatureFlags.test-renderer.native-fb.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export const enableCreateEventHandleAPI = false;
2626
exportconstenableMoveBefore=false;
2727
exportconstenableFizzExternalRuntime=true;
2828
exportconstenableInfiniteRenderLoopDetection=false;
29+
exportconstenableInfiniteRenderLoopDetectionForceThrow=false;
2930
exportconstenableLegacyCache=false;
3031
exportconstenableLegacyFBSupport=false;
3132
exportconstenableLegacyHidden=false;

‎packages/shared/forks/ReactFeatureFlags.test-renderer.www.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export const passChildrenWhenCloningPersistedNodes: boolean = false;
5454
exportconstdisableClientCache: boolean=true;
5555

5656
exportconstenableInfiniteRenderLoopDetection: boolean=false;
57+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=false;
5758

5859
exportconstenableReactTestRendererWarning: boolean=false;
5960
exportconstdisableLegacyMode: boolean=true;

‎packages/shared/forks/ReactFeatureFlags.www-dynamic.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export const transitionLaneExpirationMs = 5000;
2727
exportconstenableSchedulingProfiler: boolean=__VARIANT__;
2828

2929
exportconstenableInfiniteRenderLoopDetection: boolean=__VARIANT__;
30+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=__VARIANT__;
3031

3132
exportconstenableFastAddPropertiesInDiffing: boolean=__VARIANT__;
3233
exportconstenableSuspenseyImages: boolean=__VARIANT__;

‎packages/shared/forks/ReactFeatureFlags.www.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export const {
2020
disableSchedulerTimeoutInWorkLoop,
2121
enableEffectEventMutationPhase,
2222
enableInfiniteRenderLoopDetection,
23+
enableInfiniteRenderLoopDetectionForceThrow,
2324
enableNoCloningMemoCache,
2425
enableObjectFiber,
2526
enableRetryLaneExpiration,

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { // Add copy buttons to all
 blocks
(function() {
function addCopyButtons() {
document.querySelectorAll('pre code').forEach(function(codeBlock) {
if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;
codeBlock.parentElement.setAttribute('data-copy-added', 'true');
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';
btn.onmouseover = function() { this.style.opacity = '1'; };
btn.onmouseout = function() { this.style.opacity = '0.7'; };
btn.onclick = function() {
navigator.clipboard.writeText(codeBlock.textContent).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = 'Copy'; }, 1500);
});
};
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(btn);
});
}
addCopyButtons();
// Re-run on dynamic content
var observer = new MutationObserver(addCopyButtons);
observer.observe(document.body, { childList: true, subtree: true });
})();
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
[enableInfiniteRenderLoopDetection] Add a flag to force throwing (#36… · react/react@f4e0d4e · GitHub
Skip to content

Commit f4e0d4e

Browse files
authored
[enableInfiniteRenderLoopDetection] Add a flag to force throwing (#36357)
Adds a `enableInfiniteRenderLoopDetectionForceThrow` flag, which changes the detection mechanism behaviour to start throwing, when the loop is observed. By default, the value is set to `false`, also made dynamic from the start for `www`.
1 parent ad5dfc8 commit f4e0d4e

11 files changed

Lines changed: 203 additions & 8 deletions

‎packages/react-dom/src/__tests__/ReactUpdates-test.js‎

Lines changed: 151 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,7 +1793,14 @@ describe('ReactUpdates', () => {
17931793
});
17941794

17951795
it("warns about potential infinite loop if there's a synchronous render phase update on another component",async()=>{
1796-
if(!__DEV__||gate(flags=>!flags.enableInfiniteRenderLoopDetection)){
1796+
if(
1797+
!__DEV__||
1798+
gate(
1799+
flags=>
1800+
!flags.enableInfiniteRenderLoopDetection||
1801+
flags.enableInfiniteRenderLoopDetectionForceThrow,
1802+
)
1803+
){
17971804
return;
17981805
}
17991806
letsetState;
@@ -1831,7 +1838,14 @@ describe('ReactUpdates', () => {
18311838
});
18321839

18331840
it("warns about potential infinite loop if there's an async render phase update on another component",async()=>{
1834-
if(!__DEV__||gate(flags=>!flags.enableInfiniteRenderLoopDetection)){
1841+
if(
1842+
!__DEV__||
1843+
gate(
1844+
flags=>
1845+
!flags.enableInfiniteRenderLoopDetection||
1846+
flags.enableInfiniteRenderLoopDetectionForceThrow,
1847+
)
1848+
){
18351849
return;
18361850
}
18371851
letsetState;
@@ -2007,7 +2021,14 @@ describe('ReactUpdates', () => {
20072021
});
20082022

20092023
it('warns instead of throwing when infinite Suspense ping loop is detected via enableInfiniteRenderLoopDetection during commit phase',async()=>{
2010-
if(!__DEV__||gate(flags=>!flags.enableInfiniteRenderLoopDetection)){
2024+
if(
2025+
!__DEV__||
2026+
gate(
2027+
flags=>
2028+
!flags.enableInfiniteRenderLoopDetection||
2029+
flags.enableInfiniteRenderLoopDetectionForceThrow,
2030+
)
2031+
){
20112032
return;
20122033
}
20132034

@@ -2115,6 +2136,133 @@ describe('ReactUpdates', () => {
21152136
expect(errors).toEqual([]);
21162137
});
21172138

2139+
// @gate enableInfiniteRenderLoopDetection && enableInfiniteRenderLoopDetectionForceThrow
2140+
it('throws when sync render-phase update loop is detected with force-throw enabled',async()=>{
2141+
// Render-phase setState on another component's hook produces a sync
2142+
// recursive update. With ForceThrow enabled this should throw via
2143+
// throwForcedInfiniteRenderLoopError instead of only warning in DEV.
2144+
letsetState;
2145+
letshouldStop=false;
2146+
functionApp(){
2147+
const[,_setState]=React.useState(0);
2148+
setState=_setState;
2149+
return<Child/>;
2150+
}
2151+
2152+
functionChild(){
2153+
if(shouldStop){
2154+
returnnull;
2155+
}
2156+
setState(n=>n+1);
2157+
returnnull;
2158+
}
2159+
2160+
constcontainer=document.createElement('div');
2161+
consterrors=[];
2162+
constcaptureError=error=>{
2163+
errors.push(error.message);
2164+
// Stop scheduling new updates so the test (and the gate-off variant
2165+
// where the legacy error path is recoverable) can terminate cleanly
2166+
// without tripping the babel infinite-loop guard.
2167+
shouldStop=true;
2168+
};
2169+
constroot=ReactDOMClient.createRoot(container,{
2170+
onUncaughtError: captureError,
2171+
onRecoverableError: captureError,
2172+
onCaughtError: captureError,
2173+
});
2174+
2175+
// The render-phase setState path also produces a dev-only "Cannot update
2176+
// a component while rendering a different component" console.error on
2177+
// every recursion. Swallow those so the test framework doesn't require
2178+
// us to assert each one.
2179+
constoriginalConsoleError=console.error;
2180+
console.error=msg=>{
2181+
if(
2182+
typeofmsg==='string'&&
2183+
msg.startsWith('Cannot update a component')
2184+
){
2185+
return;
2186+
}
2187+
originalConsoleError(msg);
2188+
};
2189+
try{
2190+
awaitact(()=>{
2191+
root.render(<App/>);
2192+
});
2193+
}finally{
2194+
console.error=originalConsoleError;
2195+
}
2196+
2197+
expect(errors.length).toBeGreaterThanOrEqual(1);
2198+
expect(errors[0]).toContain(
2199+
'Maximum update depth exceeded. This could be an infinite loop.',
2200+
);
2201+
});
2202+
2203+
// @gate enableInfiniteRenderLoopDetection && enableInfiniteRenderLoopDetectionForceThrow
2204+
it('throws when phase-spawn update loop is detected with force-throw enabled',async()=>{
2205+
// Wrapping the initial render in startTransition makes the render-phase
2206+
// setState inherit a non-sync transition lane. After commit, the next
2207+
// render is non-sync, so the loop detector classifies the recursion as
2208+
// NESTED_UPDATE_PHASE_SPAWN (rather than SYNC_LANE). With ForceThrow
2209+
// enabled, this branch should throw via throwForcedInfiniteRenderLoopError
2210+
// instead of only warning in DEV.
2211+
letsetState;
2212+
letshouldStop=false;
2213+
// Hard cap on Child renders. Without enableInfiniteRenderLoopDetection,
2214+
// the PHASE_SPAWN branch is gated off entirely, so no throw fires and
2215+
// the loop would otherwise run until the babel infinite-loop guard.
2216+
letrenderCount=0;
2217+
constRENDER_CAP=100;
2218+
functionApp(){
2219+
const[,_setState]=React.useState(0);
2220+
setState=_setState;
2221+
return<Child/>;
2222+
}
2223+
2224+
functionChild(){
2225+
if(shouldStop||renderCount>=RENDER_CAP){
2226+
returnnull;
2227+
}
2228+
renderCount++;
2229+
setState(n=>n+1);
2230+
returnnull;
2231+
}
2232+
2233+
constcontainer=document.createElement('div');
2234+
consterrors=[];
2235+
constroot=ReactDOMClient.createRoot(container,{
2236+
onUncaughtError: error=>{
2237+
errors.push(error.message);
2238+
shouldStop=true;
2239+
},
2240+
});
2241+
2242+
constoriginalConsoleError=console.error;
2243+
console.error=msg=>{
2244+
if(
2245+
typeofmsg==='string'&&
2246+
msg.startsWith('Cannot update a component')
2247+
){
2248+
return;
2249+
}
2250+
originalConsoleError(msg);
2251+
};
2252+
try{
2253+
awaitact(()=>{
2254+
React.startTransition(()=>root.render(<App/>));
2255+
});
2256+
}finally{
2257+
console.error=originalConsoleError;
2258+
}
2259+
2260+
expect(errors.length).toBeGreaterThanOrEqual(1);
2261+
expect(errors[0]).toContain(
2262+
'Maximum update depth exceeded. This could be an infinite loop.',
2263+
);
2264+
});
2265+
21182266
it('prevents infinite update loop triggered by too many updates in ref callbacks',async()=>{
21192267
letscheduleUpdate;
21202268
functionTooManyRefUpdates(){

‎packages/react-reconciler/src/ReactFiberWorkLoop.js‎

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import {
5151
disableLegacyContext,
5252
alwaysThrottleRetries,
5353
enableInfiniteRenderLoopDetection,
54+
enableInfiniteRenderLoopDetectionForceThrow,
5455
disableLegacyMode,
5556
enableComponentPerformanceTrack,
5657
enableYieldingBeforePassive,
@@ -5175,6 +5176,27 @@ export function resolveRetryWakeable(boundaryFiber: Fiber, wakeable: Wakeable) {
51755176
retryTimedOutBoundary(boundaryFiber,retryLane);
51765177
}
51775178

5179+
functionthrowForcedInfiniteRenderLoopError(
5180+
root: FiberRoot|null,
5181+
renderLanes: Lanes,
5182+
): empty{
5183+
if(root!==null){
5184+
// Disable concurrent error recovery for the in-progress render so the thrown
5185+
// error reaches the nearest error boundary and breaks the infinite update
5186+
// loop instead of being silently retried by the recovery mechanism.
5187+
root.errorRecoveryDisabledLanes=mergeLanes(
5188+
root.errorRecoveryDisabledLanes,
5189+
renderLanes,
5190+
);
5191+
}
5192+
thrownewError(
5193+
'Maximum update depth exceeded. This could be an infinite loop. This can happen when a component '+
5194+
'repeatedly calls setState during render phase or inside useLayoutEffect, '+
5195+
'causing infinite render loop. React limits the number of nested updates to '+
5196+
'prevent infinite loops.',
5197+
);
5198+
}
5199+
51785200
exportfunctionthrowIfInfiniteUpdateLoopDetected(
51795201
isFromInfiniteRenderLoopDetectionInstrumentation: boolean,
51805202
){
@@ -5191,10 +5213,16 @@ export function throwIfInfiniteUpdateLoopDetected(
51915213
if(updateKind===NESTED_UPDATE_SYNC_LANE){
51925214
if(
51935215
isFromInfiniteRenderLoopDetectionInstrumentation||
5194-
(executionContext&RenderContext&&workInProgressRoot!==null)
5216+
(executionContext&RenderContext)!==NoContext
51955217
){
5196-
// This loop was identified only because of the instrumentation gated with enableInfiniteRenderLoopDetection, warn instead of throwing.
5197-
if(__DEV__){
5218+
// This loop was identified only because of the instrumentation gated with enableInfiniteRenderLoopDetection,
5219+
// warn instead of throwing, unless enableInfiniteRenderLoopDetectionForceThrow.
5220+
if(enableInfiniteRenderLoopDetectionForceThrow){
5221+
throwForcedInfiniteRenderLoopError(
5222+
workInProgressRoot,
5223+
workInProgressRootRenderLanes,
5224+
);
5225+
}elseif(__DEV__){
51985226
console.error(
51995227
'Maximum update depth exceeded. This could be an infinite loop. This can happen when a component '+
52005228
'repeatedly calls setState during render phase or inside useLayoutEffect, '+
@@ -5211,7 +5239,12 @@ export function throwIfInfiniteUpdateLoopDetected(
52115239
);
52125240
}
52135241
}elseif(updateKind===NESTED_UPDATE_PHASE_SPAWN){
5214-
if(__DEV__){
5242+
if(enableInfiniteRenderLoopDetectionForceThrow){
5243+
throwForcedInfiniteRenderLoopError(
5244+
workInProgressRoot,
5245+
workInProgressRootRenderLanes,
5246+
);
5247+
}elseif(__DEV__){
52155248
console.error(
52165249
'Maximum update depth exceeded. This could be an infinite loop. This can happen when a component '+
52175250
'repeatedly calls setState during render phase or inside useLayoutEffect, '+

‎packages/shared/ReactFeatureFlags.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ export const transitionLaneExpirationMs = 5000;
144144
* by setState or similar outside of the component owning the state.
145145
*/
146146
exportconstenableInfiniteRenderLoopDetection: boolean=false;
147+
/**
148+
* When `enableInfiniteRenderLoopDetection` is on, forces the detection
149+
* mechanism to throw instead of only warning in cases where it would
150+
* otherwise downgrade to a warning.
151+
*/
152+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=false;
147153

148154
exportconstenableFragmentRefs: boolean=true;
149155
exportconstenableFragmentRefsScrollIntoView: boolean=true;

‎packages/shared/forks/ReactFeatureFlags.native-fb.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export const enableCreateEventHandleAPI: boolean = false;
4747
exportconstenableMoveBefore: boolean=true;
4848
exportconstenableFizzExternalRuntime: boolean=true;
4949
exportconstenableInfiniteRenderLoopDetection: boolean=false;
50+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=false;
5051
exportconstenableLegacyCache: boolean=false;
5152
exportconstenableLegacyFBSupport: boolean=false;
5253
exportconstenableLegacyHidden: boolean=false;

‎packages/shared/forks/ReactFeatureFlags.native-oss.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export const enableCreateEventHandleAPI: boolean = false;
3131
exportconstenableMoveBefore: boolean=true;
3232
exportconstenableFizzExternalRuntime: boolean=true;
3333
exportconstenableInfiniteRenderLoopDetection: boolean=false;
34+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=false;
3435
exportconstenableLegacyCache: boolean=false;
3536
exportconstenableLegacyFBSupport: boolean=false;
3637
exportconstenableLegacyHidden: boolean=false;

‎packages/shared/forks/ReactFeatureFlags.test-renderer.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export const passChildrenWhenCloningPersistedNodes: boolean = false;
5252
exportconstdisableClientCache: boolean=true;
5353

5454
exportconstenableInfiniteRenderLoopDetection: boolean=false;
55+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=false;
5556

5657
exportconstenableEagerAlternateStateNodeCleanup: boolean=true;
5758
exportconstenableEffectEventMutationPhase: boolean=false;

‎packages/shared/forks/ReactFeatureFlags.test-renderer.native-fb.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export const enableCreateEventHandleAPI = false;
2626
exportconstenableMoveBefore=false;
2727
exportconstenableFizzExternalRuntime=true;
2828
exportconstenableInfiniteRenderLoopDetection=false;
29+
exportconstenableInfiniteRenderLoopDetectionForceThrow=false;
2930
exportconstenableLegacyCache=false;
3031
exportconstenableLegacyFBSupport=false;
3132
exportconstenableLegacyHidden=false;

‎packages/shared/forks/ReactFeatureFlags.test-renderer.www.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export const passChildrenWhenCloningPersistedNodes: boolean = false;
5454
exportconstdisableClientCache: boolean=true;
5555

5656
exportconstenableInfiniteRenderLoopDetection: boolean=false;
57+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=false;
5758

5859
exportconstenableReactTestRendererWarning: boolean=false;
5960
exportconstdisableLegacyMode: boolean=true;

‎packages/shared/forks/ReactFeatureFlags.www-dynamic.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export const transitionLaneExpirationMs = 5000;
2727
exportconstenableSchedulingProfiler: boolean=__VARIANT__;
2828

2929
exportconstenableInfiniteRenderLoopDetection: boolean=__VARIANT__;
30+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=__VARIANT__;
3031

3132
exportconstenableFastAddPropertiesInDiffing: boolean=__VARIANT__;
3233
exportconstenableSuspenseyImages: boolean=__VARIANT__;

‎packages/shared/forks/ReactFeatureFlags.www.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export const {
2020
disableSchedulerTimeoutInWorkLoop,
2121
enableEffectEventMutationPhase,
2222
enableInfiniteRenderLoopDetection,
23+
enableInfiniteRenderLoopDetectionForceThrow,
2324
enableNoCloningMemoCache,
2425
enableObjectFiber,
2526
enableRetryLaneExpiration,

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { // Force GitHub README to respect dark mode (function() { var style = document.createElement('style'); style.textContent = ' .markdown-body { color-scheme: dark light; } .markdown-body pre { background: #161b22 !important; } .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; } .markdown-body table th, .markdown-body table td { border-color: #30363d !important; } .markdown-body img { background: #0d1117; } .markdown-body blockquote { border-left-color: #8b949e; } .markdown-body hr { border-color: #30363d; } '; document.head.appendChild(style); })(); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' [enableInfiniteRenderLoopDetection] Add a flag to force throwing (#36… · react/react@f4e0d4e · GitHub
Skip to content

Commit f4e0d4e

Browse files
authored
[enableInfiniteRenderLoopDetection] Add a flag to force throwing (#36357)
Adds a `enableInfiniteRenderLoopDetectionForceThrow` flag, which changes the detection mechanism behaviour to start throwing, when the loop is observed. By default, the value is set to `false`, also made dynamic from the start for `www`.
1 parent ad5dfc8 commit f4e0d4e

11 files changed

Lines changed: 203 additions & 8 deletions

‎packages/react-dom/src/__tests__/ReactUpdates-test.js‎

Lines changed: 151 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,7 +1793,14 @@ describe('ReactUpdates', () => {
17931793
});
17941794

17951795
it("warns about potential infinite loop if there's a synchronous render phase update on another component",async()=>{
1796-
if(!__DEV__||gate(flags=>!flags.enableInfiniteRenderLoopDetection)){
1796+
if(
1797+
!__DEV__||
1798+
gate(
1799+
flags=>
1800+
!flags.enableInfiniteRenderLoopDetection||
1801+
flags.enableInfiniteRenderLoopDetectionForceThrow,
1802+
)
1803+
){
17971804
return;
17981805
}
17991806
letsetState;
@@ -1831,7 +1838,14 @@ describe('ReactUpdates', () => {
18311838
});
18321839

18331840
it("warns about potential infinite loop if there's an async render phase update on another component",async()=>{
1834-
if(!__DEV__||gate(flags=>!flags.enableInfiniteRenderLoopDetection)){
1841+
if(
1842+
!__DEV__||
1843+
gate(
1844+
flags=>
1845+
!flags.enableInfiniteRenderLoopDetection||
1846+
flags.enableInfiniteRenderLoopDetectionForceThrow,
1847+
)
1848+
){
18351849
return;
18361850
}
18371851
letsetState;
@@ -2007,7 +2021,14 @@ describe('ReactUpdates', () => {
20072021
});
20082022

20092023
it('warns instead of throwing when infinite Suspense ping loop is detected via enableInfiniteRenderLoopDetection during commit phase',async()=>{
2010-
if(!__DEV__||gate(flags=>!flags.enableInfiniteRenderLoopDetection)){
2024+
if(
2025+
!__DEV__||
2026+
gate(
2027+
flags=>
2028+
!flags.enableInfiniteRenderLoopDetection||
2029+
flags.enableInfiniteRenderLoopDetectionForceThrow,
2030+
)
2031+
){
20112032
return;
20122033
}
20132034

@@ -2115,6 +2136,133 @@ describe('ReactUpdates', () => {
21152136
expect(errors).toEqual([]);
21162137
});
21172138

2139+
// @gate enableInfiniteRenderLoopDetection && enableInfiniteRenderLoopDetectionForceThrow
2140+
it('throws when sync render-phase update loop is detected with force-throw enabled',async()=>{
2141+
// Render-phase setState on another component's hook produces a sync
2142+
// recursive update. With ForceThrow enabled this should throw via
2143+
// throwForcedInfiniteRenderLoopError instead of only warning in DEV.
2144+
letsetState;
2145+
letshouldStop=false;
2146+
functionApp(){
2147+
const[,_setState]=React.useState(0);
2148+
setState=_setState;
2149+
return<Child/>;
2150+
}
2151+
2152+
functionChild(){
2153+
if(shouldStop){
2154+
returnnull;
2155+
}
2156+
setState(n=>n+1);
2157+
returnnull;
2158+
}
2159+
2160+
constcontainer=document.createElement('div');
2161+
consterrors=[];
2162+
constcaptureError=error=>{
2163+
errors.push(error.message);
2164+
// Stop scheduling new updates so the test (and the gate-off variant
2165+
// where the legacy error path is recoverable) can terminate cleanly
2166+
// without tripping the babel infinite-loop guard.
2167+
shouldStop=true;
2168+
};
2169+
constroot=ReactDOMClient.createRoot(container,{
2170+
onUncaughtError: captureError,
2171+
onRecoverableError: captureError,
2172+
onCaughtError: captureError,
2173+
});
2174+
2175+
// The render-phase setState path also produces a dev-only "Cannot update
2176+
// a component while rendering a different component" console.error on
2177+
// every recursion. Swallow those so the test framework doesn't require
2178+
// us to assert each one.
2179+
constoriginalConsoleError=console.error;
2180+
console.error=msg=>{
2181+
if(
2182+
typeofmsg==='string'&&
2183+
msg.startsWith('Cannot update a component')
2184+
){
2185+
return;
2186+
}
2187+
originalConsoleError(msg);
2188+
};
2189+
try{
2190+
awaitact(()=>{
2191+
root.render(<App/>);
2192+
});
2193+
}finally{
2194+
console.error=originalConsoleError;
2195+
}
2196+
2197+
expect(errors.length).toBeGreaterThanOrEqual(1);
2198+
expect(errors[0]).toContain(
2199+
'Maximum update depth exceeded. This could be an infinite loop.',
2200+
);
2201+
});
2202+
2203+
// @gate enableInfiniteRenderLoopDetection && enableInfiniteRenderLoopDetectionForceThrow
2204+
it('throws when phase-spawn update loop is detected with force-throw enabled',async()=>{
2205+
// Wrapping the initial render in startTransition makes the render-phase
2206+
// setState inherit a non-sync transition lane. After commit, the next
2207+
// render is non-sync, so the loop detector classifies the recursion as
2208+
// NESTED_UPDATE_PHASE_SPAWN (rather than SYNC_LANE). With ForceThrow
2209+
// enabled, this branch should throw via throwForcedInfiniteRenderLoopError
2210+
// instead of only warning in DEV.
2211+
letsetState;
2212+
letshouldStop=false;
2213+
// Hard cap on Child renders. Without enableInfiniteRenderLoopDetection,
2214+
// the PHASE_SPAWN branch is gated off entirely, so no throw fires and
2215+
// the loop would otherwise run until the babel infinite-loop guard.
2216+
letrenderCount=0;
2217+
constRENDER_CAP=100;
2218+
functionApp(){
2219+
const[,_setState]=React.useState(0);
2220+
setState=_setState;
2221+
return<Child/>;
2222+
}
2223+
2224+
functionChild(){
2225+
if(shouldStop||renderCount>=RENDER_CAP){
2226+
returnnull;
2227+
}
2228+
renderCount++;
2229+
setState(n=>n+1);
2230+
returnnull;
2231+
}
2232+
2233+
constcontainer=document.createElement('div');
2234+
consterrors=[];
2235+
constroot=ReactDOMClient.createRoot(container,{
2236+
onUncaughtError: error=>{
2237+
errors.push(error.message);
2238+
shouldStop=true;
2239+
},
2240+
});
2241+
2242+
constoriginalConsoleError=console.error;
2243+
console.error=msg=>{
2244+
if(
2245+
typeofmsg==='string'&&
2246+
msg.startsWith('Cannot update a component')
2247+
){
2248+
return;
2249+
}
2250+
originalConsoleError(msg);
2251+
};
2252+
try{
2253+
awaitact(()=>{
2254+
React.startTransition(()=>root.render(<App/>));
2255+
});
2256+
}finally{
2257+
console.error=originalConsoleError;
2258+
}
2259+
2260+
expect(errors.length).toBeGreaterThanOrEqual(1);
2261+
expect(errors[0]).toContain(
2262+
'Maximum update depth exceeded. This could be an infinite loop.',
2263+
);
2264+
});
2265+
21182266
it('prevents infinite update loop triggered by too many updates in ref callbacks',async()=>{
21192267
letscheduleUpdate;
21202268
functionTooManyRefUpdates(){

‎packages/react-reconciler/src/ReactFiberWorkLoop.js‎

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import {
5151
disableLegacyContext,
5252
alwaysThrottleRetries,
5353
enableInfiniteRenderLoopDetection,
54+
enableInfiniteRenderLoopDetectionForceThrow,
5455
disableLegacyMode,
5556
enableComponentPerformanceTrack,
5657
enableYieldingBeforePassive,
@@ -5175,6 +5176,27 @@ export function resolveRetryWakeable(boundaryFiber: Fiber, wakeable: Wakeable) {
51755176
retryTimedOutBoundary(boundaryFiber,retryLane);
51765177
}
51775178

5179+
functionthrowForcedInfiniteRenderLoopError(
5180+
root: FiberRoot|null,
5181+
renderLanes: Lanes,
5182+
): empty{
5183+
if(root!==null){
5184+
// Disable concurrent error recovery for the in-progress render so the thrown
5185+
// error reaches the nearest error boundary and breaks the infinite update
5186+
// loop instead of being silently retried by the recovery mechanism.
5187+
root.errorRecoveryDisabledLanes=mergeLanes(
5188+
root.errorRecoveryDisabledLanes,
5189+
renderLanes,
5190+
);
5191+
}
5192+
thrownewError(
5193+
'Maximum update depth exceeded. This could be an infinite loop. This can happen when a component '+
5194+
'repeatedly calls setState during render phase or inside useLayoutEffect, '+
5195+
'causing infinite render loop. React limits the number of nested updates to '+
5196+
'prevent infinite loops.',
5197+
);
5198+
}
5199+
51785200
exportfunctionthrowIfInfiniteUpdateLoopDetected(
51795201
isFromInfiniteRenderLoopDetectionInstrumentation: boolean,
51805202
){
@@ -5191,10 +5213,16 @@ export function throwIfInfiniteUpdateLoopDetected(
51915213
if(updateKind===NESTED_UPDATE_SYNC_LANE){
51925214
if(
51935215
isFromInfiniteRenderLoopDetectionInstrumentation||
5194-
(executionContext&RenderContext&&workInProgressRoot!==null)
5216+
(executionContext&RenderContext)!==NoContext
51955217
){
5196-
// This loop was identified only because of the instrumentation gated with enableInfiniteRenderLoopDetection, warn instead of throwing.
5197-
if(__DEV__){
5218+
// This loop was identified only because of the instrumentation gated with enableInfiniteRenderLoopDetection,
5219+
// warn instead of throwing, unless enableInfiniteRenderLoopDetectionForceThrow.
5220+
if(enableInfiniteRenderLoopDetectionForceThrow){
5221+
throwForcedInfiniteRenderLoopError(
5222+
workInProgressRoot,
5223+
workInProgressRootRenderLanes,
5224+
);
5225+
}elseif(__DEV__){
51985226
console.error(
51995227
'Maximum update depth exceeded. This could be an infinite loop. This can happen when a component '+
52005228
'repeatedly calls setState during render phase or inside useLayoutEffect, '+
@@ -5211,7 +5239,12 @@ export function throwIfInfiniteUpdateLoopDetected(
52115239
);
52125240
}
52135241
}elseif(updateKind===NESTED_UPDATE_PHASE_SPAWN){
5214-
if(__DEV__){
5242+
if(enableInfiniteRenderLoopDetectionForceThrow){
5243+
throwForcedInfiniteRenderLoopError(
5244+
workInProgressRoot,
5245+
workInProgressRootRenderLanes,
5246+
);
5247+
}elseif(__DEV__){
52155248
console.error(
52165249
'Maximum update depth exceeded. This could be an infinite loop. This can happen when a component '+
52175250
'repeatedly calls setState during render phase or inside useLayoutEffect, '+

‎packages/shared/ReactFeatureFlags.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ export const transitionLaneExpirationMs = 5000;
144144
* by setState or similar outside of the component owning the state.
145145
*/
146146
exportconstenableInfiniteRenderLoopDetection: boolean=false;
147+
/**
148+
* When `enableInfiniteRenderLoopDetection` is on, forces the detection
149+
* mechanism to throw instead of only warning in cases where it would
150+
* otherwise downgrade to a warning.
151+
*/
152+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=false;
147153

148154
exportconstenableFragmentRefs: boolean=true;
149155
exportconstenableFragmentRefsScrollIntoView: boolean=true;

‎packages/shared/forks/ReactFeatureFlags.native-fb.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export const enableCreateEventHandleAPI: boolean = false;
4747
exportconstenableMoveBefore: boolean=true;
4848
exportconstenableFizzExternalRuntime: boolean=true;
4949
exportconstenableInfiniteRenderLoopDetection: boolean=false;
50+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=false;
5051
exportconstenableLegacyCache: boolean=false;
5152
exportconstenableLegacyFBSupport: boolean=false;
5253
exportconstenableLegacyHidden: boolean=false;

‎packages/shared/forks/ReactFeatureFlags.native-oss.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export const enableCreateEventHandleAPI: boolean = false;
3131
exportconstenableMoveBefore: boolean=true;
3232
exportconstenableFizzExternalRuntime: boolean=true;
3333
exportconstenableInfiniteRenderLoopDetection: boolean=false;
34+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=false;
3435
exportconstenableLegacyCache: boolean=false;
3536
exportconstenableLegacyFBSupport: boolean=false;
3637
exportconstenableLegacyHidden: boolean=false;

‎packages/shared/forks/ReactFeatureFlags.test-renderer.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export const passChildrenWhenCloningPersistedNodes: boolean = false;
5252
exportconstdisableClientCache: boolean=true;
5353

5454
exportconstenableInfiniteRenderLoopDetection: boolean=false;
55+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=false;
5556

5657
exportconstenableEagerAlternateStateNodeCleanup: boolean=true;
5758
exportconstenableEffectEventMutationPhase: boolean=false;

‎packages/shared/forks/ReactFeatureFlags.test-renderer.native-fb.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export const enableCreateEventHandleAPI = false;
2626
exportconstenableMoveBefore=false;
2727
exportconstenableFizzExternalRuntime=true;
2828
exportconstenableInfiniteRenderLoopDetection=false;
29+
exportconstenableInfiniteRenderLoopDetectionForceThrow=false;
2930
exportconstenableLegacyCache=false;
3031
exportconstenableLegacyFBSupport=false;
3132
exportconstenableLegacyHidden=false;

‎packages/shared/forks/ReactFeatureFlags.test-renderer.www.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export const passChildrenWhenCloningPersistedNodes: boolean = false;
5454
exportconstdisableClientCache: boolean=true;
5555

5656
exportconstenableInfiniteRenderLoopDetection: boolean=false;
57+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=false;
5758

5859
exportconstenableReactTestRendererWarning: boolean=false;
5960
exportconstdisableLegacyMode: boolean=true;

‎packages/shared/forks/ReactFeatureFlags.www-dynamic.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export const transitionLaneExpirationMs = 5000;
2727
exportconstenableSchedulingProfiler: boolean=__VARIANT__;
2828

2929
exportconstenableInfiniteRenderLoopDetection: boolean=__VARIANT__;
30+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=__VARIANT__;
3031

3132
exportconstenableFastAddPropertiesInDiffing: boolean=__VARIANT__;
3233
exportconstenableSuspenseyImages: boolean=__VARIANT__;

‎packages/shared/forks/ReactFeatureFlags.www.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export const {
2020
disableSchedulerTimeoutInWorkLoop,
2121
enableEffectEventMutationPhase,
2222
enableInfiniteRenderLoopDetection,
23+
enableInfiniteRenderLoopDetectionForceThrow,
2324
enableNoCloningMemoCache,
2425
enableObjectFiber,
2526
enableRetryLaneExpiration,

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { // Highlight search terms from Google/DuckDuckGo/Bing referrer (function() { var ref = document.referrer; var terms = []; if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) { var url = new URL(ref); var q = url.searchParams.get('q') || url.searchParams.get('p'); if (q) { terms = q.split(/\s+/).filter(function(t) { return t.length > 2; }); } } if (terms.length === 0) return; var style = document.createElement('style'); style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }'; document.head.appendChild(style); function highlight(node) { if (node.nodeType === 3) { // text node var text = node.textContent; var found = false; terms.forEach(function(term) { var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\]\\]/g, '\\') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' [enableInfiniteRenderLoopDetection] Add a flag to force throwing (#36… · react/react@f4e0d4e · GitHub
Skip to content

Commit f4e0d4e

Browse files
authored
[enableInfiniteRenderLoopDetection] Add a flag to force throwing (#36357)
Adds a `enableInfiniteRenderLoopDetectionForceThrow` flag, which changes the detection mechanism behaviour to start throwing, when the loop is observed. By default, the value is set to `false`, also made dynamic from the start for `www`.
1 parent ad5dfc8 commit f4e0d4e

11 files changed

Lines changed: 203 additions & 8 deletions

‎packages/react-dom/src/__tests__/ReactUpdates-test.js‎

Lines changed: 151 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,7 +1793,14 @@ describe('ReactUpdates', () => {
17931793
});
17941794

17951795
it("warns about potential infinite loop if there's a synchronous render phase update on another component",async()=>{
1796-
if(!__DEV__||gate(flags=>!flags.enableInfiniteRenderLoopDetection)){
1796+
if(
1797+
!__DEV__||
1798+
gate(
1799+
flags=>
1800+
!flags.enableInfiniteRenderLoopDetection||
1801+
flags.enableInfiniteRenderLoopDetectionForceThrow,
1802+
)
1803+
){
17971804
return;
17981805
}
17991806
letsetState;
@@ -1831,7 +1838,14 @@ describe('ReactUpdates', () => {
18311838
});
18321839

18331840
it("warns about potential infinite loop if there's an async render phase update on another component",async()=>{
1834-
if(!__DEV__||gate(flags=>!flags.enableInfiniteRenderLoopDetection)){
1841+
if(
1842+
!__DEV__||
1843+
gate(
1844+
flags=>
1845+
!flags.enableInfiniteRenderLoopDetection||
1846+
flags.enableInfiniteRenderLoopDetectionForceThrow,
1847+
)
1848+
){
18351849
return;
18361850
}
18371851
letsetState;
@@ -2007,7 +2021,14 @@ describe('ReactUpdates', () => {
20072021
});
20082022

20092023
it('warns instead of throwing when infinite Suspense ping loop is detected via enableInfiniteRenderLoopDetection during commit phase',async()=>{
2010-
if(!__DEV__||gate(flags=>!flags.enableInfiniteRenderLoopDetection)){
2024+
if(
2025+
!__DEV__||
2026+
gate(
2027+
flags=>
2028+
!flags.enableInfiniteRenderLoopDetection||
2029+
flags.enableInfiniteRenderLoopDetectionForceThrow,
2030+
)
2031+
){
20112032
return;
20122033
}
20132034

@@ -2115,6 +2136,133 @@ describe('ReactUpdates', () => {
21152136
expect(errors).toEqual([]);
21162137
});
21172138

2139+
// @gate enableInfiniteRenderLoopDetection && enableInfiniteRenderLoopDetectionForceThrow
2140+
it('throws when sync render-phase update loop is detected with force-throw enabled',async()=>{
2141+
// Render-phase setState on another component's hook produces a sync
2142+
// recursive update. With ForceThrow enabled this should throw via
2143+
// throwForcedInfiniteRenderLoopError instead of only warning in DEV.
2144+
letsetState;
2145+
letshouldStop=false;
2146+
functionApp(){
2147+
const[,_setState]=React.useState(0);
2148+
setState=_setState;
2149+
return<Child/>;
2150+
}
2151+
2152+
functionChild(){
2153+
if(shouldStop){
2154+
returnnull;
2155+
}
2156+
setState(n=>n+1);
2157+
returnnull;
2158+
}
2159+
2160+
constcontainer=document.createElement('div');
2161+
consterrors=[];
2162+
constcaptureError=error=>{
2163+
errors.push(error.message);
2164+
// Stop scheduling new updates so the test (and the gate-off variant
2165+
// where the legacy error path is recoverable) can terminate cleanly
2166+
// without tripping the babel infinite-loop guard.
2167+
shouldStop=true;
2168+
};
2169+
constroot=ReactDOMClient.createRoot(container,{
2170+
onUncaughtError: captureError,
2171+
onRecoverableError: captureError,
2172+
onCaughtError: captureError,
2173+
});
2174+
2175+
// The render-phase setState path also produces a dev-only "Cannot update
2176+
// a component while rendering a different component" console.error on
2177+
// every recursion. Swallow those so the test framework doesn't require
2178+
// us to assert each one.
2179+
constoriginalConsoleError=console.error;
2180+
console.error=msg=>{
2181+
if(
2182+
typeofmsg==='string'&&
2183+
msg.startsWith('Cannot update a component')
2184+
){
2185+
return;
2186+
}
2187+
originalConsoleError(msg);
2188+
};
2189+
try{
2190+
awaitact(()=>{
2191+
root.render(<App/>);
2192+
});
2193+
}finally{
2194+
console.error=originalConsoleError;
2195+
}
2196+
2197+
expect(errors.length).toBeGreaterThanOrEqual(1);
2198+
expect(errors[0]).toContain(
2199+
'Maximum update depth exceeded. This could be an infinite loop.',
2200+
);
2201+
});
2202+
2203+
// @gate enableInfiniteRenderLoopDetection && enableInfiniteRenderLoopDetectionForceThrow
2204+
it('throws when phase-spawn update loop is detected with force-throw enabled',async()=>{
2205+
// Wrapping the initial render in startTransition makes the render-phase
2206+
// setState inherit a non-sync transition lane. After commit, the next
2207+
// render is non-sync, so the loop detector classifies the recursion as
2208+
// NESTED_UPDATE_PHASE_SPAWN (rather than SYNC_LANE). With ForceThrow
2209+
// enabled, this branch should throw via throwForcedInfiniteRenderLoopError
2210+
// instead of only warning in DEV.
2211+
letsetState;
2212+
letshouldStop=false;
2213+
// Hard cap on Child renders. Without enableInfiniteRenderLoopDetection,
2214+
// the PHASE_SPAWN branch is gated off entirely, so no throw fires and
2215+
// the loop would otherwise run until the babel infinite-loop guard.
2216+
letrenderCount=0;
2217+
constRENDER_CAP=100;
2218+
functionApp(){
2219+
const[,_setState]=React.useState(0);
2220+
setState=_setState;
2221+
return<Child/>;
2222+
}
2223+
2224+
functionChild(){
2225+
if(shouldStop||renderCount>=RENDER_CAP){
2226+
returnnull;
2227+
}
2228+
renderCount++;
2229+
setState(n=>n+1);
2230+
returnnull;
2231+
}
2232+
2233+
constcontainer=document.createElement('div');
2234+
consterrors=[];
2235+
constroot=ReactDOMClient.createRoot(container,{
2236+
onUncaughtError: error=>{
2237+
errors.push(error.message);
2238+
shouldStop=true;
2239+
},
2240+
});
2241+
2242+
constoriginalConsoleError=console.error;
2243+
console.error=msg=>{
2244+
if(
2245+
typeofmsg==='string'&&
2246+
msg.startsWith('Cannot update a component')
2247+
){
2248+
return;
2249+
}
2250+
originalConsoleError(msg);
2251+
};
2252+
try{
2253+
awaitact(()=>{
2254+
React.startTransition(()=>root.render(<App/>));
2255+
});
2256+
}finally{
2257+
console.error=originalConsoleError;
2258+
}
2259+
2260+
expect(errors.length).toBeGreaterThanOrEqual(1);
2261+
expect(errors[0]).toContain(
2262+
'Maximum update depth exceeded. This could be an infinite loop.',
2263+
);
2264+
});
2265+
21182266
it('prevents infinite update loop triggered by too many updates in ref callbacks',async()=>{
21192267
letscheduleUpdate;
21202268
functionTooManyRefUpdates(){

‎packages/react-reconciler/src/ReactFiberWorkLoop.js‎

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import {
5151
disableLegacyContext,
5252
alwaysThrottleRetries,
5353
enableInfiniteRenderLoopDetection,
54+
enableInfiniteRenderLoopDetectionForceThrow,
5455
disableLegacyMode,
5556
enableComponentPerformanceTrack,
5657
enableYieldingBeforePassive,
@@ -5175,6 +5176,27 @@ export function resolveRetryWakeable(boundaryFiber: Fiber, wakeable: Wakeable) {
51755176
retryTimedOutBoundary(boundaryFiber,retryLane);
51765177
}
51775178

5179+
functionthrowForcedInfiniteRenderLoopError(
5180+
root: FiberRoot|null,
5181+
renderLanes: Lanes,
5182+
): empty{
5183+
if(root!==null){
5184+
// Disable concurrent error recovery for the in-progress render so the thrown
5185+
// error reaches the nearest error boundary and breaks the infinite update
5186+
// loop instead of being silently retried by the recovery mechanism.
5187+
root.errorRecoveryDisabledLanes=mergeLanes(
5188+
root.errorRecoveryDisabledLanes,
5189+
renderLanes,
5190+
);
5191+
}
5192+
thrownewError(
5193+
'Maximum update depth exceeded. This could be an infinite loop. This can happen when a component '+
5194+
'repeatedly calls setState during render phase or inside useLayoutEffect, '+
5195+
'causing infinite render loop. React limits the number of nested updates to '+
5196+
'prevent infinite loops.',
5197+
);
5198+
}
5199+
51785200
exportfunctionthrowIfInfiniteUpdateLoopDetected(
51795201
isFromInfiniteRenderLoopDetectionInstrumentation: boolean,
51805202
){
@@ -5191,10 +5213,16 @@ export function throwIfInfiniteUpdateLoopDetected(
51915213
if(updateKind===NESTED_UPDATE_SYNC_LANE){
51925214
if(
51935215
isFromInfiniteRenderLoopDetectionInstrumentation||
5194-
(executionContext&RenderContext&&workInProgressRoot!==null)
5216+
(executionContext&RenderContext)!==NoContext
51955217
){
5196-
// This loop was identified only because of the instrumentation gated with enableInfiniteRenderLoopDetection, warn instead of throwing.
5197-
if(__DEV__){
5218+
// This loop was identified only because of the instrumentation gated with enableInfiniteRenderLoopDetection,
5219+
// warn instead of throwing, unless enableInfiniteRenderLoopDetectionForceThrow.
5220+
if(enableInfiniteRenderLoopDetectionForceThrow){
5221+
throwForcedInfiniteRenderLoopError(
5222+
workInProgressRoot,
5223+
workInProgressRootRenderLanes,
5224+
);
5225+
}elseif(__DEV__){
51985226
console.error(
51995227
'Maximum update depth exceeded. This could be an infinite loop. This can happen when a component '+
52005228
'repeatedly calls setState during render phase or inside useLayoutEffect, '+
@@ -5211,7 +5239,12 @@ export function throwIfInfiniteUpdateLoopDetected(
52115239
);
52125240
}
52135241
}elseif(updateKind===NESTED_UPDATE_PHASE_SPAWN){
5214-
if(__DEV__){
5242+
if(enableInfiniteRenderLoopDetectionForceThrow){
5243+
throwForcedInfiniteRenderLoopError(
5244+
workInProgressRoot,
5245+
workInProgressRootRenderLanes,
5246+
);
5247+
}elseif(__DEV__){
52155248
console.error(
52165249
'Maximum update depth exceeded. This could be an infinite loop. This can happen when a component '+
52175250
'repeatedly calls setState during render phase or inside useLayoutEffect, '+

‎packages/shared/ReactFeatureFlags.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ export const transitionLaneExpirationMs = 5000;
144144
* by setState or similar outside of the component owning the state.
145145
*/
146146
exportconstenableInfiniteRenderLoopDetection: boolean=false;
147+
/**
148+
* When `enableInfiniteRenderLoopDetection` is on, forces the detection
149+
* mechanism to throw instead of only warning in cases where it would
150+
* otherwise downgrade to a warning.
151+
*/
152+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=false;
147153

148154
exportconstenableFragmentRefs: boolean=true;
149155
exportconstenableFragmentRefsScrollIntoView: boolean=true;

‎packages/shared/forks/ReactFeatureFlags.native-fb.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export const enableCreateEventHandleAPI: boolean = false;
4747
exportconstenableMoveBefore: boolean=true;
4848
exportconstenableFizzExternalRuntime: boolean=true;
4949
exportconstenableInfiniteRenderLoopDetection: boolean=false;
50+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=false;
5051
exportconstenableLegacyCache: boolean=false;
5152
exportconstenableLegacyFBSupport: boolean=false;
5253
exportconstenableLegacyHidden: boolean=false;

‎packages/shared/forks/ReactFeatureFlags.native-oss.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export const enableCreateEventHandleAPI: boolean = false;
3131
exportconstenableMoveBefore: boolean=true;
3232
exportconstenableFizzExternalRuntime: boolean=true;
3333
exportconstenableInfiniteRenderLoopDetection: boolean=false;
34+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=false;
3435
exportconstenableLegacyCache: boolean=false;
3536
exportconstenableLegacyFBSupport: boolean=false;
3637
exportconstenableLegacyHidden: boolean=false;

‎packages/shared/forks/ReactFeatureFlags.test-renderer.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export const passChildrenWhenCloningPersistedNodes: boolean = false;
5252
exportconstdisableClientCache: boolean=true;
5353

5454
exportconstenableInfiniteRenderLoopDetection: boolean=false;
55+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=false;
5556

5657
exportconstenableEagerAlternateStateNodeCleanup: boolean=true;
5758
exportconstenableEffectEventMutationPhase: boolean=false;

‎packages/shared/forks/ReactFeatureFlags.test-renderer.native-fb.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export const enableCreateEventHandleAPI = false;
2626
exportconstenableMoveBefore=false;
2727
exportconstenableFizzExternalRuntime=true;
2828
exportconstenableInfiniteRenderLoopDetection=false;
29+
exportconstenableInfiniteRenderLoopDetectionForceThrow=false;
2930
exportconstenableLegacyCache=false;
3031
exportconstenableLegacyFBSupport=false;
3132
exportconstenableLegacyHidden=false;

‎packages/shared/forks/ReactFeatureFlags.test-renderer.www.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export const passChildrenWhenCloningPersistedNodes: boolean = false;
5454
exportconstdisableClientCache: boolean=true;
5555

5656
exportconstenableInfiniteRenderLoopDetection: boolean=false;
57+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=false;
5758

5859
exportconstenableReactTestRendererWarning: boolean=false;
5960
exportconstdisableLegacyMode: boolean=true;

‎packages/shared/forks/ReactFeatureFlags.www-dynamic.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export const transitionLaneExpirationMs = 5000;
2727
exportconstenableSchedulingProfiler: boolean=__VARIANT__;
2828

2929
exportconstenableInfiniteRenderLoopDetection: boolean=__VARIANT__;
30+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=__VARIANT__;
3031

3132
exportconstenableFastAddPropertiesInDiffing: boolean=__VARIANT__;
3233
exportconstenableSuspenseyImages: boolean=__VARIANT__;

‎packages/shared/forks/ReactFeatureFlags.www.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export const {
2020
disableSchedulerTimeoutInWorkLoop,
2121
enableEffectEventMutationPhase,
2222
enableInfiniteRenderLoopDetection,
23+
enableInfiniteRenderLoopDetectionForceThrow,
2324
enableNoCloningMemoCache,
2425
enableObjectFiber,
2526
enableRetryLaneExpiration,

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ' [enableInfiniteRenderLoopDetection] Add a flag to force throwing (#36… · react/react@f4e0d4e · GitHub
Skip to content

Commit f4e0d4e

Browse files
authored
[enableInfiniteRenderLoopDetection] Add a flag to force throwing (#36357)
Adds a `enableInfiniteRenderLoopDetectionForceThrow` flag, which changes the detection mechanism behaviour to start throwing, when the loop is observed. By default, the value is set to `false`, also made dynamic from the start for `www`.
1 parent ad5dfc8 commit f4e0d4e

11 files changed

Lines changed: 203 additions & 8 deletions

‎packages/react-dom/src/__tests__/ReactUpdates-test.js‎

Lines changed: 151 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,7 +1793,14 @@ describe('ReactUpdates', () => {
17931793
});
17941794

17951795
it("warns about potential infinite loop if there's a synchronous render phase update on another component",async()=>{
1796-
if(!__DEV__||gate(flags=>!flags.enableInfiniteRenderLoopDetection)){
1796+
if(
1797+
!__DEV__||
1798+
gate(
1799+
flags=>
1800+
!flags.enableInfiniteRenderLoopDetection||
1801+
flags.enableInfiniteRenderLoopDetectionForceThrow,
1802+
)
1803+
){
17971804
return;
17981805
}
17991806
letsetState;
@@ -1831,7 +1838,14 @@ describe('ReactUpdates', () => {
18311838
});
18321839

18331840
it("warns about potential infinite loop if there's an async render phase update on another component",async()=>{
1834-
if(!__DEV__||gate(flags=>!flags.enableInfiniteRenderLoopDetection)){
1841+
if(
1842+
!__DEV__||
1843+
gate(
1844+
flags=>
1845+
!flags.enableInfiniteRenderLoopDetection||
1846+
flags.enableInfiniteRenderLoopDetectionForceThrow,
1847+
)
1848+
){
18351849
return;
18361850
}
18371851
letsetState;
@@ -2007,7 +2021,14 @@ describe('ReactUpdates', () => {
20072021
});
20082022

20092023
it('warns instead of throwing when infinite Suspense ping loop is detected via enableInfiniteRenderLoopDetection during commit phase',async()=>{
2010-
if(!__DEV__||gate(flags=>!flags.enableInfiniteRenderLoopDetection)){
2024+
if(
2025+
!__DEV__||
2026+
gate(
2027+
flags=>
2028+
!flags.enableInfiniteRenderLoopDetection||
2029+
flags.enableInfiniteRenderLoopDetectionForceThrow,
2030+
)
2031+
){
20112032
return;
20122033
}
20132034

@@ -2115,6 +2136,133 @@ describe('ReactUpdates', () => {
21152136
expect(errors).toEqual([]);
21162137
});
21172138

2139+
// @gate enableInfiniteRenderLoopDetection && enableInfiniteRenderLoopDetectionForceThrow
2140+
it('throws when sync render-phase update loop is detected with force-throw enabled',async()=>{
2141+
// Render-phase setState on another component's hook produces a sync
2142+
// recursive update. With ForceThrow enabled this should throw via
2143+
// throwForcedInfiniteRenderLoopError instead of only warning in DEV.
2144+
letsetState;
2145+
letshouldStop=false;
2146+
functionApp(){
2147+
const[,_setState]=React.useState(0);
2148+
setState=_setState;
2149+
return<Child/>;
2150+
}
2151+
2152+
functionChild(){
2153+
if(shouldStop){
2154+
returnnull;
2155+
}
2156+
setState(n=>n+1);
2157+
returnnull;
2158+
}
2159+
2160+
constcontainer=document.createElement('div');
2161+
consterrors=[];
2162+
constcaptureError=error=>{
2163+
errors.push(error.message);
2164+
// Stop scheduling new updates so the test (and the gate-off variant
2165+
// where the legacy error path is recoverable) can terminate cleanly
2166+
// without tripping the babel infinite-loop guard.
2167+
shouldStop=true;
2168+
};
2169+
constroot=ReactDOMClient.createRoot(container,{
2170+
onUncaughtError: captureError,
2171+
onRecoverableError: captureError,
2172+
onCaughtError: captureError,
2173+
});
2174+
2175+
// The render-phase setState path also produces a dev-only "Cannot update
2176+
// a component while rendering a different component" console.error on
2177+
// every recursion. Swallow those so the test framework doesn't require
2178+
// us to assert each one.
2179+
constoriginalConsoleError=console.error;
2180+
console.error=msg=>{
2181+
if(
2182+
typeofmsg==='string'&&
2183+
msg.startsWith('Cannot update a component')
2184+
){
2185+
return;
2186+
}
2187+
originalConsoleError(msg);
2188+
};
2189+
try{
2190+
awaitact(()=>{
2191+
root.render(<App/>);
2192+
});
2193+
}finally{
2194+
console.error=originalConsoleError;
2195+
}
2196+
2197+
expect(errors.length).toBeGreaterThanOrEqual(1);
2198+
expect(errors[0]).toContain(
2199+
'Maximum update depth exceeded. This could be an infinite loop.',
2200+
);
2201+
});
2202+
2203+
// @gate enableInfiniteRenderLoopDetection && enableInfiniteRenderLoopDetectionForceThrow
2204+
it('throws when phase-spawn update loop is detected with force-throw enabled',async()=>{
2205+
// Wrapping the initial render in startTransition makes the render-phase
2206+
// setState inherit a non-sync transition lane. After commit, the next
2207+
// render is non-sync, so the loop detector classifies the recursion as
2208+
// NESTED_UPDATE_PHASE_SPAWN (rather than SYNC_LANE). With ForceThrow
2209+
// enabled, this branch should throw via throwForcedInfiniteRenderLoopError
2210+
// instead of only warning in DEV.
2211+
letsetState;
2212+
letshouldStop=false;
2213+
// Hard cap on Child renders. Without enableInfiniteRenderLoopDetection,
2214+
// the PHASE_SPAWN branch is gated off entirely, so no throw fires and
2215+
// the loop would otherwise run until the babel infinite-loop guard.
2216+
letrenderCount=0;
2217+
constRENDER_CAP=100;
2218+
functionApp(){
2219+
const[,_setState]=React.useState(0);
2220+
setState=_setState;
2221+
return<Child/>;
2222+
}
2223+
2224+
functionChild(){
2225+
if(shouldStop||renderCount>=RENDER_CAP){
2226+
returnnull;
2227+
}
2228+
renderCount++;
2229+
setState(n=>n+1);
2230+
returnnull;
2231+
}
2232+
2233+
constcontainer=document.createElement('div');
2234+
consterrors=[];
2235+
constroot=ReactDOMClient.createRoot(container,{
2236+
onUncaughtError: error=>{
2237+
errors.push(error.message);
2238+
shouldStop=true;
2239+
},
2240+
});
2241+
2242+
constoriginalConsoleError=console.error;
2243+
console.error=msg=>{
2244+
if(
2245+
typeofmsg==='string'&&
2246+
msg.startsWith('Cannot update a component')
2247+
){
2248+
return;
2249+
}
2250+
originalConsoleError(msg);
2251+
};
2252+
try{
2253+
awaitact(()=>{
2254+
React.startTransition(()=>root.render(<App/>));
2255+
});
2256+
}finally{
2257+
console.error=originalConsoleError;
2258+
}
2259+
2260+
expect(errors.length).toBeGreaterThanOrEqual(1);
2261+
expect(errors[0]).toContain(
2262+
'Maximum update depth exceeded. This could be an infinite loop.',
2263+
);
2264+
});
2265+
21182266
it('prevents infinite update loop triggered by too many updates in ref callbacks',async()=>{
21192267
letscheduleUpdate;
21202268
functionTooManyRefUpdates(){

‎packages/react-reconciler/src/ReactFiberWorkLoop.js‎

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import {
5151
disableLegacyContext,
5252
alwaysThrottleRetries,
5353
enableInfiniteRenderLoopDetection,
54+
enableInfiniteRenderLoopDetectionForceThrow,
5455
disableLegacyMode,
5556
enableComponentPerformanceTrack,
5657
enableYieldingBeforePassive,
@@ -5175,6 +5176,27 @@ export function resolveRetryWakeable(boundaryFiber: Fiber, wakeable: Wakeable) {
51755176
retryTimedOutBoundary(boundaryFiber,retryLane);
51765177
}
51775178

5179+
functionthrowForcedInfiniteRenderLoopError(
5180+
root: FiberRoot|null,
5181+
renderLanes: Lanes,
5182+
): empty{
5183+
if(root!==null){
5184+
// Disable concurrent error recovery for the in-progress render so the thrown
5185+
// error reaches the nearest error boundary and breaks the infinite update
5186+
// loop instead of being silently retried by the recovery mechanism.
5187+
root.errorRecoveryDisabledLanes=mergeLanes(
5188+
root.errorRecoveryDisabledLanes,
5189+
renderLanes,
5190+
);
5191+
}
5192+
thrownewError(
5193+
'Maximum update depth exceeded. This could be an infinite loop. This can happen when a component '+
5194+
'repeatedly calls setState during render phase or inside useLayoutEffect, '+
5195+
'causing infinite render loop. React limits the number of nested updates to '+
5196+
'prevent infinite loops.',
5197+
);
5198+
}
5199+
51785200
exportfunctionthrowIfInfiniteUpdateLoopDetected(
51795201
isFromInfiniteRenderLoopDetectionInstrumentation: boolean,
51805202
){
@@ -5191,10 +5213,16 @@ export function throwIfInfiniteUpdateLoopDetected(
51915213
if(updateKind===NESTED_UPDATE_SYNC_LANE){
51925214
if(
51935215
isFromInfiniteRenderLoopDetectionInstrumentation||
5194-
(executionContext&RenderContext&&workInProgressRoot!==null)
5216+
(executionContext&RenderContext)!==NoContext
51955217
){
5196-
// This loop was identified only because of the instrumentation gated with enableInfiniteRenderLoopDetection, warn instead of throwing.
5197-
if(__DEV__){
5218+
// This loop was identified only because of the instrumentation gated with enableInfiniteRenderLoopDetection,
5219+
// warn instead of throwing, unless enableInfiniteRenderLoopDetectionForceThrow.
5220+
if(enableInfiniteRenderLoopDetectionForceThrow){
5221+
throwForcedInfiniteRenderLoopError(
5222+
workInProgressRoot,
5223+
workInProgressRootRenderLanes,
5224+
);
5225+
}elseif(__DEV__){
51985226
console.error(
51995227
'Maximum update depth exceeded. This could be an infinite loop. This can happen when a component '+
52005228
'repeatedly calls setState during render phase or inside useLayoutEffect, '+
@@ -5211,7 +5239,12 @@ export function throwIfInfiniteUpdateLoopDetected(
52115239
);
52125240
}
52135241
}elseif(updateKind===NESTED_UPDATE_PHASE_SPAWN){
5214-
if(__DEV__){
5242+
if(enableInfiniteRenderLoopDetectionForceThrow){
5243+
throwForcedInfiniteRenderLoopError(
5244+
workInProgressRoot,
5245+
workInProgressRootRenderLanes,
5246+
);
5247+
}elseif(__DEV__){
52155248
console.error(
52165249
'Maximum update depth exceeded. This could be an infinite loop. This can happen when a component '+
52175250
'repeatedly calls setState during render phase or inside useLayoutEffect, '+

‎packages/shared/ReactFeatureFlags.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ export const transitionLaneExpirationMs = 5000;
144144
* by setState or similar outside of the component owning the state.
145145
*/
146146
exportconstenableInfiniteRenderLoopDetection: boolean=false;
147+
/**
148+
* When `enableInfiniteRenderLoopDetection` is on, forces the detection
149+
* mechanism to throw instead of only warning in cases where it would
150+
* otherwise downgrade to a warning.
151+
*/
152+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=false;
147153

148154
exportconstenableFragmentRefs: boolean=true;
149155
exportconstenableFragmentRefsScrollIntoView: boolean=true;

‎packages/shared/forks/ReactFeatureFlags.native-fb.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export const enableCreateEventHandleAPI: boolean = false;
4747
exportconstenableMoveBefore: boolean=true;
4848
exportconstenableFizzExternalRuntime: boolean=true;
4949
exportconstenableInfiniteRenderLoopDetection: boolean=false;
50+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=false;
5051
exportconstenableLegacyCache: boolean=false;
5152
exportconstenableLegacyFBSupport: boolean=false;
5253
exportconstenableLegacyHidden: boolean=false;

‎packages/shared/forks/ReactFeatureFlags.native-oss.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export const enableCreateEventHandleAPI: boolean = false;
3131
exportconstenableMoveBefore: boolean=true;
3232
exportconstenableFizzExternalRuntime: boolean=true;
3333
exportconstenableInfiniteRenderLoopDetection: boolean=false;
34+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=false;
3435
exportconstenableLegacyCache: boolean=false;
3536
exportconstenableLegacyFBSupport: boolean=false;
3637
exportconstenableLegacyHidden: boolean=false;

‎packages/shared/forks/ReactFeatureFlags.test-renderer.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export const passChildrenWhenCloningPersistedNodes: boolean = false;
5252
exportconstdisableClientCache: boolean=true;
5353

5454
exportconstenableInfiniteRenderLoopDetection: boolean=false;
55+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=false;
5556

5657
exportconstenableEagerAlternateStateNodeCleanup: boolean=true;
5758
exportconstenableEffectEventMutationPhase: boolean=false;

‎packages/shared/forks/ReactFeatureFlags.test-renderer.native-fb.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export const enableCreateEventHandleAPI = false;
2626
exportconstenableMoveBefore=false;
2727
exportconstenableFizzExternalRuntime=true;
2828
exportconstenableInfiniteRenderLoopDetection=false;
29+
exportconstenableInfiniteRenderLoopDetectionForceThrow=false;
2930
exportconstenableLegacyCache=false;
3031
exportconstenableLegacyFBSupport=false;
3132
exportconstenableLegacyHidden=false;

‎packages/shared/forks/ReactFeatureFlags.test-renderer.www.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export const passChildrenWhenCloningPersistedNodes: boolean = false;
5454
exportconstdisableClientCache: boolean=true;
5555

5656
exportconstenableInfiniteRenderLoopDetection: boolean=false;
57+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=false;
5758

5859
exportconstenableReactTestRendererWarning: boolean=false;
5960
exportconstdisableLegacyMode: boolean=true;

‎packages/shared/forks/ReactFeatureFlags.www-dynamic.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export const transitionLaneExpirationMs = 5000;
2727
exportconstenableSchedulingProfiler: boolean=__VARIANT__;
2828

2929
exportconstenableInfiniteRenderLoopDetection: boolean=__VARIANT__;
30+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=__VARIANT__;
3031

3132
exportconstenableFastAddPropertiesInDiffing: boolean=__VARIANT__;
3233
exportconstenableSuspenseyImages: boolean=__VARIANT__;

‎packages/shared/forks/ReactFeatureFlags.www.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export const {
2020
disableSchedulerTimeoutInWorkLoop,
2121
enableEffectEventMutationPhase,
2222
enableInfiniteRenderLoopDetection,
23+
enableInfiniteRenderLoopDetectionForceThrow,
2324
enableNoCloningMemoCache,
2425
enableObjectFiber,
2526
enableRetryLaneExpiration,

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' [enableInfiniteRenderLoopDetection] Add a flag to force throwing (#36… · react/react@f4e0d4e · GitHub
Skip to content

Commit f4e0d4e

Browse files
authored
[enableInfiniteRenderLoopDetection] Add a flag to force throwing (#36357)
Adds a `enableInfiniteRenderLoopDetectionForceThrow` flag, which changes the detection mechanism behaviour to start throwing, when the loop is observed. By default, the value is set to `false`, also made dynamic from the start for `www`.
1 parent ad5dfc8 commit f4e0d4e

11 files changed

Lines changed: 203 additions & 8 deletions

‎packages/react-dom/src/__tests__/ReactUpdates-test.js‎

Lines changed: 151 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,7 +1793,14 @@ describe('ReactUpdates', () => {
17931793
});
17941794

17951795
it("warns about potential infinite loop if there's a synchronous render phase update on another component",async()=>{
1796-
if(!__DEV__||gate(flags=>!flags.enableInfiniteRenderLoopDetection)){
1796+
if(
1797+
!__DEV__||
1798+
gate(
1799+
flags=>
1800+
!flags.enableInfiniteRenderLoopDetection||
1801+
flags.enableInfiniteRenderLoopDetectionForceThrow,
1802+
)
1803+
){
17971804
return;
17981805
}
17991806
letsetState;
@@ -1831,7 +1838,14 @@ describe('ReactUpdates', () => {
18311838
});
18321839

18331840
it("warns about potential infinite loop if there's an async render phase update on another component",async()=>{
1834-
if(!__DEV__||gate(flags=>!flags.enableInfiniteRenderLoopDetection)){
1841+
if(
1842+
!__DEV__||
1843+
gate(
1844+
flags=>
1845+
!flags.enableInfiniteRenderLoopDetection||
1846+
flags.enableInfiniteRenderLoopDetectionForceThrow,
1847+
)
1848+
){
18351849
return;
18361850
}
18371851
letsetState;
@@ -2007,7 +2021,14 @@ describe('ReactUpdates', () => {
20072021
});
20082022

20092023
it('warns instead of throwing when infinite Suspense ping loop is detected via enableInfiniteRenderLoopDetection during commit phase',async()=>{
2010-
if(!__DEV__||gate(flags=>!flags.enableInfiniteRenderLoopDetection)){
2024+
if(
2025+
!__DEV__||
2026+
gate(
2027+
flags=>
2028+
!flags.enableInfiniteRenderLoopDetection||
2029+
flags.enableInfiniteRenderLoopDetectionForceThrow,
2030+
)
2031+
){
20112032
return;
20122033
}
20132034

@@ -2115,6 +2136,133 @@ describe('ReactUpdates', () => {
21152136
expect(errors).toEqual([]);
21162137
});
21172138

2139+
// @gate enableInfiniteRenderLoopDetection && enableInfiniteRenderLoopDetectionForceThrow
2140+
it('throws when sync render-phase update loop is detected with force-throw enabled',async()=>{
2141+
// Render-phase setState on another component's hook produces a sync
2142+
// recursive update. With ForceThrow enabled this should throw via
2143+
// throwForcedInfiniteRenderLoopError instead of only warning in DEV.
2144+
letsetState;
2145+
letshouldStop=false;
2146+
functionApp(){
2147+
const[,_setState]=React.useState(0);
2148+
setState=_setState;
2149+
return<Child/>;
2150+
}
2151+
2152+
functionChild(){
2153+
if(shouldStop){
2154+
returnnull;
2155+
}
2156+
setState(n=>n+1);
2157+
returnnull;
2158+
}
2159+
2160+
constcontainer=document.createElement('div');
2161+
consterrors=[];
2162+
constcaptureError=error=>{
2163+
errors.push(error.message);
2164+
// Stop scheduling new updates so the test (and the gate-off variant
2165+
// where the legacy error path is recoverable) can terminate cleanly
2166+
// without tripping the babel infinite-loop guard.
2167+
shouldStop=true;
2168+
};
2169+
constroot=ReactDOMClient.createRoot(container,{
2170+
onUncaughtError: captureError,
2171+
onRecoverableError: captureError,
2172+
onCaughtError: captureError,
2173+
});
2174+
2175+
// The render-phase setState path also produces a dev-only "Cannot update
2176+
// a component while rendering a different component" console.error on
2177+
// every recursion. Swallow those so the test framework doesn't require
2178+
// us to assert each one.
2179+
constoriginalConsoleError=console.error;
2180+
console.error=msg=>{
2181+
if(
2182+
typeofmsg==='string'&&
2183+
msg.startsWith('Cannot update a component')
2184+
){
2185+
return;
2186+
}
2187+
originalConsoleError(msg);
2188+
};
2189+
try{
2190+
awaitact(()=>{
2191+
root.render(<App/>);
2192+
});
2193+
}finally{
2194+
console.error=originalConsoleError;
2195+
}
2196+
2197+
expect(errors.length).toBeGreaterThanOrEqual(1);
2198+
expect(errors[0]).toContain(
2199+
'Maximum update depth exceeded. This could be an infinite loop.',
2200+
);
2201+
});
2202+
2203+
// @gate enableInfiniteRenderLoopDetection && enableInfiniteRenderLoopDetectionForceThrow
2204+
it('throws when phase-spawn update loop is detected with force-throw enabled',async()=>{
2205+
// Wrapping the initial render in startTransition makes the render-phase
2206+
// setState inherit a non-sync transition lane. After commit, the next
2207+
// render is non-sync, so the loop detector classifies the recursion as
2208+
// NESTED_UPDATE_PHASE_SPAWN (rather than SYNC_LANE). With ForceThrow
2209+
// enabled, this branch should throw via throwForcedInfiniteRenderLoopError
2210+
// instead of only warning in DEV.
2211+
letsetState;
2212+
letshouldStop=false;
2213+
// Hard cap on Child renders. Without enableInfiniteRenderLoopDetection,
2214+
// the PHASE_SPAWN branch is gated off entirely, so no throw fires and
2215+
// the loop would otherwise run until the babel infinite-loop guard.
2216+
letrenderCount=0;
2217+
constRENDER_CAP=100;
2218+
functionApp(){
2219+
const[,_setState]=React.useState(0);
2220+
setState=_setState;
2221+
return<Child/>;
2222+
}
2223+
2224+
functionChild(){
2225+
if(shouldStop||renderCount>=RENDER_CAP){
2226+
returnnull;
2227+
}
2228+
renderCount++;
2229+
setState(n=>n+1);
2230+
returnnull;
2231+
}
2232+
2233+
constcontainer=document.createElement('div');
2234+
consterrors=[];
2235+
constroot=ReactDOMClient.createRoot(container,{
2236+
onUncaughtError: error=>{
2237+
errors.push(error.message);
2238+
shouldStop=true;
2239+
},
2240+
});
2241+
2242+
constoriginalConsoleError=console.error;
2243+
console.error=msg=>{
2244+
if(
2245+
typeofmsg==='string'&&
2246+
msg.startsWith('Cannot update a component')
2247+
){
2248+
return;
2249+
}
2250+
originalConsoleError(msg);
2251+
};
2252+
try{
2253+
awaitact(()=>{
2254+
React.startTransition(()=>root.render(<App/>));
2255+
});
2256+
}finally{
2257+
console.error=originalConsoleError;
2258+
}
2259+
2260+
expect(errors.length).toBeGreaterThanOrEqual(1);
2261+
expect(errors[0]).toContain(
2262+
'Maximum update depth exceeded. This could be an infinite loop.',
2263+
);
2264+
});
2265+
21182266
it('prevents infinite update loop triggered by too many updates in ref callbacks',async()=>{
21192267
letscheduleUpdate;
21202268
functionTooManyRefUpdates(){

‎packages/react-reconciler/src/ReactFiberWorkLoop.js‎

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import {
5151
disableLegacyContext,
5252
alwaysThrottleRetries,
5353
enableInfiniteRenderLoopDetection,
54+
enableInfiniteRenderLoopDetectionForceThrow,
5455
disableLegacyMode,
5556
enableComponentPerformanceTrack,
5657
enableYieldingBeforePassive,
@@ -5175,6 +5176,27 @@ export function resolveRetryWakeable(boundaryFiber: Fiber, wakeable: Wakeable) {
51755176
retryTimedOutBoundary(boundaryFiber,retryLane);
51765177
}
51775178

5179+
functionthrowForcedInfiniteRenderLoopError(
5180+
root: FiberRoot|null,
5181+
renderLanes: Lanes,
5182+
): empty{
5183+
if(root!==null){
5184+
// Disable concurrent error recovery for the in-progress render so the thrown
5185+
// error reaches the nearest error boundary and breaks the infinite update
5186+
// loop instead of being silently retried by the recovery mechanism.
5187+
root.errorRecoveryDisabledLanes=mergeLanes(
5188+
root.errorRecoveryDisabledLanes,
5189+
renderLanes,
5190+
);
5191+
}
5192+
thrownewError(
5193+
'Maximum update depth exceeded. This could be an infinite loop. This can happen when a component '+
5194+
'repeatedly calls setState during render phase or inside useLayoutEffect, '+
5195+
'causing infinite render loop. React limits the number of nested updates to '+
5196+
'prevent infinite loops.',
5197+
);
5198+
}
5199+
51785200
exportfunctionthrowIfInfiniteUpdateLoopDetected(
51795201
isFromInfiniteRenderLoopDetectionInstrumentation: boolean,
51805202
){
@@ -5191,10 +5213,16 @@ export function throwIfInfiniteUpdateLoopDetected(
51915213
if(updateKind===NESTED_UPDATE_SYNC_LANE){
51925214
if(
51935215
isFromInfiniteRenderLoopDetectionInstrumentation||
5194-
(executionContext&RenderContext&&workInProgressRoot!==null)
5216+
(executionContext&RenderContext)!==NoContext
51955217
){
5196-
// This loop was identified only because of the instrumentation gated with enableInfiniteRenderLoopDetection, warn instead of throwing.
5197-
if(__DEV__){
5218+
// This loop was identified only because of the instrumentation gated with enableInfiniteRenderLoopDetection,
5219+
// warn instead of throwing, unless enableInfiniteRenderLoopDetectionForceThrow.
5220+
if(enableInfiniteRenderLoopDetectionForceThrow){
5221+
throwForcedInfiniteRenderLoopError(
5222+
workInProgressRoot,
5223+
workInProgressRootRenderLanes,
5224+
);
5225+
}elseif(__DEV__){
51985226
console.error(
51995227
'Maximum update depth exceeded. This could be an infinite loop. This can happen when a component '+
52005228
'repeatedly calls setState during render phase or inside useLayoutEffect, '+
@@ -5211,7 +5239,12 @@ export function throwIfInfiniteUpdateLoopDetected(
52115239
);
52125240
}
52135241
}elseif(updateKind===NESTED_UPDATE_PHASE_SPAWN){
5214-
if(__DEV__){
5242+
if(enableInfiniteRenderLoopDetectionForceThrow){
5243+
throwForcedInfiniteRenderLoopError(
5244+
workInProgressRoot,
5245+
workInProgressRootRenderLanes,
5246+
);
5247+
}elseif(__DEV__){
52155248
console.error(
52165249
'Maximum update depth exceeded. This could be an infinite loop. This can happen when a component '+
52175250
'repeatedly calls setState during render phase or inside useLayoutEffect, '+

‎packages/shared/ReactFeatureFlags.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ export const transitionLaneExpirationMs = 5000;
144144
* by setState or similar outside of the component owning the state.
145145
*/
146146
exportconstenableInfiniteRenderLoopDetection: boolean=false;
147+
/**
148+
* When `enableInfiniteRenderLoopDetection` is on, forces the detection
149+
* mechanism to throw instead of only warning in cases where it would
150+
* otherwise downgrade to a warning.
151+
*/
152+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=false;
147153

148154
exportconstenableFragmentRefs: boolean=true;
149155
exportconstenableFragmentRefsScrollIntoView: boolean=true;

‎packages/shared/forks/ReactFeatureFlags.native-fb.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export const enableCreateEventHandleAPI: boolean = false;
4747
exportconstenableMoveBefore: boolean=true;
4848
exportconstenableFizzExternalRuntime: boolean=true;
4949
exportconstenableInfiniteRenderLoopDetection: boolean=false;
50+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=false;
5051
exportconstenableLegacyCache: boolean=false;
5152
exportconstenableLegacyFBSupport: boolean=false;
5253
exportconstenableLegacyHidden: boolean=false;

‎packages/shared/forks/ReactFeatureFlags.native-oss.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export const enableCreateEventHandleAPI: boolean = false;
3131
exportconstenableMoveBefore: boolean=true;
3232
exportconstenableFizzExternalRuntime: boolean=true;
3333
exportconstenableInfiniteRenderLoopDetection: boolean=false;
34+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=false;
3435
exportconstenableLegacyCache: boolean=false;
3536
exportconstenableLegacyFBSupport: boolean=false;
3637
exportconstenableLegacyHidden: boolean=false;

‎packages/shared/forks/ReactFeatureFlags.test-renderer.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export const passChildrenWhenCloningPersistedNodes: boolean = false;
5252
exportconstdisableClientCache: boolean=true;
5353

5454
exportconstenableInfiniteRenderLoopDetection: boolean=false;
55+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=false;
5556

5657
exportconstenableEagerAlternateStateNodeCleanup: boolean=true;
5758
exportconstenableEffectEventMutationPhase: boolean=false;

‎packages/shared/forks/ReactFeatureFlags.test-renderer.native-fb.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export const enableCreateEventHandleAPI = false;
2626
exportconstenableMoveBefore=false;
2727
exportconstenableFizzExternalRuntime=true;
2828
exportconstenableInfiniteRenderLoopDetection=false;
29+
exportconstenableInfiniteRenderLoopDetectionForceThrow=false;
2930
exportconstenableLegacyCache=false;
3031
exportconstenableLegacyFBSupport=false;
3132
exportconstenableLegacyHidden=false;

‎packages/shared/forks/ReactFeatureFlags.test-renderer.www.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export const passChildrenWhenCloningPersistedNodes: boolean = false;
5454
exportconstdisableClientCache: boolean=true;
5555

5656
exportconstenableInfiniteRenderLoopDetection: boolean=false;
57+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=false;
5758

5859
exportconstenableReactTestRendererWarning: boolean=false;
5960
exportconstdisableLegacyMode: boolean=true;

‎packages/shared/forks/ReactFeatureFlags.www-dynamic.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export const transitionLaneExpirationMs = 5000;
2727
exportconstenableSchedulingProfiler: boolean=__VARIANT__;
2828

2929
exportconstenableInfiniteRenderLoopDetection: boolean=__VARIANT__;
30+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=__VARIANT__;
3031

3132
exportconstenableFastAddPropertiesInDiffing: boolean=__VARIANT__;
3233
exportconstenableSuspenseyImages: boolean=__VARIANT__;

‎packages/shared/forks/ReactFeatureFlags.www.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export const {
2020
disableSchedulerTimeoutInWorkLoop,
2121
enableEffectEventMutationPhase,
2222
enableInfiniteRenderLoopDetection,
23+
enableInfiniteRenderLoopDetectionForceThrow,
2324
enableNoCloningMemoCache,
2425
enableObjectFiber,
2526
enableRetryLaneExpiration,

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' [enableInfiniteRenderLoopDetection] Add a flag to force throwing (#36… · react/react@f4e0d4e · GitHub
Skip to content

Commit f4e0d4e

Browse files
authored
[enableInfiniteRenderLoopDetection] Add a flag to force throwing (#36357)
Adds a `enableInfiniteRenderLoopDetectionForceThrow` flag, which changes the detection mechanism behaviour to start throwing, when the loop is observed. By default, the value is set to `false`, also made dynamic from the start for `www`.
1 parent ad5dfc8 commit f4e0d4e

11 files changed

Lines changed: 203 additions & 8 deletions

‎packages/react-dom/src/__tests__/ReactUpdates-test.js‎

Lines changed: 151 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,7 +1793,14 @@ describe('ReactUpdates', () => {
17931793
});
17941794

17951795
it("warns about potential infinite loop if there's a synchronous render phase update on another component",async()=>{
1796-
if(!__DEV__||gate(flags=>!flags.enableInfiniteRenderLoopDetection)){
1796+
if(
1797+
!__DEV__||
1798+
gate(
1799+
flags=>
1800+
!flags.enableInfiniteRenderLoopDetection||
1801+
flags.enableInfiniteRenderLoopDetectionForceThrow,
1802+
)
1803+
){
17971804
return;
17981805
}
17991806
letsetState;
@@ -1831,7 +1838,14 @@ describe('ReactUpdates', () => {
18311838
});
18321839

18331840
it("warns about potential infinite loop if there's an async render phase update on another component",async()=>{
1834-
if(!__DEV__||gate(flags=>!flags.enableInfiniteRenderLoopDetection)){
1841+
if(
1842+
!__DEV__||
1843+
gate(
1844+
flags=>
1845+
!flags.enableInfiniteRenderLoopDetection||
1846+
flags.enableInfiniteRenderLoopDetectionForceThrow,
1847+
)
1848+
){
18351849
return;
18361850
}
18371851
letsetState;
@@ -2007,7 +2021,14 @@ describe('ReactUpdates', () => {
20072021
});
20082022

20092023
it('warns instead of throwing when infinite Suspense ping loop is detected via enableInfiniteRenderLoopDetection during commit phase',async()=>{
2010-
if(!__DEV__||gate(flags=>!flags.enableInfiniteRenderLoopDetection)){
2024+
if(
2025+
!__DEV__||
2026+
gate(
2027+
flags=>
2028+
!flags.enableInfiniteRenderLoopDetection||
2029+
flags.enableInfiniteRenderLoopDetectionForceThrow,
2030+
)
2031+
){
20112032
return;
20122033
}
20132034

@@ -2115,6 +2136,133 @@ describe('ReactUpdates', () => {
21152136
expect(errors).toEqual([]);
21162137
});
21172138

2139+
// @gate enableInfiniteRenderLoopDetection && enableInfiniteRenderLoopDetectionForceThrow
2140+
it('throws when sync render-phase update loop is detected with force-throw enabled',async()=>{
2141+
// Render-phase setState on another component's hook produces a sync
2142+
// recursive update. With ForceThrow enabled this should throw via
2143+
// throwForcedInfiniteRenderLoopError instead of only warning in DEV.
2144+
letsetState;
2145+
letshouldStop=false;
2146+
functionApp(){
2147+
const[,_setState]=React.useState(0);
2148+
setState=_setState;
2149+
return<Child/>;
2150+
}
2151+
2152+
functionChild(){
2153+
if(shouldStop){
2154+
returnnull;
2155+
}
2156+
setState(n=>n+1);
2157+
returnnull;
2158+
}
2159+
2160+
constcontainer=document.createElement('div');
2161+
consterrors=[];
2162+
constcaptureError=error=>{
2163+
errors.push(error.message);
2164+
// Stop scheduling new updates so the test (and the gate-off variant
2165+
// where the legacy error path is recoverable) can terminate cleanly
2166+
// without tripping the babel infinite-loop guard.
2167+
shouldStop=true;
2168+
};
2169+
constroot=ReactDOMClient.createRoot(container,{
2170+
onUncaughtError: captureError,
2171+
onRecoverableError: captureError,
2172+
onCaughtError: captureError,
2173+
});
2174+
2175+
// The render-phase setState path also produces a dev-only "Cannot update
2176+
// a component while rendering a different component" console.error on
2177+
// every recursion. Swallow those so the test framework doesn't require
2178+
// us to assert each one.
2179+
constoriginalConsoleError=console.error;
2180+
console.error=msg=>{
2181+
if(
2182+
typeofmsg==='string'&&
2183+
msg.startsWith('Cannot update a component')
2184+
){
2185+
return;
2186+
}
2187+
originalConsoleError(msg);
2188+
};
2189+
try{
2190+
awaitact(()=>{
2191+
root.render(<App/>);
2192+
});
2193+
}finally{
2194+
console.error=originalConsoleError;
2195+
}
2196+
2197+
expect(errors.length).toBeGreaterThanOrEqual(1);
2198+
expect(errors[0]).toContain(
2199+
'Maximum update depth exceeded. This could be an infinite loop.',
2200+
);
2201+
});
2202+
2203+
// @gate enableInfiniteRenderLoopDetection && enableInfiniteRenderLoopDetectionForceThrow
2204+
it('throws when phase-spawn update loop is detected with force-throw enabled',async()=>{
2205+
// Wrapping the initial render in startTransition makes the render-phase
2206+
// setState inherit a non-sync transition lane. After commit, the next
2207+
// render is non-sync, so the loop detector classifies the recursion as
2208+
// NESTED_UPDATE_PHASE_SPAWN (rather than SYNC_LANE). With ForceThrow
2209+
// enabled, this branch should throw via throwForcedInfiniteRenderLoopError
2210+
// instead of only warning in DEV.
2211+
letsetState;
2212+
letshouldStop=false;
2213+
// Hard cap on Child renders. Without enableInfiniteRenderLoopDetection,
2214+
// the PHASE_SPAWN branch is gated off entirely, so no throw fires and
2215+
// the loop would otherwise run until the babel infinite-loop guard.
2216+
letrenderCount=0;
2217+
constRENDER_CAP=100;
2218+
functionApp(){
2219+
const[,_setState]=React.useState(0);
2220+
setState=_setState;
2221+
return<Child/>;
2222+
}
2223+
2224+
functionChild(){
2225+
if(shouldStop||renderCount>=RENDER_CAP){
2226+
returnnull;
2227+
}
2228+
renderCount++;
2229+
setState(n=>n+1);
2230+
returnnull;
2231+
}
2232+
2233+
constcontainer=document.createElement('div');
2234+
consterrors=[];
2235+
constroot=ReactDOMClient.createRoot(container,{
2236+
onUncaughtError: error=>{
2237+
errors.push(error.message);
2238+
shouldStop=true;
2239+
},
2240+
});
2241+
2242+
constoriginalConsoleError=console.error;
2243+
console.error=msg=>{
2244+
if(
2245+
typeofmsg==='string'&&
2246+
msg.startsWith('Cannot update a component')
2247+
){
2248+
return;
2249+
}
2250+
originalConsoleError(msg);
2251+
};
2252+
try{
2253+
awaitact(()=>{
2254+
React.startTransition(()=>root.render(<App/>));
2255+
});
2256+
}finally{
2257+
console.error=originalConsoleError;
2258+
}
2259+
2260+
expect(errors.length).toBeGreaterThanOrEqual(1);
2261+
expect(errors[0]).toContain(
2262+
'Maximum update depth exceeded. This could be an infinite loop.',
2263+
);
2264+
});
2265+
21182266
it('prevents infinite update loop triggered by too many updates in ref callbacks',async()=>{
21192267
letscheduleUpdate;
21202268
functionTooManyRefUpdates(){

‎packages/react-reconciler/src/ReactFiberWorkLoop.js‎

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import {
5151
disableLegacyContext,
5252
alwaysThrottleRetries,
5353
enableInfiniteRenderLoopDetection,
54+
enableInfiniteRenderLoopDetectionForceThrow,
5455
disableLegacyMode,
5556
enableComponentPerformanceTrack,
5657
enableYieldingBeforePassive,
@@ -5175,6 +5176,27 @@ export function resolveRetryWakeable(boundaryFiber: Fiber, wakeable: Wakeable) {
51755176
retryTimedOutBoundary(boundaryFiber,retryLane);
51765177
}
51775178

5179+
functionthrowForcedInfiniteRenderLoopError(
5180+
root: FiberRoot|null,
5181+
renderLanes: Lanes,
5182+
): empty{
5183+
if(root!==null){
5184+
// Disable concurrent error recovery for the in-progress render so the thrown
5185+
// error reaches the nearest error boundary and breaks the infinite update
5186+
// loop instead of being silently retried by the recovery mechanism.
5187+
root.errorRecoveryDisabledLanes=mergeLanes(
5188+
root.errorRecoveryDisabledLanes,
5189+
renderLanes,
5190+
);
5191+
}
5192+
thrownewError(
5193+
'Maximum update depth exceeded. This could be an infinite loop. This can happen when a component '+
5194+
'repeatedly calls setState during render phase or inside useLayoutEffect, '+
5195+
'causing infinite render loop. React limits the number of nested updates to '+
5196+
'prevent infinite loops.',
5197+
);
5198+
}
5199+
51785200
exportfunctionthrowIfInfiniteUpdateLoopDetected(
51795201
isFromInfiniteRenderLoopDetectionInstrumentation: boolean,
51805202
){
@@ -5191,10 +5213,16 @@ export function throwIfInfiniteUpdateLoopDetected(
51915213
if(updateKind===NESTED_UPDATE_SYNC_LANE){
51925214
if(
51935215
isFromInfiniteRenderLoopDetectionInstrumentation||
5194-
(executionContext&RenderContext&&workInProgressRoot!==null)
5216+
(executionContext&RenderContext)!==NoContext
51955217
){
5196-
// This loop was identified only because of the instrumentation gated with enableInfiniteRenderLoopDetection, warn instead of throwing.
5197-
if(__DEV__){
5218+
// This loop was identified only because of the instrumentation gated with enableInfiniteRenderLoopDetection,
5219+
// warn instead of throwing, unless enableInfiniteRenderLoopDetectionForceThrow.
5220+
if(enableInfiniteRenderLoopDetectionForceThrow){
5221+
throwForcedInfiniteRenderLoopError(
5222+
workInProgressRoot,
5223+
workInProgressRootRenderLanes,
5224+
);
5225+
}elseif(__DEV__){
51985226
console.error(
51995227
'Maximum update depth exceeded. This could be an infinite loop. This can happen when a component '+
52005228
'repeatedly calls setState during render phase or inside useLayoutEffect, '+
@@ -5211,7 +5239,12 @@ export function throwIfInfiniteUpdateLoopDetected(
52115239
);
52125240
}
52135241
}elseif(updateKind===NESTED_UPDATE_PHASE_SPAWN){
5214-
if(__DEV__){
5242+
if(enableInfiniteRenderLoopDetectionForceThrow){
5243+
throwForcedInfiniteRenderLoopError(
5244+
workInProgressRoot,
5245+
workInProgressRootRenderLanes,
5246+
);
5247+
}elseif(__DEV__){
52155248
console.error(
52165249
'Maximum update depth exceeded. This could be an infinite loop. This can happen when a component '+
52175250
'repeatedly calls setState during render phase or inside useLayoutEffect, '+

‎packages/shared/ReactFeatureFlags.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ export const transitionLaneExpirationMs = 5000;
144144
* by setState or similar outside of the component owning the state.
145145
*/
146146
exportconstenableInfiniteRenderLoopDetection: boolean=false;
147+
/**
148+
* When `enableInfiniteRenderLoopDetection` is on, forces the detection
149+
* mechanism to throw instead of only warning in cases where it would
150+
* otherwise downgrade to a warning.
151+
*/
152+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=false;
147153

148154
exportconstenableFragmentRefs: boolean=true;
149155
exportconstenableFragmentRefsScrollIntoView: boolean=true;

‎packages/shared/forks/ReactFeatureFlags.native-fb.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export const enableCreateEventHandleAPI: boolean = false;
4747
exportconstenableMoveBefore: boolean=true;
4848
exportconstenableFizzExternalRuntime: boolean=true;
4949
exportconstenableInfiniteRenderLoopDetection: boolean=false;
50+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=false;
5051
exportconstenableLegacyCache: boolean=false;
5152
exportconstenableLegacyFBSupport: boolean=false;
5253
exportconstenableLegacyHidden: boolean=false;

‎packages/shared/forks/ReactFeatureFlags.native-oss.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export const enableCreateEventHandleAPI: boolean = false;
3131
exportconstenableMoveBefore: boolean=true;
3232
exportconstenableFizzExternalRuntime: boolean=true;
3333
exportconstenableInfiniteRenderLoopDetection: boolean=false;
34+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=false;
3435
exportconstenableLegacyCache: boolean=false;
3536
exportconstenableLegacyFBSupport: boolean=false;
3637
exportconstenableLegacyHidden: boolean=false;

‎packages/shared/forks/ReactFeatureFlags.test-renderer.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export const passChildrenWhenCloningPersistedNodes: boolean = false;
5252
exportconstdisableClientCache: boolean=true;
5353

5454
exportconstenableInfiniteRenderLoopDetection: boolean=false;
55+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=false;
5556

5657
exportconstenableEagerAlternateStateNodeCleanup: boolean=true;
5758
exportconstenableEffectEventMutationPhase: boolean=false;

‎packages/shared/forks/ReactFeatureFlags.test-renderer.native-fb.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export const enableCreateEventHandleAPI = false;
2626
exportconstenableMoveBefore=false;
2727
exportconstenableFizzExternalRuntime=true;
2828
exportconstenableInfiniteRenderLoopDetection=false;
29+
exportconstenableInfiniteRenderLoopDetectionForceThrow=false;
2930
exportconstenableLegacyCache=false;
3031
exportconstenableLegacyFBSupport=false;
3132
exportconstenableLegacyHidden=false;

‎packages/shared/forks/ReactFeatureFlags.test-renderer.www.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export const passChildrenWhenCloningPersistedNodes: boolean = false;
5454
exportconstdisableClientCache: boolean=true;
5555

5656
exportconstenableInfiniteRenderLoopDetection: boolean=false;
57+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=false;
5758

5859
exportconstenableReactTestRendererWarning: boolean=false;
5960
exportconstdisableLegacyMode: boolean=true;

‎packages/shared/forks/ReactFeatureFlags.www-dynamic.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export const transitionLaneExpirationMs = 5000;
2727
exportconstenableSchedulingProfiler: boolean=__VARIANT__;
2828

2929
exportconstenableInfiniteRenderLoopDetection: boolean=__VARIANT__;
30+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=__VARIANT__;
3031

3132
exportconstenableFastAddPropertiesInDiffing: boolean=__VARIANT__;
3233
exportconstenableSuspenseyImages: boolean=__VARIANT__;

‎packages/shared/forks/ReactFeatureFlags.www.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export const {
2020
disableSchedulerTimeoutInWorkLoop,
2121
enableEffectEventMutationPhase,
2222
enableInfiniteRenderLoopDetection,
23+
enableInfiniteRenderLoopDetectionForceThrow,
2324
enableNoCloningMemoCache,
2425
enableObjectFiber,
2526
enableRetryLaneExpiration,

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { // Universal Dark Mode - works on any site (function() { var enabled = true; function applyDarkMode() { if (!enabled) return; // Create style element if it doesn't exist var style = document.getElementById('universal-dark-mode-style'); if (!style) { style = document.createElement('style'); style.id = 'universal-dark-mode-style'; document.head.appendChild(style); } // Dark mode CSS - inverts colors but preserves images/video style.textContent = ' /* Invert everything except media */ html { filter: invert(1) hue-rotate(180deg) !important; background: #1a1a2e !important; } /* Restore images, videos, iframes, canvas */ img, video, iframe, canvas, svg, picture, [style*="background-image"] { filter: invert(1) hue-rotate(180deg) !important; } /* Preserve specific elements that should not be inverted */ .no-dark-mode, .no-dark-mode *, [data-theme="light"], [data-theme="light"], .ace_editor, .ace_editor *, .CodeMirror, .CodeMirror *, .monaco-editor, .monaco-editor *, .markdown-body pre, .markdown-body pre *, .highlight, .highlight *, pre code, pre code * { filter: none !important; } /* Fix common UI elements */ .modal, .popup, .dropdown-menu, .tooltip, .popover { filter: invert(1) hue-rotate(180deg) !important; background: #2d2d44 !important; border-color: #444 !important; } /* Scrollbars */ ::-webkit-scrollbar { background: #1a1a2e !important; } ::-webkit-scrollbar-thumb { background: #444 !important; } ::-webkit-scrollbar-thumb:hover { background: #555 !important; } /* Selection */ ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; } ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; } '; } function removeDarkMode() { var style = document.getElementById('universal-dark-mode-style'); if (style) style.remove(); } // Toggle with Alt+Shift+D document.addEventListener('keydown', function(e) { if (e.altKey && e.shiftKey && e.key === 'D') { e.preventDefault(); enabled = !enabled; if (enabled) { applyDarkMode(); console.log('[Universal Dark Mode] Enabled'); } else { removeDarkMode(); console.log('[Universal Dark Mode] Disabled'); } } }); // Apply on load applyDarkMode(); // Re-apply on dynamic content var observer = new MutationObserver(function(mutations) { if (enabled && !document.getElementById('universal-dark-mode-style')) { applyDarkMode(); } }); observer.observe(document.head, { childList: true }); console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle'); })(); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })(); [enableInfiniteRenderLoopDetection] Add a flag to force throwing (#36… · react/react@f4e0d4e · GitHub
Skip to content

Commit f4e0d4e

Browse files
authored
[enableInfiniteRenderLoopDetection] Add a flag to force throwing (#36357)
Adds a `enableInfiniteRenderLoopDetectionForceThrow` flag, which changes the detection mechanism behaviour to start throwing, when the loop is observed. By default, the value is set to `false`, also made dynamic from the start for `www`.
1 parent ad5dfc8 commit f4e0d4e

11 files changed

Lines changed: 203 additions & 8 deletions

‎packages/react-dom/src/__tests__/ReactUpdates-test.js‎

Lines changed: 151 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,7 +1793,14 @@ describe('ReactUpdates', () => {
17931793
});
17941794

17951795
it("warns about potential infinite loop if there's a synchronous render phase update on another component",async()=>{
1796-
if(!__DEV__||gate(flags=>!flags.enableInfiniteRenderLoopDetection)){
1796+
if(
1797+
!__DEV__||
1798+
gate(
1799+
flags=>
1800+
!flags.enableInfiniteRenderLoopDetection||
1801+
flags.enableInfiniteRenderLoopDetectionForceThrow,
1802+
)
1803+
){
17971804
return;
17981805
}
17991806
letsetState;
@@ -1831,7 +1838,14 @@ describe('ReactUpdates', () => {
18311838
});
18321839

18331840
it("warns about potential infinite loop if there's an async render phase update on another component",async()=>{
1834-
if(!__DEV__||gate(flags=>!flags.enableInfiniteRenderLoopDetection)){
1841+
if(
1842+
!__DEV__||
1843+
gate(
1844+
flags=>
1845+
!flags.enableInfiniteRenderLoopDetection||
1846+
flags.enableInfiniteRenderLoopDetectionForceThrow,
1847+
)
1848+
){
18351849
return;
18361850
}
18371851
letsetState;
@@ -2007,7 +2021,14 @@ describe('ReactUpdates', () => {
20072021
});
20082022

20092023
it('warns instead of throwing when infinite Suspense ping loop is detected via enableInfiniteRenderLoopDetection during commit phase',async()=>{
2010-
if(!__DEV__||gate(flags=>!flags.enableInfiniteRenderLoopDetection)){
2024+
if(
2025+
!__DEV__||
2026+
gate(
2027+
flags=>
2028+
!flags.enableInfiniteRenderLoopDetection||
2029+
flags.enableInfiniteRenderLoopDetectionForceThrow,
2030+
)
2031+
){
20112032
return;
20122033
}
20132034

@@ -2115,6 +2136,133 @@ describe('ReactUpdates', () => {
21152136
expect(errors).toEqual([]);
21162137
});
21172138

2139+
// @gate enableInfiniteRenderLoopDetection && enableInfiniteRenderLoopDetectionForceThrow
2140+
it('throws when sync render-phase update loop is detected with force-throw enabled',async()=>{
2141+
// Render-phase setState on another component's hook produces a sync
2142+
// recursive update. With ForceThrow enabled this should throw via
2143+
// throwForcedInfiniteRenderLoopError instead of only warning in DEV.
2144+
letsetState;
2145+
letshouldStop=false;
2146+
functionApp(){
2147+
const[,_setState]=React.useState(0);
2148+
setState=_setState;
2149+
return<Child/>;
2150+
}
2151+
2152+
functionChild(){
2153+
if(shouldStop){
2154+
returnnull;
2155+
}
2156+
setState(n=>n+1);
2157+
returnnull;
2158+
}
2159+
2160+
constcontainer=document.createElement('div');
2161+
consterrors=[];
2162+
constcaptureError=error=>{
2163+
errors.push(error.message);
2164+
// Stop scheduling new updates so the test (and the gate-off variant
2165+
// where the legacy error path is recoverable) can terminate cleanly
2166+
// without tripping the babel infinite-loop guard.
2167+
shouldStop=true;
2168+
};
2169+
constroot=ReactDOMClient.createRoot(container,{
2170+
onUncaughtError: captureError,
2171+
onRecoverableError: captureError,
2172+
onCaughtError: captureError,
2173+
});
2174+
2175+
// The render-phase setState path also produces a dev-only "Cannot update
2176+
// a component while rendering a different component" console.error on
2177+
// every recursion. Swallow those so the test framework doesn't require
2178+
// us to assert each one.
2179+
constoriginalConsoleError=console.error;
2180+
console.error=msg=>{
2181+
if(
2182+
typeofmsg==='string'&&
2183+
msg.startsWith('Cannot update a component')
2184+
){
2185+
return;
2186+
}
2187+
originalConsoleError(msg);
2188+
};
2189+
try{
2190+
awaitact(()=>{
2191+
root.render(<App/>);
2192+
});
2193+
}finally{
2194+
console.error=originalConsoleError;
2195+
}
2196+
2197+
expect(errors.length).toBeGreaterThanOrEqual(1);
2198+
expect(errors[0]).toContain(
2199+
'Maximum update depth exceeded. This could be an infinite loop.',
2200+
);
2201+
});
2202+
2203+
// @gate enableInfiniteRenderLoopDetection && enableInfiniteRenderLoopDetectionForceThrow
2204+
it('throws when phase-spawn update loop is detected with force-throw enabled',async()=>{
2205+
// Wrapping the initial render in startTransition makes the render-phase
2206+
// setState inherit a non-sync transition lane. After commit, the next
2207+
// render is non-sync, so the loop detector classifies the recursion as
2208+
// NESTED_UPDATE_PHASE_SPAWN (rather than SYNC_LANE). With ForceThrow
2209+
// enabled, this branch should throw via throwForcedInfiniteRenderLoopError
2210+
// instead of only warning in DEV.
2211+
letsetState;
2212+
letshouldStop=false;
2213+
// Hard cap on Child renders. Without enableInfiniteRenderLoopDetection,
2214+
// the PHASE_SPAWN branch is gated off entirely, so no throw fires and
2215+
// the loop would otherwise run until the babel infinite-loop guard.
2216+
letrenderCount=0;
2217+
constRENDER_CAP=100;
2218+
functionApp(){
2219+
const[,_setState]=React.useState(0);
2220+
setState=_setState;
2221+
return<Child/>;
2222+
}
2223+
2224+
functionChild(){
2225+
if(shouldStop||renderCount>=RENDER_CAP){
2226+
returnnull;
2227+
}
2228+
renderCount++;
2229+
setState(n=>n+1);
2230+
returnnull;
2231+
}
2232+
2233+
constcontainer=document.createElement('div');
2234+
consterrors=[];
2235+
constroot=ReactDOMClient.createRoot(container,{
2236+
onUncaughtError: error=>{
2237+
errors.push(error.message);
2238+
shouldStop=true;
2239+
},
2240+
});
2241+
2242+
constoriginalConsoleError=console.error;
2243+
console.error=msg=>{
2244+
if(
2245+
typeofmsg==='string'&&
2246+
msg.startsWith('Cannot update a component')
2247+
){
2248+
return;
2249+
}
2250+
originalConsoleError(msg);
2251+
};
2252+
try{
2253+
awaitact(()=>{
2254+
React.startTransition(()=>root.render(<App/>));
2255+
});
2256+
}finally{
2257+
console.error=originalConsoleError;
2258+
}
2259+
2260+
expect(errors.length).toBeGreaterThanOrEqual(1);
2261+
expect(errors[0]).toContain(
2262+
'Maximum update depth exceeded. This could be an infinite loop.',
2263+
);
2264+
});
2265+
21182266
it('prevents infinite update loop triggered by too many updates in ref callbacks',async()=>{
21192267
letscheduleUpdate;
21202268
functionTooManyRefUpdates(){

‎packages/react-reconciler/src/ReactFiberWorkLoop.js‎

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import {
5151
disableLegacyContext,
5252
alwaysThrottleRetries,
5353
enableInfiniteRenderLoopDetection,
54+
enableInfiniteRenderLoopDetectionForceThrow,
5455
disableLegacyMode,
5556
enableComponentPerformanceTrack,
5657
enableYieldingBeforePassive,
@@ -5175,6 +5176,27 @@ export function resolveRetryWakeable(boundaryFiber: Fiber, wakeable: Wakeable) {
51755176
retryTimedOutBoundary(boundaryFiber,retryLane);
51765177
}
51775178

5179+
functionthrowForcedInfiniteRenderLoopError(
5180+
root: FiberRoot|null,
5181+
renderLanes: Lanes,
5182+
): empty{
5183+
if(root!==null){
5184+
// Disable concurrent error recovery for the in-progress render so the thrown
5185+
// error reaches the nearest error boundary and breaks the infinite update
5186+
// loop instead of being silently retried by the recovery mechanism.
5187+
root.errorRecoveryDisabledLanes=mergeLanes(
5188+
root.errorRecoveryDisabledLanes,
5189+
renderLanes,
5190+
);
5191+
}
5192+
thrownewError(
5193+
'Maximum update depth exceeded. This could be an infinite loop. This can happen when a component '+
5194+
'repeatedly calls setState during render phase or inside useLayoutEffect, '+
5195+
'causing infinite render loop. React limits the number of nested updates to '+
5196+
'prevent infinite loops.',
5197+
);
5198+
}
5199+
51785200
exportfunctionthrowIfInfiniteUpdateLoopDetected(
51795201
isFromInfiniteRenderLoopDetectionInstrumentation: boolean,
51805202
){
@@ -5191,10 +5213,16 @@ export function throwIfInfiniteUpdateLoopDetected(
51915213
if(updateKind===NESTED_UPDATE_SYNC_LANE){
51925214
if(
51935215
isFromInfiniteRenderLoopDetectionInstrumentation||
5194-
(executionContext&RenderContext&&workInProgressRoot!==null)
5216+
(executionContext&RenderContext)!==NoContext
51955217
){
5196-
// This loop was identified only because of the instrumentation gated with enableInfiniteRenderLoopDetection, warn instead of throwing.
5197-
if(__DEV__){
5218+
// This loop was identified only because of the instrumentation gated with enableInfiniteRenderLoopDetection,
5219+
// warn instead of throwing, unless enableInfiniteRenderLoopDetectionForceThrow.
5220+
if(enableInfiniteRenderLoopDetectionForceThrow){
5221+
throwForcedInfiniteRenderLoopError(
5222+
workInProgressRoot,
5223+
workInProgressRootRenderLanes,
5224+
);
5225+
}elseif(__DEV__){
51985226
console.error(
51995227
'Maximum update depth exceeded. This could be an infinite loop. This can happen when a component '+
52005228
'repeatedly calls setState during render phase or inside useLayoutEffect, '+
@@ -5211,7 +5239,12 @@ export function throwIfInfiniteUpdateLoopDetected(
52115239
);
52125240
}
52135241
}elseif(updateKind===NESTED_UPDATE_PHASE_SPAWN){
5214-
if(__DEV__){
5242+
if(enableInfiniteRenderLoopDetectionForceThrow){
5243+
throwForcedInfiniteRenderLoopError(
5244+
workInProgressRoot,
5245+
workInProgressRootRenderLanes,
5246+
);
5247+
}elseif(__DEV__){
52155248
console.error(
52165249
'Maximum update depth exceeded. This could be an infinite loop. This can happen when a component '+
52175250
'repeatedly calls setState during render phase or inside useLayoutEffect, '+

‎packages/shared/ReactFeatureFlags.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ export const transitionLaneExpirationMs = 5000;
144144
* by setState or similar outside of the component owning the state.
145145
*/
146146
exportconstenableInfiniteRenderLoopDetection: boolean=false;
147+
/**
148+
* When `enableInfiniteRenderLoopDetection` is on, forces the detection
149+
* mechanism to throw instead of only warning in cases where it would
150+
* otherwise downgrade to a warning.
151+
*/
152+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=false;
147153

148154
exportconstenableFragmentRefs: boolean=true;
149155
exportconstenableFragmentRefsScrollIntoView: boolean=true;

‎packages/shared/forks/ReactFeatureFlags.native-fb.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export const enableCreateEventHandleAPI: boolean = false;
4747
exportconstenableMoveBefore: boolean=true;
4848
exportconstenableFizzExternalRuntime: boolean=true;
4949
exportconstenableInfiniteRenderLoopDetection: boolean=false;
50+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=false;
5051
exportconstenableLegacyCache: boolean=false;
5152
exportconstenableLegacyFBSupport: boolean=false;
5253
exportconstenableLegacyHidden: boolean=false;

‎packages/shared/forks/ReactFeatureFlags.native-oss.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export const enableCreateEventHandleAPI: boolean = false;
3131
exportconstenableMoveBefore: boolean=true;
3232
exportconstenableFizzExternalRuntime: boolean=true;
3333
exportconstenableInfiniteRenderLoopDetection: boolean=false;
34+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=false;
3435
exportconstenableLegacyCache: boolean=false;
3536
exportconstenableLegacyFBSupport: boolean=false;
3637
exportconstenableLegacyHidden: boolean=false;

‎packages/shared/forks/ReactFeatureFlags.test-renderer.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export const passChildrenWhenCloningPersistedNodes: boolean = false;
5252
exportconstdisableClientCache: boolean=true;
5353

5454
exportconstenableInfiniteRenderLoopDetection: boolean=false;
55+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=false;
5556

5657
exportconstenableEagerAlternateStateNodeCleanup: boolean=true;
5758
exportconstenableEffectEventMutationPhase: boolean=false;

‎packages/shared/forks/ReactFeatureFlags.test-renderer.native-fb.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export const enableCreateEventHandleAPI = false;
2626
exportconstenableMoveBefore=false;
2727
exportconstenableFizzExternalRuntime=true;
2828
exportconstenableInfiniteRenderLoopDetection=false;
29+
exportconstenableInfiniteRenderLoopDetectionForceThrow=false;
2930
exportconstenableLegacyCache=false;
3031
exportconstenableLegacyFBSupport=false;
3132
exportconstenableLegacyHidden=false;

‎packages/shared/forks/ReactFeatureFlags.test-renderer.www.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export const passChildrenWhenCloningPersistedNodes: boolean = false;
5454
exportconstdisableClientCache: boolean=true;
5555

5656
exportconstenableInfiniteRenderLoopDetection: boolean=false;
57+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=false;
5758

5859
exportconstenableReactTestRendererWarning: boolean=false;
5960
exportconstdisableLegacyMode: boolean=true;

‎packages/shared/forks/ReactFeatureFlags.www-dynamic.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export const transitionLaneExpirationMs = 5000;
2727
exportconstenableSchedulingProfiler: boolean=__VARIANT__;
2828

2929
exportconstenableInfiniteRenderLoopDetection: boolean=__VARIANT__;
30+
exportconstenableInfiniteRenderLoopDetectionForceThrow: boolean=__VARIANT__;
3031

3132
exportconstenableFastAddPropertiesInDiffing: boolean=__VARIANT__;
3233
exportconstenableSuspenseyImages: boolean=__VARIANT__;

‎packages/shared/forks/ReactFeatureFlags.www.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export const {
2020
disableSchedulerTimeoutInWorkLoop,
2121
enableEffectEventMutationPhase,
2222
enableInfiniteRenderLoopDetection,
23+
enableInfiniteRenderLoopDetectionForceThrow,
2324
enableNoCloningMemoCache,
2425
enableObjectFiber,
2526
enableRetryLaneExpiration,

0 commit comments

Comments
 (0)