fix(EffectComposer): fix StrictMode composer leak, guard against r3f reorder bug - #353
Merged
Merged
Conversation
This was referenced Jul 29, 2026
kvvasuu
force-pushed
the
fix/03-effectcomposer-leak-and-reorder
branch
from
July 30, 2026 20:31
7df1c67 to
0d18afa
Compare
…reorder bug EffectComposerImpl was created inside useMemo, which doesn't guarantee its cleanup runs before React discards a memoized value - this leaked the composer (and its WebGL resources) under StrictMode. It's now created and disposed inside a useState/useEffect lifecycle instead. Pass collection walks the real r3f instance tree (group.current.__r3f .children) and rebuilds the pass list from scratch whenever it or the composer changes, so the order always matches current JSX - including through wrapper components - after a reorder or a remount. While testing the reorder case, found a genuine bug in r3f's host config: when React moves (not remounts) an existing child - e.g. a key-preserving reorder of a keyed effect list - insertBefore/ appendChild splice the moved instance into its new slot without detaching it from the old one first, leaving a stale duplicate in Instance.children. Worth reporting upstream. Added a cheap dedupe (keep each object's last occurrence, sorted by that position) that recovers the correct order either way and is a no-op when the bug isn't present. Also widens `children` from JSX.Element | JSX.Element[] to ReactNode, which was rejecting the common `condition && <Effect/>` idiom (that expression is `false | Element`, and `false` isn't assignable to either half of the union). The tree-walk already tolerates arbitrary children gracefully - it just filters for `instanceof Effect || instanceof Pass` - so the stricter type wasn't buying any actual safety. Replaces the old root-level EffectComposer.test.tsx (superseded, predates this suite and used a different mock-root setup) with a suite covering pass registration/composition order, StrictMode disposal, and the reorder/dedupe behavior above. (ColorAverage-specific dispose coverage lands with the primitive-effect dispose fix, a few commits later - ColorAverage doesn't dispose itself until then.)
kvvasuu
force-pushed
the
fix/03-effectcomposer-leak-and-reorder
branch
from
July 30, 2026 21:33
0d18afa to
76b09e5
Compare
Member
|
The bug was addressed here: pmndrs/react-three-fiber#3808 I'll cut a release after doing some quick hardening checks. |
Member
|
R3F v9.7.0 is out so I think remove the workaround (verify that it works correctly too) and then this is ready to go. |
…3f 9.7.0 r3f 9.7.0 fixes the reconciler bug this workaround existed for: internal instance children were drifting out of sync on a keyed reorder (reported upstream by us, fixed in pmndrs/react-three-fiber#3808). With the fix in place, group.current.__r3f.children no longer contains a stale duplicate after React moves a keyed child, so the dedupe step is no longer needed. Bumped @react-three/fiber to ^9.7.0 and removed dedupeByLastOccurrence, reading groupInstance.children directly. Verified empirically: the existing reorder-with-key test passes on 9.7.0 without the workaround, and fails with the exact same symptom as before (Expected EffectPass with 2 effects) when temporarily downgraded back to 9.6.1 with the workaround still removed - confirming this is the actual upstream fix taking effect, not a coincidence.
Collaborator
Author
|
Bumped the dependency and dropped dedupeByLastOccurrence accordingly. All tests passed. Also verified empirically. |
krispya
approved these changes
Jul 31, 2026
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.
Fixes #270
EffectComposerImpl was created inside useMemo, which doesn't guarantee its cleanup runs before React discards a memoized value - this leaked the composer (and its WebGL resources) under StrictMode. It's now created and disposed inside a useState/useEffect lifecycle instead.
Pass collection walks the real r3f instance tree (group.current.__r3f .children) and rebuilds the pass list from scratch whenever it or the composer changes, so the order always matches current JSX - including through wrapper components - after a reorder or a remount.
While testing the reorder case, found a genuine bug in r3f's host config: when React moves (not remounts) an existing child - e.g. a key-preserving reorder of a keyed effect list - insertBefore/ appendChild splice the moved instance into its new slot without detaching it from the old one first, leaving a stale duplicate in Instance.children. Worth reporting upstream. Added a cheap dedupe (keep each object's last occurrence, sorted by that position) that recovers the correct order either way and is a no-op when the bug isn't present.
Also widens
childrenfrom JSX.Element | JSX.Element[] to ReactNode, which was rejecting the commoncondition && <Effect/>idiom (that expression isfalse | Element, andfalseisn't assignable to either half of the union). The tree-walk already tolerates arbitrary children gracefully - it just filters forinstanceof Effect || instanceof Pass- so the stricter type wasn't buying any actual safety.Replaces the old root-level EffectComposer.test.tsx (superseded, predates this suite and used a different mock-root setup) with a suite covering pass registration/composition order, StrictMode disposal, and the reorder/dedupe behavior above. (ColorAverage-specific dispose coverage lands with the primitive-effect dispose fix, a few commits later - ColorAverage doesn't dispose itself until then.)