Skip to content

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

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

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. retargetPointerEvent dereferences the result of getNewestCloneOfShadowNode without a null check (a TODO asks whether that is safe) and crashes for such a target. This can happen today when JavaScript keeps a reference to a removed node that has capture. The processor now releases the capture of a target that has left the tree, as Implicit release of pointer capture describes, and treats a target without a newest clone as no target.

The cost is that a capture entry keeps its node revision, and the subtree that revision owns, allocated until an event for that pointer ends the capture: releasePointerCapture, a pointerup or pointercancel, or the first event after the target leaves the tree. If a surface stops during a captured drag and the platform never delivers another event for that pointer id, the entry stays for the life of the runtime. With the weak_ptr such an entry held nothing. The processor already keeps more than this in the same case: previousHoverTrackersPerPointer_ holds the root node of the tree revision each pointer last hovered, also until the next event for that pointer. Clearing a stopped surface's entries would have to cover the hover trackers and active pointers too, and UIManager::stopSurface does not run on the JavaScript thread that owns the processor, so this PR leaves it out.

CaptureTargetOverrideRegistry is declared in a public header and now maps to std::shared_ptr. This PR updates the three *FrameworksCxx.api snapshots under scripts/cxx-api/api-snapshots to match.

Changelog:

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

Test Plan:

Two cases are added to PointerEventsProcessorTest: captureOutlivesTheNodeItWasSetOn captures to a revision that is then released, and captureEndsWhenTheTargetLeavesTheTree removes the captured node from the tree.

Run on this branch (base 94aee19ac9):

node scripts/clang-format.js --check packages/react-native/ReactCommon/react/renderer/uimanager/PointerEventsProcessor.cpp packages/react-native/ReactCommon/react/renderer/uimanager/PointerEventsProcessor.h packages/react-native/ReactCommon/react/renderer/uimanager/tests/PointerEventsProcessorTest.cpp   # passes
git diff --check upstream/main   # clean
xcrun clang++ -fsyntax-only <flags of react_renderer_uimanager> PointerEventsProcessor.cpp   # compiles with -Wall -Werror (Apple clang 21)

The C++ tests, built with the Fantom tester's CMake build on this base (2026-11-28):

./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 PointerEventsProcessor.h from main and the new tests kept: captureOutlivesTheNodeItWasSetOn fails and captureEndsWhenTheTargetLeavesTheTree crashes with a segmentation fault; the three existing PointerEventsProcessorTest cases pass.
  • On this branch: all 14 uimanager tests that were built pass, including both new cases. (react_renderer_uimanager_consistency_tests was not built by that target and reports as not run.)

🤖 Generated with Claude Code

`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, as the Pointer Events specification describes
under "Implicit release of pointer capture", and treat a target without
a newest clone as no target.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@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

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant