Skip non-owned subtrees in roundLayoutResultsToPixelGrid - #58144
Open
tjzel wants to merge 1 commit into
Open
Conversation
roundLayoutResultsToPixelGrid recurses into every yoga child and writes
rounded positions and dimensions back into each node. Unlike the layout
pass, which clones children before mutating them (cloneChildrenIfNeeded),
the rounding pass crosses the ownership frontier into subtrees that are
structurally shared with other shadow trees.
Under Fabric, two trees can run layout concurrently: commits run
layoutIfNeeded on candidate trees before taking the commit mutex, and
concurrent committers (React on the JS thread and a library committing
from another thread) share every unchanged subtree. Both rounding passes
then mutate the same shared yoga nodes. ThreadSanitizer reports this as
a data race: reads at PixelGrid.cpp:72/75 against writes at
PixelGrid.cpp:89/109 via Node::setLayoutPosition.
Skipping children whose owner is not the current node is safe:
- YogaLayoutableShadowNode::layout only copies metrics from children
with hasNewLayout, and asserts those children are owned
(YGNodeGetOwner(childYogaNode) == &yogaNode_).
- hasNewLayout is only set on nodes the pass performed layout on, so
nodes past the ownership frontier are cache-restored, keep the flag
unset, and the metrics-copying recursion never descends into them.
- The shadow nodes past the frontier were not cloned in this commit,
so they are sealed and cannot accept new LayoutMetrics at all.
Rounded values written to non-owned nodes therefore have no reader; the
writes can only corrupt the state of other trees. The guard mirrors the
existing owner checks in Node::cloneChildrenIfNeeded and
YGNodeFreeRecursive ("Don't free shared nodes that we don't own").
Observed in react-native-reanimated's ThreadSanitizer nightly CI:
https://github.com/software-mansion/react-native-reanimated/actions/runs/32811464205/job/97691453444
tjzel
marked this pull request as ready for review
August 25, 2026 12:54
zeyap
self-requested a review
August 25, 2026 14:37
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.
Note
This PR description is AI-generated.
Summary:
Fabric runs layout on candidate trees before taking the commit mutex, and concurrent commits share every unchanged subtree, so Yoga's pixel-grid rounding pass could write rounded positions and dimensions into the same shared
yoga::Nodefrom two threads at once. The layout pass itself never mutates nodes it does not own (Node::cloneChildrenIfNeeded), butroundLayoutResultsToPixelGridrecursed across the ownership frontier. I made the rounding recursion skip children whose owner is not the current node, mirroring the existing owner checks inNode::cloneChildrenIfNeededandYGNodeFreeRecursive. The skipped writes had no reader:YogaLayoutableShadowNode::layoutcopies metrics only from children withhasNewLayout(asserting they are owned),hasNewLayoutis set only on nodes the pass laid out, and the shadow nodes past the frontier are sealed, so theirLayoutMetricscould not change in that commit anyway.Changelog:
[GENERAL] [FIXED] - Fix data race between concurrent Fabric commits in Yoga's pixel-grid rounding pass
Test Plan:
ThreadSanitizer reports of the race, from react-native-reanimated's sanitizer nightly (React commit on the JS thread racing a Reanimated commit on the main thread over a shared
ParagraphShadowNode): https://github.com/software-mansion/react-native-reanimated/actions/runs/32811464205/job/97691453444