Skip to content

Commit aa75e94

Browse files
rubennortereact-native-bot
authored andcommitted
Avoid errors when dispatching mount operations within mount hooks (#50091)
Summary: Pull Request resolved: #50091 Changelog: [internal] If a library uses mount hooks to perform mount operations, it's possible to get concurrent modifications of the list of pending surface IDs to report. This fixes that potential error by making a copy of the list before dispatching the mount notifications. Fixes #49783. Reviewed By: javache Differential Revision: D71387739 fbshipit-source-id: 96c723ef2d6bcc659c4452434b7a4d5af26117ef
1 parent eb8c1c3 commit aa75e94

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

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

+11-8
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public class FabricUIManager
173173
private final CopyOnWriteArrayList<UIManagerListener> mListeners = new CopyOnWriteArrayList<>();
174174

175175
private boolean mMountNotificationScheduled = false;
176-
private final List<Integer> mMountedSurfaceIds = new ArrayList<>();
176+
private List<Integer> mSurfaceIdsWithPendingMountNotification = new ArrayList<>();
177177

178178
@ThreadConfined(UI)
179179
@NonNull
@@ -1250,12 +1250,13 @@ public void didMountItems(@Nullable List<MountItem> mountItems) {
12501250

12511251
// Collect surface IDs for all the mount items
12521252
for (MountItem mountItem : mountItems) {
1253-
if (mountItem != null && !mMountedSurfaceIds.contains(mountItem.getSurfaceId())) {
1254-
mMountedSurfaceIds.add(mountItem.getSurfaceId());
1253+
if (mountItem != null
1254+
&& !mSurfaceIdsWithPendingMountNotification.contains(mountItem.getSurfaceId())) {
1255+
mSurfaceIdsWithPendingMountNotification.add(mountItem.getSurfaceId());
12551256
}
12561257
}
12571258

1258-
if (!mMountNotificationScheduled && !mMountedSurfaceIds.isEmpty()) {
1259+
if (!mMountNotificationScheduled && !mSurfaceIdsWithPendingMountNotification.isEmpty()) {
12591260
mMountNotificationScheduled = true;
12601261

12611262
// Notify mount when the effects are visible and prevent mount hooks to
@@ -1267,17 +1268,19 @@ public void didMountItems(@Nullable List<MountItem> mountItems) {
12671268
public void run() {
12681269
mMountNotificationScheduled = false;
12691270

1271+
// Create a copy in case mount hooks trigger more mutations
1272+
final List<Integer> surfaceIdsToReportMount =
1273+
mSurfaceIdsWithPendingMountNotification;
1274+
mSurfaceIdsWithPendingMountNotification = new ArrayList<>();
1275+
12701276
final @Nullable FabricUIManagerBinding binding = mBinding;
12711277
if (binding == null || mDestroyed) {
1272-
mMountedSurfaceIds.clear();
12731278
return;
12741279
}
12751280

1276-
for (int surfaceId : mMountedSurfaceIds) {
1281+
for (int surfaceId : surfaceIdsToReportMount) {
12771282
binding.reportMount(surfaceId);
12781283
}
1279-
1280-
mMountedSurfaceIds.clear();
12811284
}
12821285
});
12831286
}

0 commit comments

Comments
 (0)