Skip to content

Commit af72108

Browse files
Oleh Malanchukfacebook-github-bot
authored andcommitted
Fix SurfaceMountingManager leaking activity from stopped surfaces (#44584)
Summary: Pull Request resolved: #44584 Changelog: [Android][Fixed] Surfaces no longer leak activity once stopped Reviewed By: javache Differential Revision: D57367419 fbshipit-source-id: 7aa69256284f97679ebcc3309f2b74650ec3fb51
1 parent 044aadb commit af72108

20 files changed

+133
-27
lines changed

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/SurfaceMountingManager.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.facebook.react.fabric.events.EventEmitterWrapper;
3636
import com.facebook.react.fabric.mounting.MountingManager.MountItemExecutor;
3737
import com.facebook.react.fabric.mounting.mountitems.MountItem;
38+
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags;
3839
import com.facebook.react.modules.core.ReactChoreographer;
3940
import com.facebook.react.touch.JSResponderHandler;
4041
import com.facebook.react.uimanager.IViewGroupManager;
@@ -301,6 +302,9 @@ public void stopSurface() {
301302
mRootViewManager = null;
302303
mMountItemExecutor = null;
303304
mThemedReactContext = null;
305+
if (ReactNativeFeatureFlags.fixStoppedSurfaceRemoveDeleteTreeUIFrameCallbackLeak()) {
306+
mRemoveDeleteTreeUIFrameCallback = null;
307+
}
304308
mOnViewAttachMountItems.clear();
305309

306310
if (ReactFeatureFlags.enableViewRecycling) {

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlags.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<c35ee97cf5c4b5f77cdd045341f2848b>>
7+
* @generated SignedSource<<fbc717f14e387b92aea7cb71202cce95>>
88
*/
99

1010
/**
@@ -94,6 +94,12 @@ public object ReactNativeFeatureFlags {
9494
@JvmStatic
9595
public fun enableUIConsistency(): Boolean = accessor.enableUIConsistency()
9696

97+
/**
98+
* Fixes a leak in SurfaceMountingManager.mRemoveDeleteTreeUIFrameCallback
99+
*/
100+
@JvmStatic
101+
public fun fixStoppedSurfaceRemoveDeleteTreeUIFrameCallbackLeak(): Boolean = accessor.fixStoppedSurfaceRemoveDeleteTreeUIFrameCallbackLeak()
102+
97103
/**
98104
* Forces the mounting layer on Android to always batch mount items instead of dispatching them immediately. This might fix some crashes related to synchronous state updates, where some views dispatch state updates during mount.
99105
*/

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsCxxAccessor.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<1b84e5fa96120a511db6f831afb73eab>>
7+
* @generated SignedSource<<cfbb7879b105079fe76e37b0683f7083>>
88
*/
99

1010
/**
@@ -31,6 +31,7 @@ public class ReactNativeFeatureFlagsCxxAccessor : ReactNativeFeatureFlagsAccesso
3131
private var enableMicrotasksCache: Boolean? = null
3232
private var enableSynchronousStateUpdatesCache: Boolean? = null
3333
private var enableUIConsistencyCache: Boolean? = null
34+
private var fixStoppedSurfaceRemoveDeleteTreeUIFrameCallbackLeakCache: Boolean? = null
3435
private var forceBatchingMountItemsOnAndroidCache: Boolean? = null
3536
private var inspectorEnableCxxInspectorPackagerConnectionCache: Boolean? = null
3637
private var inspectorEnableModernCDPRegistryCache: Boolean? = null
@@ -139,6 +140,15 @@ public class ReactNativeFeatureFlagsCxxAccessor : ReactNativeFeatureFlagsAccesso
139140
return cached
140141
}
141142

143+
override fun fixStoppedSurfaceRemoveDeleteTreeUIFrameCallbackLeak(): Boolean {
144+
var cached = fixStoppedSurfaceRemoveDeleteTreeUIFrameCallbackLeakCache
145+
if (cached == null) {
146+
cached = ReactNativeFeatureFlagsCxxInterop.fixStoppedSurfaceRemoveDeleteTreeUIFrameCallbackLeak()
147+
fixStoppedSurfaceRemoveDeleteTreeUIFrameCallbackLeakCache = cached
148+
}
149+
return cached
150+
}
151+
142152
override fun forceBatchingMountItemsOnAndroid(): Boolean {
143153
var cached = forceBatchingMountItemsOnAndroidCache
144154
if (cached == null) {

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsCxxInterop.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<2cd7ab4688ca2179ba3a19e9a062f695>>
7+
* @generated SignedSource<<77b8e93114ed0cc2b6bdd9ee46e162c7>>
88
*/
99

1010
/**
@@ -50,6 +50,8 @@ public object ReactNativeFeatureFlagsCxxInterop {
5050

5151
@DoNotStrip @JvmStatic public external fun enableUIConsistency(): Boolean
5252

53+
@DoNotStrip @JvmStatic public external fun fixStoppedSurfaceRemoveDeleteTreeUIFrameCallbackLeak(): Boolean
54+
5355
@DoNotStrip @JvmStatic public external fun forceBatchingMountItemsOnAndroid(): Boolean
5456

5557
@DoNotStrip @JvmStatic public external fun inspectorEnableCxxInspectorPackagerConnection(): Boolean

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsDefaults.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<a2c4f2c950a7b92163fdc6219864d6ca>>
7+
* @generated SignedSource<<13a5d73dea53b8d4f16946cebc49e684>>
88
*/
99

1010
/**
@@ -45,6 +45,8 @@ public open class ReactNativeFeatureFlagsDefaults : ReactNativeFeatureFlagsProvi
4545

4646
override fun enableUIConsistency(): Boolean = false
4747

48+
override fun fixStoppedSurfaceRemoveDeleteTreeUIFrameCallbackLeak(): Boolean = false
49+
4850
override fun forceBatchingMountItemsOnAndroid(): Boolean = false
4951

5052
override fun inspectorEnableCxxInspectorPackagerConnection(): Boolean = false

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsLocalAccessor.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<48f657b282ef658ab7e9024f470d37ad>>
7+
* @generated SignedSource<<67f1828f6a4cf9b2cf808be12d3fb06b>>
88
*/
99

1010
/**
@@ -35,6 +35,7 @@ public class ReactNativeFeatureFlagsLocalAccessor : ReactNativeFeatureFlagsAcces
3535
private var enableMicrotasksCache: Boolean? = null
3636
private var enableSynchronousStateUpdatesCache: Boolean? = null
3737
private var enableUIConsistencyCache: Boolean? = null
38+
private var fixStoppedSurfaceRemoveDeleteTreeUIFrameCallbackLeakCache: Boolean? = null
3839
private var forceBatchingMountItemsOnAndroidCache: Boolean? = null
3940
private var inspectorEnableCxxInspectorPackagerConnectionCache: Boolean? = null
4041
private var inspectorEnableModernCDPRegistryCache: Boolean? = null
@@ -154,6 +155,16 @@ public class ReactNativeFeatureFlagsLocalAccessor : ReactNativeFeatureFlagsAcces
154155
return cached
155156
}
156157

158+
override fun fixStoppedSurfaceRemoveDeleteTreeUIFrameCallbackLeak(): Boolean {
159+
var cached = fixStoppedSurfaceRemoveDeleteTreeUIFrameCallbackLeakCache
160+
if (cached == null) {
161+
cached = currentProvider.fixStoppedSurfaceRemoveDeleteTreeUIFrameCallbackLeak()
162+
accessedFeatureFlags.add("fixStoppedSurfaceRemoveDeleteTreeUIFrameCallbackLeak")
163+
fixStoppedSurfaceRemoveDeleteTreeUIFrameCallbackLeakCache = cached
164+
}
165+
return cached
166+
}
167+
157168
override fun forceBatchingMountItemsOnAndroid(): Boolean {
158169
var cached = forceBatchingMountItemsOnAndroidCache
159170
if (cached == null) {

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsProvider.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<0f3d31f94f4bded41936fe4ecafbdd4a>>
7+
* @generated SignedSource<<a6a65fe7d9b04671de407a03e1feb161>>
88
*/
99

1010
/**
@@ -45,6 +45,8 @@ public interface ReactNativeFeatureFlagsProvider {
4545

4646
@DoNotStrip public fun enableUIConsistency(): Boolean
4747

48+
@DoNotStrip public fun fixStoppedSurfaceRemoveDeleteTreeUIFrameCallbackLeak(): Boolean
49+
4850
@DoNotStrip public fun forceBatchingMountItemsOnAndroid(): Boolean
4951

5052
@DoNotStrip public fun inspectorEnableCxxInspectorPackagerConnection(): Boolean

packages/react-native/ReactAndroid/src/main/jni/react/featureflags/JReactNativeFeatureFlagsCxxInterop.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<630ef8f9e65223c8d4c2b07bcc0ebadd>>
7+
* @generated SignedSource<<ad6b81206d1f410cd6c8d9109bd340bd>>
88
*/
99

1010
/**
@@ -105,6 +105,12 @@ class ReactNativeFeatureFlagsProviderHolder
105105
return method(javaProvider_);
106106
}
107107

108+
bool fixStoppedSurfaceRemoveDeleteTreeUIFrameCallbackLeak() override {
109+
static const auto method =
110+
getReactNativeFeatureFlagsProviderJavaClass()->getMethod<jboolean()>("fixStoppedSurfaceRemoveDeleteTreeUIFrameCallbackLeak");
111+
return method(javaProvider_);
112+
}
113+
108114
bool forceBatchingMountItemsOnAndroid() override {
109115
static const auto method =
110116
getReactNativeFeatureFlagsProviderJavaClass()->getMethod<jboolean()>("forceBatchingMountItemsOnAndroid");
@@ -212,6 +218,11 @@ bool JReactNativeFeatureFlagsCxxInterop::enableUIConsistency(
212218
return ReactNativeFeatureFlags::enableUIConsistency();
213219
}
214220

221+
bool JReactNativeFeatureFlagsCxxInterop::fixStoppedSurfaceRemoveDeleteTreeUIFrameCallbackLeak(
222+
facebook::jni::alias_ref<JReactNativeFeatureFlagsCxxInterop> /*unused*/) {
223+
return ReactNativeFeatureFlags::fixStoppedSurfaceRemoveDeleteTreeUIFrameCallbackLeak();
224+
}
225+
215226
bool JReactNativeFeatureFlagsCxxInterop::forceBatchingMountItemsOnAndroid(
216227
facebook::jni::alias_ref<JReactNativeFeatureFlagsCxxInterop> /*unused*/) {
217228
return ReactNativeFeatureFlags::forceBatchingMountItemsOnAndroid();
@@ -302,6 +313,9 @@ void JReactNativeFeatureFlagsCxxInterop::registerNatives() {
302313
makeNativeMethod(
303314
"enableUIConsistency",
304315
JReactNativeFeatureFlagsCxxInterop::enableUIConsistency),
316+
makeNativeMethod(
317+
"fixStoppedSurfaceRemoveDeleteTreeUIFrameCallbackLeak",
318+
JReactNativeFeatureFlagsCxxInterop::fixStoppedSurfaceRemoveDeleteTreeUIFrameCallbackLeak),
305319
makeNativeMethod(
306320
"forceBatchingMountItemsOnAndroid",
307321
JReactNativeFeatureFlagsCxxInterop::forceBatchingMountItemsOnAndroid),

packages/react-native/ReactAndroid/src/main/jni/react/featureflags/JReactNativeFeatureFlagsCxxInterop.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<39dde965f082a0dffbe00763e184d1db>>
7+
* @generated SignedSource<<eb5c66cf6c5ceb117b7cad784cbe20a1>>
88
*/
99

1010
/**
@@ -63,6 +63,9 @@ class JReactNativeFeatureFlagsCxxInterop
6363
static bool enableUIConsistency(
6464
facebook::jni::alias_ref<JReactNativeFeatureFlagsCxxInterop>);
6565

66+
static bool fixStoppedSurfaceRemoveDeleteTreeUIFrameCallbackLeak(
67+
facebook::jni::alias_ref<JReactNativeFeatureFlagsCxxInterop>);
68+
6669
static bool forceBatchingMountItemsOnAndroid(
6770
facebook::jni::alias_ref<JReactNativeFeatureFlagsCxxInterop>);
6871

packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlags.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<d176ed0be7b015e7b9444a0b7ac7f7ba>>
7+
* @generated SignedSource<<5001001b2979f9b759ddac1b035842bf>>
88
*/
99

1010
/**
@@ -65,6 +65,10 @@ bool ReactNativeFeatureFlags::enableUIConsistency() {
6565
return getAccessor().enableUIConsistency();
6666
}
6767

68+
bool ReactNativeFeatureFlags::fixStoppedSurfaceRemoveDeleteTreeUIFrameCallbackLeak() {
69+
return getAccessor().fixStoppedSurfaceRemoveDeleteTreeUIFrameCallbackLeak();
70+
}
71+
6872
bool ReactNativeFeatureFlags::forceBatchingMountItemsOnAndroid() {
6973
return getAccessor().forceBatchingMountItemsOnAndroid();
7074
}

0 commit comments

Comments
 (0)