From 743eea1f951f3dda4d05037b8becc3517fbae79b Mon Sep 17 00:00:00 2001 From: Alan Lee Date: Fri, 19 Jul 2024 17:20:27 -0700 Subject: [PATCH] fix ConcurrentModificationException in ReactScrollViewHelper (#45550) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/45550 `ConcurrentModificationException` is happening from `emitScrollEvent()`. This usually happens if we modify the collection (add/remove) while accessing collection in a foreach loop. Seems likely add/remove is called from a different thread while in the foreach loop. Converting to list before we do the foreach as a quick fix. Changelog: [Internal] - quick fix for exception Issure reported here: https://fb.workplace.com/groups/rn.support/permalink/26557068097248454/ Reviewed By: mdvacca Differential Revision: D59991739 --- .../com/facebook/react/views/scroll/ReactScrollViewHelper.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewHelper.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewHelper.kt index 633b26dd1c59f6..51e55bbf7b1714 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewHelper.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewHelper.kt @@ -113,7 +113,7 @@ public object ReactScrollViewHelper { return } val contentView = scrollView.getChildAt(0) ?: return - for (scrollListener in scrollListeners) { + for (scrollListener in scrollListeners.toList()) { scrollListener.onScroll(scrollView, scrollEventType, xVelocity, yVelocity) } val reactContext = scrollView.context as ReactContext @@ -146,7 +146,7 @@ public object ReactScrollViewHelper { /** This is only for Java listeners. onLayout events emitted to JS are handled elsewhere. */ @JvmStatic public fun emitLayoutEvent(scrollView: ViewGroup) { - for (scrollListener in scrollListeners) { + for (scrollListener in scrollListeners.toList()) { scrollListener.onLayout(scrollView) } }