[Android] Crash: NullPointerException in GestureHandler.isWithinBounds
Description
Description
Production crash on Android. GestureHandler.isWithinBounds throws a NullPointerException when handle() passes a null coordinateView:
// GestureHandler.kt:689
fun isWithinBounds(view: View?, posX: Float, posY: Float): Boolean {
if (RNSVGHitTester.isSvgElement(view!!)) { // <- NPEcoordinateView is null when the handler's view is null. As far as I can tell, view is only set to null in reset().
GestureHandlerOrchestrator.deliverEventToGestureHandlers copies gestureHandlers into a list before dispatching. If handling the event for one handler causes another handler later in that list to be cancelled or reset, the later handler can still get into handle() with view == null. Nothing checks for null before the !!.
The same code is in 3.4.0-nightly-20260921-e26231e9d.
Stack trace
Fatal Exception: java.lang.NullPointerException:
at com.swmansion.gesturehandler.core.GestureHandler.isWithinBounds(GestureHandler.kt:689)
at com.swmansion.gesturehandler.core.GestureHandler.handle(GestureHandler.kt:410)
at com.swmansion.gesturehandler.core.GestureHandlerOrchestrator.deliverEventToGestureHandler(GestureHandlerOrchestrator.kt:338)
at com.swmansion.gesturehandler.core.GestureHandlerOrchestrator.deliverEventToGestureHandlers(GestureHandlerOrchestrator.kt:299)
at com.swmansion.gesturehandler.core.GestureHandlerOrchestrator.onTouchEvent(GestureHandlerOrchestrator.kt:76)
at com.swmansion.gesturehandler.react.RNGestureHandlerRootHelper.dispatchTouchEvent(RNGestureHandlerRootHelper.kt:166)
at com.swmansion.gesturehandler.react.RNGestureHandlerRootView.dispatchTouchEvent(RNGestureHandlerRootView.kt:62)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3171)
...
at com.xiaomi.mirror.MiuiMirrorImpl.isHandled(MiuiMirrorImpl.java:150)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:9965)Steps to reproduce
I don't have a reliable repro. It shows up in Crashlytics from production users. I suspect it happens when a view with a gesture unmounts or its handler is dropped while a touch is still in progress, for example a bottom sheet or screen closing under the finger.
Suggested fix
In handle(), return early or cancel() when coordinateView is null. Alternatively, make isWithinBounds return false for a null view instead of using !!.
Environment
- react-native-gesture-handler: 3.3.0
- react-native: 0.87
- @swmansion/react-native-bottom-sheet: 0.16.2
- Platform: Android
- Device: Xiaomi (MIUI/HyperOS)
- Architecture: New Architecture (Fabric)
Steps to reproduce
Random in production.
A link to a Gist, an Expo Snack or a link to a repository based on this template that reproduces the bug.
https://description.provided.com
Gesture Handler version
3.3.0
React Native version
0.87
Platforms
Android
JavaScript runtime
Hermes
Workflow
React Native (without Expo)
Architecture
New Architecture (Fabric)
Build type
Release mode
Device
Real device
Device model
Xiaomi (MIUI/HyperOS)
Acknowledgements
Yes
Source: software-mansion/react-native-gesture-handler