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

Đang mở Phù hợp với người mới
#140 1 bình luận 1 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
88/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
android, kotlin
Lĩnh vực
mobile

Hướng nghiên cứu

Đọc ScrollIndicators.kt, đặc biệt là refit() và releaseAll(), nơi issue báo cáo views.toList() ở cả hai chỗ. Thay các bản sao này bằng cách tiếp cận được đề xuất là dùng ArrayList(views), sau đó chạy các bài kiểm thử Android liên quan hoặc tái hiện trường hợp garbage collection bị buộc được mô tả trong issue. Công việc hoàn tất khi cả hai đường xử lý đều tránh được NoSuchElementException đã báo cáo khi một view được giữ bằng tham chiếu yếu bị thu gom.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Kotlin
Star
536
Fork
24
Merge trung bình
12 giờ 10 phút
Pull request đã merge (30 ngày)
22

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của software-mansion/react-native-bottom-sheet

Tất cả issue của software-mansion/react-native-bottom-sheet

Issue tương tự

Thêm issue về Kotlin

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.