From 6a7e9d28a938cb19f90af2230048ff46bbde16f1 Mon Sep 17 00:00:00 2001 From: GitHub Workflows Support Date: Mon, 30 Nov 2026 09:02:35 -0800 Subject: [PATCH] Make Node::owner_ a relaxed atomic pointer (#2094) Summary: * Make Node::owner_ a relaxed atomic pointer A Yoga node shared between two trees can have its owner read by a layout pass on one thread while a clone of its previous owner on another thread rewrites it. owner_ was a plain pointer, so that is a data race. Loads and stores of the owner now use std::atomic with relaxed ordering, through a small copyable wrapper so Node keeps its defaulted copy operations. * Keep RelaxedAtomicPointer private to Node and test owner access Moves the RelaxedAtomicPointer helper into Node's private section so it is not part of the facebook::yoga namespace, and adds tests for reading a node's owner while another thread sets it, and for the owner after copying, moving, cloning the parent and resetting. * Free the shared child in cloned_parent_keeps_shared_child_owner YGNodeFree(clone) clears the shared child's owner, so YGNodeFreeRecursive(parent) skips it and the child leaks. Co-Authored-By: Claude Opus 5.5 --------- Reviewed By: javache Differential Revision: D135070417 --- .../ReactCommon/yoga/yoga/node/Node.cpp | 14 ++++--- .../ReactCommon/yoga/yoga/node/Node.h | 37 +++++++++++++++++-- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/packages/react-native/ReactCommon/yoga/yoga/node/Node.cpp b/packages/react-native/ReactCommon/yoga/yoga/node/Node.cpp index 885e73b9f884..95b27ccfde9a 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/node/Node.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/node/Node.cpp @@ -318,7 +318,7 @@ void Node::setPosition( /* Root nodes should always be laid out as LTR, so we don't return negative * values. */ const Direction directionRespectingRoot = - owner_ != nullptr ? direction : Direction::LTR; + getOwner() != nullptr ? direction : Direction::LTR; const FlexDirection mainAxis = yoga::resolveDirection(style_.flexDirection(), directionRespectingRoot); const FlexDirection crossAxis = @@ -458,15 +458,15 @@ void Node::markDirtyAndPropagate() { if (!isDirty_) { setDirty(true); setLayoutComputedFlexBasis(FloatOptional()); - if (owner_ != nullptr) { - owner_->markDirtyAndPropagate(); + if (Node* owner = getOwner()) { + owner->markDirtyAndPropagate(); } } } float Node::resolveFlexGrow() const { // Root nodes flexGrow should always be 0 - if (owner_ == nullptr) { + if (getOwner() == nullptr) { return 0.0; } if (style_.flexGrow().isDefined()) { @@ -479,7 +479,7 @@ float Node::resolveFlexGrow() const { } float Node::resolveFlexShrink() const { - if (owner_ == nullptr) { + if (getOwner() == nullptr) { return 0.0; } if (style_.flexShrink().isDefined()) { @@ -505,7 +505,9 @@ void Node::reset() { children_.empty(), "Cannot reset a node which still has children attached"); yoga::assertFatalWithNode( - this, owner_ == nullptr, "Cannot reset a node still attached to a owner"); + this, + getOwner() == nullptr, + "Cannot reset a node still attached to a owner"); *this = Node{getConfig()}; } diff --git a/packages/react-native/ReactCommon/yoga/yoga/node/Node.h b/packages/react-native/ReactCommon/yoga/yoga/node/Node.h index 1c6eedf85267..3f1dd9307af6 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/node/Node.h +++ b/packages/react-native/ReactCommon/yoga/yoga/node/Node.h @@ -7,6 +7,7 @@ #pragma once +#include #include #include #include @@ -153,7 +154,7 @@ class YG_EXPORT Node : public ::YGNode { // of the Node when a Node only belongs to one YogaTree or nullptr when // the Node is shared between two or more YogaTrees. Node* getOwner() const { - return owner_; + return owner_.load(); } const std::vector& getChildren() const { @@ -275,7 +276,7 @@ class YG_EXPORT Node : public ::YGNode { } void setOwner(Node* owner) { - owner_ = owner; + owner_.store(owner); } // TODO: rvalue override for setChildren @@ -331,6 +332,36 @@ class YG_EXPORT Node : public ::YGNode { float axisSize) const; private: + /** + * A pointer with relaxed atomic loads and stores. A node shared between trees + * can have its owner read by a layout pass on one thread while a clone of its + * previous owner, on another thread, writes it. Copies read the pointer once, + * so Node keeps its defaulted copy operations. + */ + template + class RelaxedAtomicPointer { + public: + RelaxedAtomicPointer() = default; + RelaxedAtomicPointer(const RelaxedAtomicPointer& other) noexcept + : value_(other.load()) {} + RelaxedAtomicPointer& operator=( + const RelaxedAtomicPointer& other) noexcept { + store(other.load()); + return *this; + } + + T* load() const noexcept { + return value_.load(std::memory_order_relaxed); + } + + void store(T* value) noexcept { + value_.store(value, std::memory_order_relaxed); + } + + private: + std::atomic value_{nullptr}; + }; + // Used to allow resetting the node Node& operator=(Node&&) noexcept = default; @@ -355,7 +386,7 @@ class YG_EXPORT Node : public ::YGNode { LayoutResults layout_; size_t lineIndex_ = 0; size_t contentsChildrenCount_ = 0; - Node* owner_ = nullptr; + RelaxedAtomicPointer owner_; std::vector children_; const Config* config_; std::array processedDimensions_{