Skip to content

Commit 3d96794

Browse files
rubennortefacebook-github-bot
authored andcommitted
Avoid errors when dispatching mount operations within mount hooks
Summary: 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. Differential Revision: D71387739
1 parent 59f421c commit 3d96794

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

Lines changed: 11 additions & 8 deletions
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
@@ -1254,12 +1254,13 @@ public void didMountItems(@Nullable List<MountItem> mountItems) {
12541254

12551255
// Collect surface IDs for all the mount items
12561256
for (MountItem mountItem : mountItems) {
1257-
if (mountItem != null && !mMountedSurfaceIds.contains(mountItem.getSurfaceId())) {
1258-
mMountedSurfaceIds.add(mountItem.getSurfaceId());
1257+
if (mountItem != null
1258+
&& !mSurfaceIdsWithPendingMountNotification.contains(mountItem.getSurfaceId())) {
1259+
mSurfaceIdsWithPendingMountNotification.add(mountItem.getSurfaceId());
12591260
}
12601261
}
12611262

1262-
if (!mMountNotificationScheduled && !mMountedSurfaceIds.isEmpty()) {
1263+
if (!mMountNotificationScheduled && !mSurfaceIdsWithPendingMountNotification.isEmpty()) {
12631264
mMountNotificationScheduled = true;
12641265

12651266
// Notify mount when the effects are visible and prevent mount hooks to
@@ -1271,17 +1272,19 @@ public void didMountItems(@Nullable List<MountItem> mountItems) {
12711272
public void run() {
12721273
mMountNotificationScheduled = false;
12731274

1275+
// Create a copy in case mount hooks trigger more mutations
1276+
final List<Integer> surfaceIdsToReportMount =
1277+
mSurfaceIdsWithPendingMountNotification;
1278+
mSurfaceIdsWithPendingMountNotification = new ArrayList<>();
1279+
12741280
final @Nullable FabricUIManagerBinding binding = mBinding;
12751281
if (binding == null || mDestroyed) {
1276-
mMountedSurfaceIds.clear();
12771282
return;
12781283
}
12791284

1280-
for (int surfaceId : mMountedSurfaceIds) {
1285+
for (int surfaceId : surfaceIdsToReportMount) {
12811286
binding.reportMount(surfaceId);
12821287
}
1283-
1284-
mMountedSurfaceIds.clear();
12851288
}
12861289
});
12871290
}

0 commit comments

Comments
 (0)