GeezerVison is showing github.com — Improve error boundary handling for unmounted subtrees (#19542) · react/react@ffb749c
File treeExpand file treeCollapse file tree Open diff view settings react-dom/src/events/__tests__
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/events/__tests__/DOMPluginEventSystem-test.internal.js +40-16Lines changed: 40 additions & 16 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1667,16 +1667,28 @@ describe('DOMPluginEventSystem', () => { | 1667 | 1667 |
| 1668 | 1668 | function Test() { | 1669 | 1669 | React.useEffect(() => { | 1670 | | - setClick1(buttonRef.current, targetListener1); | 1671 | | - setClick2(buttonRef.current, targetListener2); | 1672 | | - setClick3(buttonRef.current, targetListener3); | 1673 | | - setClick4(buttonRef.current, targetListener4); | | 1670 | + const clearClick1 = setClick1( | | 1671 | + buttonRef.current, | | 1672 | + targetListener1, | | 1673 | + ); | | 1674 | + const clearClick2 = setClick2( | | 1675 | + buttonRef.current, | | 1676 | + targetListener2, | | 1677 | + ); | | 1678 | + const clearClick3 = setClick3( | | 1679 | + buttonRef.current, | | 1680 | + targetListener3, | | 1681 | + ); | | 1682 | + const clearClick4 = setClick4( | | 1683 | + buttonRef.current, | | 1684 | + targetListener4, | | 1685 | + ); | 1674 | 1686 |
| 1675 | 1687 | return () => { | 1676 | | - setClick1(); | 1677 | | - setClick2(); | 1678 | | - setClick3(); | 1679 | | - setClick4(); | | 1688 | + clearClick1(); | | 1689 | + clearClick2(); | | 1690 | + clearClick3(); | | 1691 | + clearClick4(); | 1680 | 1692 | }; | 1681 | 1693 | }); | 1682 | 1694 |
| @@ -1701,16 +1713,28 @@ describe('DOMPluginEventSystem', () => { | 1701 | 1713 |
| 1702 | 1714 | function Test2() { | 1703 | 1715 | React.useEffect(() => { | 1704 | | - setClick1(buttonRef.current, targetListener1); | 1705 | | - setClick2(buttonRef.current, targetListener2); | 1706 | | - setClick3(buttonRef.current, targetListener3); | 1707 | | - setClick4(buttonRef.current, targetListener4); | | 1716 | + const clearClick1 = setClick1( | | 1717 | + buttonRef.current, | | 1718 | + targetListener1, | | 1719 | + ); | | 1720 | + const clearClick2 = setClick2( | | 1721 | + buttonRef.current, | | 1722 | + targetListener2, | | 1723 | + ); | | 1724 | + const clearClick3 = setClick3( | | 1725 | + buttonRef.current, | | 1726 | + targetListener3, | | 1727 | + ); | | 1728 | + const clearClick4 = setClick4( | | 1729 | + buttonRef.current, | | 1730 | + targetListener4, | | 1731 | + ); | 1708 | 1732 |
| 1709 | 1733 | return () => { | 1710 | | - setClick1(); | 1711 | | - setClick2(); | 1712 | | - setClick3(); | 1713 | | - setClick4(); | | 1734 | + clearClick1(); | | 1735 | + clearClick2(); | | 1736 | + clearClick3(); | | 1737 | + clearClick4(); | 1714 | 1738 | }; | 1715 | 1739 | }); | 1716 | 1740 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberCommitWork.new.js +33-18Lines changed: 33 additions & 18 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -173,13 +173,13 @@ function safelyCallComponentWillUnmount(current, instance) { | 173 | 173 | ); | 174 | 174 | if (hasCaughtError()) { | 175 | 175 | const unmountError = clearCaughtError(); | 176 | | - captureCommitPhaseError(current, unmountError); | | 176 | + captureCommitPhaseError(current, current.return, unmountError); | 177 | 177 | } | 178 | 178 | } else { | 179 | 179 | try { | 180 | 180 | callComponentWillUnmountWithTimer(current, instance); | 181 | 181 | } catch (unmountError) { | 182 | | - captureCommitPhaseError(current, unmountError); | | 182 | + captureCommitPhaseError(current, current.return, unmountError); | 183 | 183 | } | 184 | 184 | } | 185 | 185 | } | @@ -192,13 +192,13 @@ function safelyDetachRef(current: Fiber) { | 192 | 192 | invokeGuardedCallback(null, ref, null, null); | 193 | 193 | if (hasCaughtError()) { | 194 | 194 | const refError = clearCaughtError(); | 195 | | - captureCommitPhaseError(current, refError); | | 195 | + captureCommitPhaseError(current, current.return, refError); | 196 | 196 | } | 197 | 197 | } else { | 198 | 198 | try { | 199 | 199 | ref(null); | 200 | 200 | } catch (refError) { | 201 | | - captureCommitPhaseError(current, refError); | | 201 | + captureCommitPhaseError(current, current.return, refError); | 202 | 202 | } | 203 | 203 | } | 204 | 204 | } else { | @@ -207,18 +207,22 @@ function safelyDetachRef(current: Fiber) { | 207 | 207 | } | 208 | 208 | } | 209 | 209 |
| 210 | | -export function safelyCallDestroy(current: Fiber, destroy: () => void) { | | 210 | +export function safelyCallDestroy( | | 211 | + current: Fiber, | | 212 | + nearestMountedAncestor: Fiber | null, | | 213 | + destroy: () => void, | | 214 | +) { | 211 | 215 | if (__DEV__) { | 212 | 216 | invokeGuardedCallback(null, destroy, null); | 213 | 217 | if (hasCaughtError()) { | 214 | 218 | const error = clearCaughtError(); | 215 | | - captureCommitPhaseError(current, error); | | 219 | + captureCommitPhaseError(current, nearestMountedAncestor, error); | 216 | 220 | } | 217 | 221 | } else { | 218 | 222 | try { | 219 | 223 | destroy(); | 220 | 224 | } catch (error) { | 221 | | - captureCommitPhaseError(current, error); | | 225 | + captureCommitPhaseError(current, nearestMountedAncestor, error); | 222 | 226 | } | 223 | 227 | } | 224 | 228 | } | @@ -337,10 +341,10 @@ function commitHookEffectListUnmount(tag: HookEffectTag, finishedWork: Fiber) { | 337 | 341 |
| 338 | 342 | // TODO: Remove this duplication. | 339 | 343 | function commitHookEffectListUnmount2( | 340 | | - // Tags to check for when deciding whether to unmount. e.g. to skip over | 341 | | - // layout effects | | 344 | + // Tags to check for when deciding whether to unmount. e.g. to skip over layout effects | 342 | 345 | hookEffectTag: HookEffectTag, | 343 | 346 | fiber: Fiber, | | 347 | + nearestMountedAncestor: Fiber | null, | 344 | 348 | ): void { | 345 | 349 | const updateQueue: FunctionComponentUpdateQueue | null = (fiber.updateQueue: any); | 346 | 350 | const lastEffect = updateQueue !== null ? updateQueue.lastEffect : null; | @@ -359,10 +363,10 @@ function commitHookEffectListUnmount2( | 359 | 363 | fiber.mode & ProfileMode | 360 | 364 | ) { | 361 | 365 | startPassiveEffectTimer(); | 362 | | - safelyCallDestroy(fiber, destroy); | | 366 | + safelyCallDestroy(fiber, nearestMountedAncestor, destroy); | 363 | 367 | recordPassiveEffectDuration(fiber); | 364 | 368 | } else { | 365 | | - safelyCallDestroy(fiber, destroy); | | 369 | + safelyCallDestroy(fiber, nearestMountedAncestor, destroy); | 366 | 370 | } | 367 | 371 | } | 368 | 372 | } | @@ -465,7 +469,7 @@ function commitHookEffectListMount2(fiber: Fiber): void { | 465 | 469 | if (hasCaughtError()) { | 466 | 470 | invariant(fiber !== null, 'Should be working on an effect.'); | 467 | 471 | const error = clearCaughtError(); | 468 | | - captureCommitPhaseError(fiber, error); | | 472 | + captureCommitPhaseError(fiber, fiber.return, error); | 469 | 473 | } | 470 | 474 | } else { | 471 | 475 | try { | @@ -488,7 +492,7 @@ function commitHookEffectListMount2(fiber: Fiber): void { | 488 | 492 | // The warning refers to useEffect but only applies to useLayoutEffect. | 489 | 493 | } catch (error) { | 490 | 494 | invariant(fiber !== null, 'Should be working on an effect.'); | 491 | | - captureCommitPhaseError(fiber, error); | | 495 | + captureCommitPhaseError(fiber, fiber.return, error); | 492 | 496 | } | 493 | 497 | } | 494 | 498 | } | @@ -997,10 +1001,10 @@ function commitUnmount( | 997 | 1001 | current.mode & ProfileMode | 998 | 1002 | ) { | 999 | 1003 | startLayoutEffectTimer(); | 1000 | | - safelyCallDestroy(current, destroy); | | 1004 | + safelyCallDestroy(current, current.return, destroy); | 1001 | 1005 | recordLayoutEffectDuration(current); | 1002 | 1006 | } else { | 1003 | | - safelyCallDestroy(current, destroy); | | 1007 | + safelyCallDestroy(current, current.return, destroy); | 1004 | 1008 | } | 1005 | 1009 | } | 1006 | 1010 | } | @@ -1842,18 +1846,29 @@ function commitPassiveWork(finishedWork: Fiber): void { | 1842 | 1846 | case ForwardRef: | 1843 | 1847 | case SimpleMemoComponent: | 1844 | 1848 | case Block: { | 1845 | | - commitHookEffectListUnmount2(HookPassive | HookHasEffect, finishedWork); | | 1849 | + commitHookEffectListUnmount2( | | 1850 | + HookPassive | HookHasEffect, | | 1851 | + finishedWork, | | 1852 | + finishedWork.return, | | 1853 | + ); | 1846 | 1854 | } | 1847 | 1855 | } | 1848 | 1856 | } | 1849 | 1857 |
| 1850 | | -function commitPassiveUnmount(current: Fiber): void { | | 1858 | +function commitPassiveUnmount( | | 1859 | + current: Fiber, | | 1860 | + nearestMountedAncestor: Fiber | null, | | 1861 | +): void { | 1851 | 1862 | switch (current.tag) { | 1852 | 1863 | case FunctionComponent: | 1853 | 1864 | case ForwardRef: | 1854 | 1865 | case SimpleMemoComponent: | 1855 | 1866 | case Block: | 1856 | | - commitHookEffectListUnmount2(HookPassive, current); | | 1867 | + commitHookEffectListUnmount2( | | 1868 | + HookPassive, | | 1869 | + current, | | 1870 | + nearestMountedAncestor, | | 1871 | + ); | 1857 | 1872 | } | 1858 | 1873 | } | 1859 | 1874 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberWorkLoop.new.js +21-13Lines changed: 21 additions & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2393,14 +2393,14 @@ function commitBeforeMutationEffects(firstChild: Fiber) { | 2393 | 2393 | invokeGuardedCallback(null, commitBeforeMutationEffectsImpl, null, fiber); | 2394 | 2394 | if (hasCaughtError()) { | 2395 | 2395 | const error = clearCaughtError(); | 2396 | | - captureCommitPhaseError(fiber, error); | | 2396 | + captureCommitPhaseError(fiber, fiber.return, error); | 2397 | 2397 | } | 2398 | 2398 | resetCurrentDebugFiberInDEV(); | 2399 | 2399 | } else { | 2400 | 2400 | try { | 2401 | 2401 | commitBeforeMutationEffectsImpl(fiber); | 2402 | 2402 | } catch (error) { | 2403 | | - captureCommitPhaseError(fiber, error); | | 2403 | + captureCommitPhaseError(fiber, fiber.return, error); | 2404 | 2404 | } | 2405 | 2405 | } | 2406 | 2406 | fiber = fiber.sibling; | @@ -2490,14 +2490,14 @@ function commitMutationEffects( | 2490 | 2490 | ); | 2491 | 2491 | if (hasCaughtError()) { | 2492 | 2492 | const error = clearCaughtError(); | 2493 | | - captureCommitPhaseError(fiber, error); | | 2493 | + captureCommitPhaseError(fiber, fiber.return, error); | 2494 | 2494 | } | 2495 | 2495 | resetCurrentDebugFiberInDEV(); | 2496 | 2496 | } else { | 2497 | 2497 | try { | 2498 | 2498 | commitMutationEffectsImpl(fiber, root, renderPriorityLevel); | 2499 | 2499 | } catch (error) { | 2500 | | - captureCommitPhaseError(fiber, error); | | 2500 | + captureCommitPhaseError(fiber, fiber.return, error); | 2501 | 2501 | } | 2502 | 2502 | } | 2503 | 2503 | fiber = fiber.sibling; | @@ -2593,13 +2593,13 @@ function commitMutationEffectsDeletions( | 2593 | 2593 | ); | 2594 | 2594 | if (hasCaughtError()) { | 2595 | 2595 | const error = clearCaughtError(); | 2596 | | - captureCommitPhaseError(childToDelete, error); | | 2596 | + captureCommitPhaseError(childToDelete, childToDelete.return, error); | 2597 | 2597 | } | 2598 | 2598 | } else { | 2599 | 2599 | try { | 2600 | 2600 | commitDeletion(root, childToDelete, renderPriorityLevel); | 2601 | 2601 | } catch (error) { | 2602 | | - captureCommitPhaseError(childToDelete, error); | | 2602 | + captureCommitPhaseError(childToDelete, childToDelete.return, error); | 2603 | 2603 | } | 2604 | 2604 | } | 2605 | 2605 | } | @@ -2641,14 +2641,14 @@ function commitLayoutEffects( | 2641 | 2641 | ); | 2642 | 2642 | if (hasCaughtError()) { | 2643 | 2643 | const error = clearCaughtError(); | 2644 | | - captureCommitPhaseError(fiber, error); | | 2644 | + captureCommitPhaseError(fiber, fiber.return, error); | 2645 | 2645 | } | 2646 | 2646 | resetCurrentDebugFiberInDEV(); | 2647 | 2647 | } else { | 2648 | 2648 | try { | 2649 | 2649 | commitLayoutEffectsImpl(fiber, root, committedLanes); | 2650 | 2650 | } catch (error) { | 2651 | | - captureCommitPhaseError(fiber, error); | | 2651 | + captureCommitPhaseError(fiber, fiber.return, error); | 2652 | 2652 | } | 2653 | 2653 | } | 2654 | 2654 | fiber = fiber.sibling; | @@ -2748,7 +2748,7 @@ function flushPassiveUnmountEffects(firstChild: Fiber): void { | 2748 | 2748 | if (deletions !== null) { | 2749 | 2749 | for (let i = 0; i < deletions.length; i++) { | 2750 | 2750 | const fiberToDelete = deletions[i]; | 2751 | | - flushPassiveUnmountEffectsInsideOfDeletedTree(fiberToDelete); | | 2751 | + flushPassiveUnmountEffectsInsideOfDeletedTree(fiberToDelete, fiber); | 2752 | 2752 |
| 2753 | 2753 | // Now that passive effects have been processed, it's safe to detach lingering pointers. | 2754 | 2754 | detachFiberAfterEffects(fiberToDelete); | @@ -2780,6 +2780,7 @@ function flushPassiveUnmountEffects(firstChild: Fiber): void { | 2780 | 2780 |
| 2781 | 2781 | function flushPassiveUnmountEffectsInsideOfDeletedTree( | 2782 | 2782 | fiberToDelete: Fiber, | | 2783 | + nearestMountedAncestor: Fiber, | 2783 | 2784 | ): void { | 2784 | 2785 | if ((fiberToDelete.subtreeTag & PassiveStaticSubtreeTag) !== NoSubtreeTag) { | 2785 | 2786 | // If any children have passive effects then traverse the subtree. | @@ -2788,14 +2789,17 @@ function flushPassiveUnmountEffectsInsideOfDeletedTree( | 2788 | 2789 | // since that would not cover passive effects in siblings. | 2789 | 2790 | let child = fiberToDelete.child; | 2790 | 2791 | while (child !== null) { | 2791 | | - flushPassiveUnmountEffectsInsideOfDeletedTree(child); | | 2792 | + flushPassiveUnmountEffectsInsideOfDeletedTree( | | 2793 | + child, | | 2794 | + nearestMountedAncestor, | | 2795 | + ); | 2792 | 2796 | child = child.sibling; | 2793 | 2797 | } | 2794 | 2798 | } | 2795 | 2799 |
| 2796 | 2800 | if ((fiberToDelete.effectTag & PassiveStatic) !== NoEffect) { | 2797 | 2801 | setCurrentDebugFiberInDEV(fiberToDelete); | 2798 | | - commitPassiveUnmount(fiberToDelete); | | 2802 | + commitPassiveUnmount(fiberToDelete, nearestMountedAncestor); | 2799 | 2803 | resetCurrentDebugFiberInDEV(); | 2800 | 2804 | } | 2801 | 2805 | } | @@ -2922,15 +2926,19 @@ function captureCommitPhaseErrorOnRoot( | 2922 | 2926 | } | 2923 | 2927 | } | 2924 | 2928 |
| 2925 | | -export function captureCommitPhaseError(sourceFiber: Fiber, error: mixed) { | | 2929 | +export function captureCommitPhaseError( | | 2930 | + sourceFiber: Fiber, | | 2931 | + nearestMountedAncestor: Fiber | null, | | 2932 | + error: mixed, | | 2933 | +) { | 2926 | 2934 | if (sourceFiber.tag === HostRoot) { | 2927 | 2935 | // Error was thrown at the root. There is no parent, so the root | 2928 | 2936 | // itself should capture it. | 2929 | 2937 | captureCommitPhaseErrorOnRoot(sourceFiber, sourceFiber, error); | 2930 | 2938 | return; | 2931 | 2939 | } | 2932 | 2940 |
| 2933 | | - let fiber = sourceFiber.return; | | 2941 | + let fiber = nearestMountedAncestor; | 2934 | 2942 | while (fiber !== null) { | 2935 | 2943 | if (fiber.tag === HostRoot) { | 2936 | 2944 | captureCommitPhaseErrorOnRoot(fiber, sourceFiber, error); | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberWorkLoop.old.js +18Lines changed: 18 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2850,6 +2850,24 @@ export function captureCommitPhaseError(sourceFiber: Fiber, error: mixed) { | 2850 | 2850 | markRootUpdated(root, SyncLane, eventTime); | 2851 | 2851 | ensureRootIsScheduled(root, eventTime); | 2852 | 2852 | schedulePendingInteractions(root, SyncLane); | | 2853 | + } else { | | 2854 | + // This component has already been unmounted. | | 2855 | + // We can't schedule any follow up work for the root because the fiber is already unmounted, | | 2856 | + // but we can still call the log-only boundary so the error isn't swallowed. | | 2857 | + // | | 2858 | + // TODO This is only a temporary bandaid for the old reconciler fork. | | 2859 | + // We can delete this special case once the new fork is merged. | | 2860 | + if ( | | 2861 | + typeof instance.componentDidCatch === 'function' && | | 2862 | + !isAlreadyFailedLegacyErrorBoundary(instance) | | 2863 | + ) { | | 2864 | + try { | | 2865 | + instance.componentDidCatch(error, errorInfo); | | 2866 | + } catch (errorToIgnore) { | | 2867 | + // TODO Ignore this error? Rethrow it? | | 2868 | + // This is kind of an edge case. | | 2869 | + } | | 2870 | + } | 2853 | 2871 | } | 2854 | 2872 | return; | 2855 | 2873 | } | |
|