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

オープン 初心者向け
#140 コメント 1 件 リアクション 1 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
88/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
android, kotlin
領域
mobile

調査の方向性

ScrollIndicators.ktを読み、特にrefit()とreleaseAll()を確認してください。issueでは両方の箇所にあるviews.toList()が報告されています。これらのコピーを、提案されているArrayList(views)を使う方法に置き換え、その後、該当するAndroidテストを実行するか、issueに記載された強制ガベージコレクションのケースを再現してください。弱参照で保持されたviewが回収されたときに、両方の経路で報告されたNoSuchElementExceptionが発生しなくなれば完了です。

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
Kotlin
スター
536
フォーク
24
平均マージ
12時間 10分
マージ済み PR(30日)
22

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

software-mansion/react-native-bottom-sheet のほかの issue

software-mansion/react-native-bottom-sheet の issue をすべて見る

似ている issue

Kotlin の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。