Android: NoSuchElementException in ScrollIndicators.refit when a scroll view is garbage collected

Open Beginner friendly
#140 1 comment 1 reaction 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
88/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
android, kotlin
Domain
mobile

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from software-mansion/react-native-bottom-sheet

All issues in software-mansion/react-native-bottom-sheet

Similar issues

More Kotlin issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.