Skip to content

Keep pointer capture across re-renders of the capture target - #60365

Open
ide wants to merge 1 commit into
react:mainfrom
expo:@ide/pointer-capture-newest-clone
Open

ide wants to merge 1 commit into
react:mainfrom
expo:@ide/pointer-capture-newest-clone

Conversation

@ide

@ide ide commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary:

PointerEventsProcessor holds the node passed to setPointerCapture through a weak_ptr. That node is one revision of its element. Once the element renders again and nothing else owns the old revision, the weak pointer expires and the capture ends without a lostpointercapture event. A drag handle that re-renders as the drag starts stops receiving pointermove when the pointer leaves it.

The processor now holds the capture target with a shared_ptr until the capture ends. Events already go to the target's newest clone, so they reach the element as it is now.

A target that is still owned may have left the tree. On main, retargetPointerEvent dereferences the result of getNewestCloneOfShadowNode without a null check and crashes for such a target; this can already happen when JavaScript keeps a reference to a removed node that has capture. The processor now treats a target without a newest clone as no target, and ends its capture when it handles the next event for that pointer. Until then, hasPointerCapture still reports the capture.

This follows Implicit release of pointer capture only in part. There, a capture target that is no longer connected loses its capture to the document, and the next processing of pending pointer capture fires lostpointercapture at the document. React Native has no document target, so it fires no lostpointercapture when a capture ends this way.

The cost: a capture entry keeps its node revision, and the subtree that revision owns, allocated until a pointerup or pointercancel for that pointer, or the first event for it after releasePointerCapture or after the target leaves the tree. While a capture is set, each event for its pointer also looks up the target's newest clone once more, to check that it is still in the tree. If a surface stops during a captured drag and no later event arrives for that pointer, the entry stays for the life of the runtime. previousHoverTrackersPerPointer_ already does more in that case: it holds the root node of the tree revision the pointer last hovered. Clearing a stopped surface's entries is left out: UIManager::stopSurface does not run on the JavaScript thread that owns the processor.

CaptureTargetOverrideRegistry, in a public header, now maps to std::shared_ptr; the three *FrameworksCxx.api snapshots are updated.

Changelog:

[GENERAL] [FIXED] - Keep pointer capture when the capturing element re-renders, and release it when the element is removed

Test Plan:

New cases in PointerEventsProcessorTest: captureOutlivesTheNodeItWasSetOn and captureEndsWhenTheTargetLeavesTheTree.

./gradlew :private:react-native-fantom:buildFantomTester :private:react-native-fantom:buildReactCommonTests
cmake --build private/react-native-fantom/build/tester --target react_renderer_uimanager_tests -j 8
ctest --test-dir private/react-native-fantom/build/tester/react-common-tests -R 'uimanager' --output-on-failure

With PointerEventsProcessor.cpp and .h from main, the first fails and the second crashes with a segmentation fault; on this branch the uimanager tests pass. Not run: react_renderer_uimanager_consistency_tests, which that target does not build.

@github-actions github-actions Bot added the Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. label Nov 28, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 28, 2026
chrfalch
chrfalch previously approved these changes Nov 29, 2026

@chrfalch chrfalch left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good — the fix addresses the root cause, and both new tests fail without it.

One small request on the description: when the capture target leaves the tree, no lostpointercapture event is delivered (retargeted.target == nullptr in PointerEventsProcessor.cpp). On the web the implicit release fires lostpointercapture at the document; React Native has no document target, so skipping it is reasonable. But the description currently reads as if the capture ends with the normal release semantics. Could you say explicitly that the event is not sent in that case?

@chrfalch
chrfalch dismissed their stale review November 29, 2026 09:45

Still in draft :)

@ide

ide commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor Author

The description now says that no lostpointercapture is delivered when the capture target has left the tree: the web sends it to the document, and React Native has no document target.

@ide
ide force-pushed the @ide/pointer-capture-newest-clone branch from 9dcf356 to 970b120 Compare November 29, 2026 12:01
@ide
ide marked this pull request as ready for review November 29, 2026 13:49
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Nov 29, 2026

@chrfalch chrfalch left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update, LGTM.

`PointerEventsProcessor` held the node passed to `setPointerCapture`
through a `weak_ptr`. That node is one revision of its element. Once the
element renders again and nothing else owns the old revision, the
pointer expires and the capture ends without a `lostpointercapture`
event. A drag handle that re-renders as the drag starts stops receiving
`pointermove` when the pointer leaves it.

Hold the capture target with a `shared_ptr` until the capture ends.
Events already go to the target's newest clone.

A capture ends only when an event for its pointer is processed, so the
held revision and its subtree stay allocated until then. If a surface
stops during a captured drag and no later event arrives for that
pointer, the entry stays for the life of the runtime. The hover tracker
for the same pointer already holds the root of a tree revision for as
long.

A target that is still owned may have left the tree, and
`retargetPointerEvent` dereferenced the null result of
`getNewestCloneOfShadowNode` for it. Release the capture of a target
that has left the tree at the next event for its pointer, and treat a
target without a newest clone as no target. The Pointer Events
specification releases such a capture and fires `lostpointercapture`
at the document ("Implicit release of pointer capture"). React Native
has no document target and fires no event.

Signed-off-by: James Ide <ide@expo.dev>
@ide
ide force-pushed the @ide/pointer-capture-newest-clone branch from 970b120 to bd8d4bc Compare December 5, 2026 16:46

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants