You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix: Move destroy field to shared instance object (#26561)
This fixes the "double free" bug illustrated by the regression test
added in the previous commit.
The underlying issue is that `effect.destroy` field is a mutable field
but we read it during render. This is a concurrency bug — if we had a
borrow checker, it would not allow this.
It's rare in practice today because the field is updated during the
commit phase, which takes a lock on the fiber tree until all the effects
have fired. But it's still theoretically wrong because you can have
multiple Fiber copies each with their own reference to a single destroy
function, and indeed we discovered in production a scenario where this
happens via our current APIs.
In the future these types of scenarios will be much more common because
we will introduce features where effects may run concurrently with the
render phase — i.e. an imperative `hide` method that synchronously hides
a React tree and unmounts all its effects without entering the render
phase, and without interrupting a render phase that's already in
progress.
A future version of React may also be able to run the entire commit
phase concurrently with a subsequent render phase. We can't do this now
because our data structures are not fully thread safe (see: the Fiber
alternate model) but we should be able to do this in the future.
The fix I've introduced in this commit is to move the `destroy` field to
a separate object. The effect "instance" is a shared object that remains
the same for the entire lifetime of an effect. In Rust terms, a RefCell.
The field is `undefined` if the effect is unmounted, or if the effect
ran but is not stateful. We don't explicitly track whether the effect is
mounted or unmounted because that can be inferred by the hiddenness of
the fiber in the tree, i.e. whether there is a hidden Offscreen fiber
above it.
It's unfortunate that this is stored on a separate object, because it
adds more memory per effect instance, but it's conceptually sound. I
think there's likely a better data structure we could use for effects;
perhaps just one array of effect instances per fiber. But I think this
is OK for now despite the additional memory and we can follow up with
performance optimizations later.
---------
Co-authored-by: Dan Abramov <dan.abramov@gmail.com>
Co-authored-by: Rick Hanlon <rickhanlonii@gmail.com>
Co-authored-by: Jan Kassens <jan@kassens.net>
DiffTrain build for commit 85bb7b6.
Copy file name to clipboardExpand all lines: compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react-test-renderer/cjs/ReactTestRenderer-dev.js
+45-21Lines changed: 45 additions & 21 deletions
Original file line number
Diff line number
Diff line change
@@ -6005,7 +6005,21 @@ var didWarnAboutUseWrappedInTryCatch;
6005
6005
{
6006
6006
didWarnAboutMismatchedHooksForComponent = new Set();
6007
6007
didWarnAboutUseWrappedInTryCatch = new Set();
6008
-
} // These are set right before calling the component.
6008
+
} // The effect "instance" is a shared object that remains the same for the entire
6009
+
// lifetime of an effect. In Rust terms, a RefCell. We use it to store the
6010
+
// "destroy" function that is returned from an effect, because that is stateful.
6011
+
// The field is `undefined` if the effect is unmounted, or if the effect ran
6012
+
// but is not stateful. We don't explicitly track whether the effect is mounted
6013
+
// or unmounted because that can be inferred by the hiddenness of the fiber in
6014
+
// the tree, i.e. whether there is a hidden Offscreen fiber above it.
6015
+
//
6016
+
// It's unfortunate that this is stored on a separate object, because it adds
6017
+
// more memory per effect instance, but it's conceptually sound. I think there's
6018
+
// likely a better data structure we could use for effects; perhaps just one
6019
+
// array of effect instances per fiber. But I think this is OK for now despite
6020
+
// the additional memory and we can follow up with performance
6021
+
// optimizations later.
6022
+
// These are set right before calling the component.
6009
6023
6010
6024
var renderLanes$1 = NoLanes; // The work-in-progress fiber. I've named it differently to distinguish it from
6011
6025
// the work-in-progress hook.
@@ -6970,7 +6984,7 @@ function mountSyncExternalStore(subscribe, getSnapshot, getServerSnapshot) {
Copy file name to clipboardExpand all lines: compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react-test-renderer/cjs/ReactTestRenderer-prod.js
+39-34Lines changed: 39 additions & 34 deletions
Original file line number
Diff line number
Diff line change
@@ -2272,7 +2272,7 @@ function updateSyncExternalStore(subscribe, getSnapshot) {
0 commit comments