Skip to content

Commit 48b2dd3

Browse files
jkoutavasfacebook-github-bot
authored andcommitted
Fix ConcurrentModificationException in InteropUIBlockListener (#51631)
Summary: This React Native Android fix hardens the Fabric `InteropUiBlockListener` against `ConcurrentModificationException` crashes in `willMountItems` and `didMountItems`. By iterating over a shallow copy of the UI‐block lists and catching any mid‐iteration mutations, we ensure the listener never throws during a UI frame and always clears its pending blocks. The issue was first reported in [React Native Issue https://github.com/facebook/react-native/issues/49783](https://github.com/facebook/react-native/issues/49783), and although [React Native PR#50091](#50091) was closed and not merged, the work in that PR did make it to React Native in [commit 17da3cb](17da3cb). However, the fix didn't go far enough. We saw intermittent examples of this exception being thrown when swiping through a carousel from `react-native-reanimated-carousel`. This fix goes right to the exception site itself. ## Changelog: [ANDROID] [FIXED] - Hardened the Fabric `InteropUiBlockListener` against `ConcurrentModificationException` crashes in `willMountItems` and `didMountItems` Pull Request resolved: #51631 Test Plan: The only way to test this is to develop a standalone app that emulates what we've been seeing in our commercially available RN app. We have done extensive testing of before (intermittent crashes) and after the fix (no crashes) and things have been standing up very well for us. Here is a the Red Box we see right at the time of the crash, before this fix: <img width="414" alt="image" src="https://github.com/user-attachments/assets/8d1b6c6d-42f7-48a0-9574-2f05436547d4" /> And here is the beginning of the logcat crash log: ``` 2025-05-07 16:01:49.212 unknown:BridgelessReact com.aura.suite W ReactHost{0}.handleHostException(message = "null") 2025-05-07 16:01:49.212 unknown:ReactNative com.aura.suite E Exception in native call java.util.ConcurrentModificationException at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:1111) at java.util.ArrayList$Itr.next(ArrayList.java:1064) at com.facebook.react.fabric.internal.interop.InteropUIBlockListener.willMountItems(InteropUiBlockListener.kt:72) at com.facebook.react.fabric.FabricUIManager$MountItemDispatchListener.willMountItems(FabricUIManager.java:1235) at com.facebook.react.fabric.mounting.MountItemDispatcher.dispatchMountItems(MountItemDispatcher.java:184) at com.facebook.react.fabric.mounting.MountItemDispatcher.tryDispatchMountItems(MountItemDispatcher.java:122) at com.facebook.react.fabric.FabricUIManager$3.runGuarded(FabricUIManager.java:820) at com.facebook.react.bridge.GuardedRunnable.run(GuardedRunnable.java:29) at com.facebook.react.fabric.FabricUIManager.scheduleMountItem(FabricUIManager.java:824) at com.facebook.react.fabric.FabricUIManagerBinding.reportMount(Native Method) at com.facebook.react.fabric.FabricUIManager$MountItemDispatchListener$1.run(FabricUIManager.java:1282) at android.os.Handler.handleCallback(Handler.java:959) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loopOnce(Looper.java:232) at android.os.Looper.loop(Looper.java:317) at android.app.ActivityThread.main(ActivityThread.java:8592) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:580) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:878) ``` All of this goes away with this fix. We're using React Native 0.79.2 and this is the first time we've open a PR for react-native. I hope this is enough info as far as testing goes. Can we see a 0.79.x release with this fix, please? Reviewed By: Abbondanzo, cortinico Differential Revision: D75594791 Pulled By: javache fbshipit-source-id: 982ae27e89756fdb290a24b0bdfa67c2e47c04e3
1 parent 63f4fb1 commit 48b2dd3

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/internal/interop/InteropUiBlockListener.kt

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
package com.facebook.react.fabric.internal.interop
1111

12+
import androidx.annotation.VisibleForTesting
1213
import com.facebook.react.bridge.UIManager
1314
import com.facebook.react.bridge.UIManagerListener
1415
import com.facebook.react.common.annotations.UnstableReactNativeAPI
@@ -24,9 +25,9 @@ import com.facebook.react.fabric.interop.UIBlockViewResolver
2425
*/
2526
@OptIn(UnstableReactNativeAPI::class)
2627
internal class InteropUIBlockListener : UIManagerListener {
28+
@VisibleForTesting internal val beforeUIBlocks: MutableList<UIBlock> = mutableListOf()
2729

28-
internal val beforeUIBlocks: MutableList<UIBlock> = mutableListOf()
29-
internal val afterUIBlocks: MutableList<UIBlock> = mutableListOf()
30+
@VisibleForTesting internal val afterUIBlocks: MutableList<UIBlock> = mutableListOf()
3031

3132
@Synchronized
3233
fun prependUIBlock(block: UIBlock) {
@@ -39,27 +40,37 @@ internal class InteropUIBlockListener : UIManagerListener {
3940
}
4041

4142
override fun willMountItems(uiManager: UIManager) {
42-
if (beforeUIBlocks.isEmpty()) {
43+
if (uiManager !is UIBlockViewResolver) {
4344
return
4445
}
45-
beforeUIBlocks.forEach {
46-
if (uiManager is UIBlockViewResolver) {
47-
it.execute(uiManager)
48-
}
49-
}
50-
beforeUIBlocks.clear()
46+
47+
// avoid ConcurrentModificationException by iterating over a copy
48+
val blocks =
49+
synchronized(this) {
50+
if (beforeUIBlocks.isEmpty()) {
51+
return
52+
}
53+
beforeUIBlocks.toList().also { beforeUIBlocks.clear() }
54+
}
55+
56+
blocks.forEach { block -> block.execute(uiManager) }
5157
}
5258

5359
override fun didMountItems(uiManager: UIManager) {
54-
if (afterUIBlocks.isEmpty()) {
60+
if (uiManager !is UIBlockViewResolver) {
5561
return
5662
}
57-
afterUIBlocks.forEach {
58-
if (uiManager is UIBlockViewResolver) {
59-
it.execute(uiManager)
60-
}
61-
}
62-
afterUIBlocks.clear()
63+
64+
// avoid ConcurrentModificationException by iterating over a copy
65+
val blocks =
66+
synchronized(this) {
67+
if (afterUIBlocks.isEmpty()) {
68+
return
69+
}
70+
afterUIBlocks.toList().also { afterUIBlocks.clear() }
71+
}
72+
73+
blocks.forEach { block -> block.execute(uiManager) }
6374
}
6475

6576
override fun didDispatchMountItems(uiManager: UIManager) = didMountItems(uiManager)

0 commit comments

Comments
 (0)