Android: NoSuchElementException in ScrollIndicators.refit when a scroll view is garbage collected
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 88/100
Research direction
Read ScrollIndicators.kt, especially refit() and releaseAll(), where the issue reports views.toList() in both places. Replace those copies with the suggested ArrayList(views) approach, then run the relevant Android tests or reproduce the forced garbage collection case described in the issue. Done means both paths avoid the reported NoSuchElementException when a weakly held view is collected.
Written by the indexing model from the issue text.
Description
Description
On Android, 1.1.0 crashes with a fatal java.util.NoSuchElementException while a sheet moves. We see it in production on Android 12 and 17 (Samsung devices).
java.util.NoSuchElementException
at java.util.WeakHashMap$HashIterator.nextEntry(WeakHashMap.java:808)
at java.util.WeakHashMap$KeyIterator.next(WeakHashMap.java:839)
at kotlin.collections.CollectionsKt___CollectionsKt.toList(_Collections.kt:1346)
at com.swmansion.reactnativebottomsheet.host.ScrollIndicators.refit(ScrollIndicators.kt:52)
at com.swmansion.reactnativebottomsheet.host.Snapping.emitPosition(Snapping.kt:128)
at com.swmansion.reactnativebottomsheet.host.Snapping.snapToIndex$lambda$9$lambda$7(Snapping.kt:230)
at androidx.dynamicanimation.animation.DynamicAnimation.setPropertyValue(DynamicAnimation.java:729)
at androidx.dynamicanimation.animation.DynamicAnimation.doAnimationFrame(DynamicAnimation.java:675)
Cause
ScrollIndicators.views is Collections.newSetFromMap(WeakHashMap()), and refit() / releaseAll() copy it with views.toList(). For a collection of size 1, Kotlin's toList() reads size, then calls iterator().next() without hasNext(). If the garbage collector clears the only view between the two calls, the WeakHashMap iterator has no entry left and throws NoSuchElementException (not a ConcurrentModificationException, since clearing a weak key does not change modCount).
We could reproduce the same stack on an emulator by forcing the key to disappear between size() and iterator().
Suggested fix
Copy the set with ArrayList(views) in refit() and releaseAll(): it goes through AbstractCollection.toArray(), which checks hasNext() before each element. We applied this as a local patch and the forced repro no longer crashes.
- for (view in views.toList()) {
+ for (view in ArrayList(views)) {
main still has views.toList() at both places.
- Dominant language
- Kotlin
- Stars
- 536
- Forks
- 24
- Avg merge
- 12h 10m
- Merged PRs (30d)
- 22
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from software-mansion/react-native-bottom-sheet
-
Difficulty 1/5 Under an hour Newbie friendliness 35/100
software-mansion/react-native-bottom-sheet#137 ·
Maintainers usually reply within 1 day
-
Feature request: opt-in native keyboard avoidancePossibly taken @Asad-Butt claimed this 5 days ago. Open
Difficulty 4/5 3-5 days Newbie friendliness 22/100
software-mansion/react-native-bottom-sheet#134 · 1 comment · 2 reactions ·
Maintainers usually reply within 1 day
-
bug
Difficulty 4/5 3-5 days Newbie friendliness 48/100
software-mansion/react-native-bottom-sheet#132 · 1 comment ·
Maintainers usually reply within 1 day
-
enhancement
Difficulty 4/5 3-5 days Newbie friendliness 52/100
software-mansion/react-native-bottom-sheet#74 · 1 comment ·
Maintainers usually reply within 1 day
-
enhancement
Difficulty 5/5 Over a week Newbie friendliness 35/100
software-mansion/react-native-bottom-sheet#64 · 1 comment ·
Maintainers usually reply within 1 day
All issues in software-mansion/react-native-bottom-sheet
Similar issues
-
Remove Collect againOpen
Difficulty 1/5 1-3 hours Newbie friendliness 86/100
thisisartman/Meguri#211 ·
Maintainers usually reply within 1 day
-
follow-up
Difficulty 1/5 Under an hour Newbie friendliness 75/100
Maintainers usually reply within 1 day
-
area:android fleet
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
bug needs-triage
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
emm3000/myGame#2175 · 1 comment ·
Maintainers usually reply within 1 day