Skip to content

fix: Crash on Android 10 due to pointerIndex out of range in ReactScrollView with gesture-handler #3641

@buuhvprojects

Description

@buuhvprojects

WHAT IS THE DRIVING SCENARIO?

This crash, Fatal Exception: java.lang.IllegalArgumentException with message pointerIndex out of range , occurring in com.facebook.react.views.scroll.ReactScrollView.onTouchEvent , indicates a problem with how touch events are being handled in the Android native layer by the ReactScrollView component.
Given the information about react-native-gesture-handler and the forum post, the cause is an integration issue between ReactScrollView and react-native-gesture-handler , where intercepting the latter's event disrupts the former's expectations regarding pointer indices.

HOW TO REPRODUCE?

Use an Android 10 device.
Access a screen with ReactScrollView active.
End up with an input field activated with the keyboard showing. Repeatedly scrolling
The app may crash and close unexpectedly.

WHAT WAS DONE IN THIS PR?

Applied a mitigation patch related to the pointerIndex out of range issue, inspired by discussions and solutions in the react-native-gesture-handler repository

Ensured compatibility between ReactScrollView and react-native-gesture-handler to avoid conflicts in touch events.

Added additional protections in native handlers to prevent invalid pointer exceptions.

HOW TO TEST LOCALLY?

Run a production app because the issue doesn't occur with a debug app.
Install the app on a device with Android 10.
Keep an input field activated with the keyboard showing.
Keep scrolling up and down until it crashes.

Today I used patch-package to patch react-native-gesture-handler@2.12.1 for the project I'm working on.

Here is the diff that solved my problem:

diff --git a/node_modules/react-native-gesture-handler/android/src/main/java/com/swmansion/gesturehandler/react/RNGestureHandlerRootView.kt b/node_modules/react-native-gesture-handler/android/src/main/java/com/swmansion/gesturehandler/react/RNGestureHandlerRootView.kt
index 953d3f3..f0e1ca5 100644
--- a/node_modules/react-native-gesture-handler/android/src/main/java/com/swmansion/gesturehandler/react/RNGestureHandlerRootView.kt
+++ b/node_modules/react-native-gesture-handler/android/src/main/java/com/swmansion/gesturehandler/react/RNGestureHandlerRootView.kt
@@ -32,9 +32,14 @@ class RNGestureHandlerRootView(context: Context?) : ReactViewGroup(context) {
   }
 
   override fun dispatchTouchEvent(ev: MotionEvent) =
-    if (_enabled && rootHelper!!.dispatchTouchEvent(ev)) {
-      true
-    } else super.dispatchTouchEvent(ev)
+    try {
+      if (_enabled && rootHelper!!.dispatchTouchEvent(ev)) {
+        true
+      } else super.dispatchTouchEvent(ev)
+    } catch (IllegalArgumentException e) {
+      e.printStackTrace();
+      false
+    }
 
   override fun requestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {
     if (_enabled) {

This issue body was partially generated by patch-package.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions