Don't dispatch orphaned touch events#4341
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes the root cause of malformed “orphaned” touch events when touch callbacks become attached mid-gesture, by ensuring native platforms don’t dispatch touch-up/move payloads for pointers that were never registered into the per-pointer data structures.
Changes:
- iOS/macOS: Skip emitting touch events for unregistered touches in
touchesBegan/touchesMoved/touchesEnded, and buildchangedTouchesonly from successfully registered pointers. - Android: Skip
dispatchTouchUpEventwhen the lifted pointer has no tracked pointer data, preventing orphaned UP events and counter underflow/corruption.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| packages/react-native-gesture-handler/apple/RNGestureHandlerPointerTracker.mm | Filters out unregistered touches from changedPointersData and avoids dispatching orphaned move/up/down touch events. |
| packages/react-native-gesture-handler/android/src/main/java/com/swmansion/gesturehandler/core/GestureHandler.kt | Adds an early return to prevent dispatching orphaned UP events when the pointer was never tracked in trackedPointers. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
j-piasecki
approved these changes
Jul 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Follow-up to #4316, fixing the root cause of the malformed touch events on the native side.
GestureHandleron Android keeps two parallel pointer structures with different lifecycles:trackedPointerIDs— which pointers belong to the gesture. Always maintained (startTrackingPointerruns unconditionally) and consulted bywantsEvent.trackedPointers— per-pointer position data used to build touch events. Only maintained whileneedsPointerDatais true, i.e. while touch callbacks are attached.When touch callbacks are attached mid-gesture — e.g. a callback attached conditionally, where the attaching re-render is triggered from
onBegin—needsPointerDataflips fromfalsetotruewhile a pointer is already down. That pointer's DOWN was never recorded intotrackedPointers, but the UP still passeswantsEvent, sodispatchTouchUpEvent():allTouchesfrom an empty map — thenullpayload that used to reach JS without theallToucheskey and crash the v3 update pipeline (fixed defensively in Fix fatal crashCannot read property 'translationX' of undefinedwhen a touch event is serialized withoutallTouches#4316),changedTouchesthat the JS side has never seen go down,trackedPointersCountthat was never incremented for that pointer, serializingnumberOfTouches: -1and corrupting the counter thatmoveToStateuses to decide whether to dispatchTOUCH_CANCEL(with multi-touch this can suppress a legitimate cancel event).iOS had the same asymmetry with a different failure mode:
RNGestureHandlerPointerTrackerguards the counter (unregisterTouchreturns-1, no underflow), but still dispatched the orphaned event withid: -1inchangedTouches. Same for moves of an unregistered touch intouchesMoved.Web is not affected: its
PointerTrackeris populated unconditionally and only sending is gated onneedsPointerData, so mid-gesture flips always produce well-formed events there.Test plan
Tested on the following code: