[react-refresh] fix memory leak: only add root to helpersByRoot on mount#36382
Open
sleitor wants to merge 1 commit intofacebook:mainfrom
Open
[react-refresh] fix memory leak: only add root to helpersByRoot on mount#36382sleitor wants to merge 1 commit intofacebook:mainfrom
sleitor wants to merge 1 commit intofacebook:mainfrom
Conversation
helpersByRoot.set(root, helpers) was called unconditionally on every onCommitFiberRoot invocation, including re-renders after unmount. This caused secondary renderer roots (e.g. react-three-fiber Canvas) to remain in the helpersByRoot Map permanently, preventing GC. Fix: - Move helpersByRoot.set(root, helpers) into the mount branches only (!wasMounted && isMounted, and the no-alternate initial mount path). - Skip helpersByRoot.set for the update case (wasMounted && isMounted) since the root is already in the Map from the initial mount. - Guard failedRoots.add in the (!wasMounted && !isMounted && didError) case with helpersByRoot.has(root) to avoid retaining roots that were never successfully mounted. Fixes facebook#36379
|
Comparing: f4e0d4e...e204ed5 Critical size changesIncludes critical production bundles, as well as any change greater than 2%:
Significant size changesIncludes any change greater than 0.2%: Expand to show
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #36379
Problem
helpersByRoot.set(root, helpers)was called unconditionally on everyonCommitFiberRootinvocation. This means that even after a root is unmounted (and correctly deleted fromhelpersByRootin thewasMounted && !isMountedbranch), any subsequent commit from that renderer ID would re-add the root to the Map.This causes memory leaks for secondary renderers such as react-three-fiber, where Canvas roots are repeatedly mounted and unmounted. The
helpersByRootMap grows without bound, preventing garbage collection of fiber roots and their associated component trees.Fix
Move
helpersByRoot.set(root, helpers)into the mount branches only:!wasMounted && isMounted(new mount with an existing alternate): set the root inhelpersByRoot.else(first mount without alternate): set the root inhelpersByRoot.wasMounted && isMounted(update): no change needed — root is already in the Map from the initial mount.!wasMounted && !isMounted && didError: guardfailedRoots.add(root)withhelpersByRoot.has(root)to avoid retaining roots that were never successfully mounted.Testing
ReactFreshMultipleRenderer-test.internal.jsexercise the multi-renderer path and should continue to pass.