Skip to content

[Android] Fix ConcurrentModificationException in InteropUIBlockListener #51631

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

jkoutavas
Copy link
Contributor

@jkoutavas jkoutavas commented May 27, 2025

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 #49783, and although React Native PR#50091 was closed and not merged, the work in that PR did make it to React Native in commit 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

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:

image

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?

@facebook-github-bot
Copy link
Contributor

Hi @jkoutavas!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@jkoutavas jkoutavas changed the title [Android] Fix ConcurrentModificationException in InteropUIBlockListene [Android] Fix ConcurrentModificationException in InteropUIBlockListener May 27, 2025
@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. labels May 27, 2025
@jkoutavas jkoutavas requested a review from javache May 27, 2025 17:53
@jkoutavas
Copy link
Contributor Author

I did a cleanup of the changes and retested on our app. All checked-out okay.

@jkoutavas jkoutavas requested a review from javache May 27, 2025 20:39
@jkoutavas jkoutavas requested a review from javache May 27, 2025 21:10
@jkoutavas jkoutavas requested a review from javache May 28, 2025 12:46
@javache
Copy link
Member

javache commented May 28, 2025

Sorry, my previous comment was wrong. Synchronized is a recursive lock in JVM, so you can still have concurrent modification here. The right fix is to do the copy in a synchronized block though.

@jkoutavas
Copy link
Contributor Author

Sorry, my previous comment was wrong. Synchronized is a recursive lock in JVM, so you can still have concurrent modification here. The right fix is to do the copy in a synchronized block though.

No problem, @javache, I reverted the latest commit.

@javache
Copy link
Member

javache commented May 28, 2025

The copying of beforeUiblocks and clearing of it still needs to happen in a synchronized block.

@jkoutavas
Copy link
Contributor Author

The copying of beforeUiblocks and clearing of it still needs to happen in a synchronized block.

✔️

@facebook-github-bot
Copy link
Contributor

@javache has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@jkoutavas
Copy link
Contributor Author

@javache thank you so much for helping me get through this PR and being so responsive. It was really unexpectedly wonderful. Having never tried to open a PR against such a big repo before, I didn't know how long this was going to take, so I had expected "awhile."

Now that you've imported it into your own system, can you have this be a fix for not only the main line but also for v0.79.x?

@jkoutavas jkoutavas requested a review from cortinico May 29, 2025 17:54
@jkoutavas
Copy link
Contributor Author

Now that you've imported it into your own system, can you have this be a fix for not only the main line but also for v0.79.x?

Ah, react-native-releases may become my eventual stop for a "Pick Request"?

@facebook-github-bot facebook-github-bot added the Merged This PR has been merged. label May 30, 2025
@facebook-github-bot
Copy link
Contributor

@javache merged this pull request in 48b2dd3.

@react-native-bot
Copy link
Collaborator

This pull request was successfully merged by @jkoutavas in 48b2dd3

When will my fix make it into a release? | How to file a pick request?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants