Skip to content

Apply UIManager::updateShadowTree on top of the tree it commits to - #60527

Open
leeseonghyun21 wants to merge 1 commit into
react:mainfrom
leeseonghyun21:fix/update-shadow-tree-current-root
Open

leeseonghyun21 wants to merge 1 commit into
react:mainfrom
leeseonghyun21:fix/update-shadow-tree-current-root

Conversation

@leeseonghyun21

Copy link
Copy Markdown

Summary:

UIManager::updateShadowTree clones the nodes it updates from a snapshot of the shadow tree (shadowTree.getCurrentRevision()), then commits with a transaction that ignores the root it receives and returns that clone:

shadowTree.commit(
    [rootNode = it->second](const RootShadowNode&) -> RootShadowNode::Unshared {
      return std::static_pointer_cast<RootShadowNode>(rootNode);
    },
    {});

If another thread commits between the snapshot and this commit, the commit is silently reverted. ShadowTree::commit cannot catch it: when the revision check fails, it runs the transaction again, and the transaction returns the same stale tree.

The commit that gets lost most visibly is SurfaceHandler::constraintLayout after the surface is resized (rotation, fold/unfold, split screen, window resize). The surface then keeps its previous size for good: SurfaceHandler has already stored the new constraints, so when the same size arrives again it returns early (parameters_.layoutConstraints == layoutConstraints) and never commits it. The app stays drawn at the old size, for example the unfolded layout in the top-left quarter of a folded screen, until the size changes again. Native state updates committed from the UI thread in that window are reverted in the same way.

Who calls updateShadowTree:

  • C++ Native Animated on the legacy path (NativeAnimatedNodesManagerProvider, through MergedValueDispatcher): it commits layout props, and the final props of every finished animation (forceFabricCommit). Since Promote C++ Native Animated to stable (#59936) #59936 made cxxNativeAnimatedEnabled stable, this is every app that runs a useNativeDriver animation, unless useSharedAnimatedBackend is on.
  • Libraries that commit props from C++. We hit this in production on Galaxy Z Fold devices through react-native-unistyles 3, which calls updateShadowTree in response to the same size change. In a scripted loop (screen off, fold, screen on) on a Galaxy Z Fold, the app stayed stuck at the unfolded size in 7 of 18 runs with Unistyles 3.1.1 and 6 of 144 runs with 3.5.3. We have shipped a patch with the same effect (carrying the current constraints over in this transaction) since then. Unistyles worked around it on its side in fix: keep new layout constraints when committing style updates jpudysz/react-native-unistyles#1357.

This PR moves the cloning into the commit transaction, the way AnimationBackend already commits animated props: the walk over the snapshot only finds the families of the tags, and the transaction clones those families and their ancestors with ShadowNode::cloneMultiple from the root it receives. A retried transaction now clones the newer tree, so concurrent commits are kept. Props are also cloned from the current node instead of the snapshot node.

This also drops the hand-written ancestor bookkeeping (ShadowNodeUpdateInfo, addAncestorsToUpdateList), which cloneMultiple does for us.

Changelog:

[GENERAL] [FIXED] - Fix UIManager::updateShadowTree (used by C++ Native Animated) reverting commits made concurrently, such as the new surface size after a rotation or fold

Test Plan:

New gtest UIManagerUpdateShadowTreeTest (ReactCommon/react/renderer/uimanager/tests). Two of its tests interleave a commit deterministically: a commit hook that, for the commit made by updateShadowTree, commits from another thread and waits for it. That is the state the transaction is in when a UI-thread commit lands while it runs: the revision check fails and the transaction runs again on the newer tree.

  • KeepsLayoutConstraintsCommittedConcurrently: the concurrent commit is what SurfaceHandler::constraintLayout commits on resize (500x800 → 800x500).
  • KeepsNodesCommittedConcurrently: the concurrent commit changes a sibling's props.

Built and run with the OSS ReactCommon test runner (./gradlew :private:react-native-fantom:buildReactCommonTests, then the react_renderer_uimanager_tests binary), macOS arm64:

main this PR
UpdatesProps pass pass
IgnoresTagsNotInTree pass pass
KeepsLayoutConstraintsCommittedConcurrently fail: constraints back to 500x800 pass
KeepsNodesCommittedConcurrently fail: sibling nativeID back to "initial" pass

Regression checks with this PR:

  • react_renderer_uimanager_tests (16), react_renderer_animated_tests (28), react_renderer_mounting_tests (37): all pass.
  • Fantom: yarn fantom packages/react-native/Libraries/Animated packages/react-native/src/private/animated: 19 suites, 376 tests pass (3 benchmark suites skipped).

RNTester on a Pixel 9 Pro Fold emulator (API 36, default feature flags, so the final props of every finished useNativeDriver animation are committed through updateShadowTree): a Playground screen with 200 Animated.Views whose native-driven opacity animations restart every 16–64 ms, rotated 30 times. With this PR the root view followed all 31 size changes and the animations kept running. The race did not show by hand on main either (0 of 31), so the gtests above are the reproduction.

🤖 Generated with Claude Code

@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 Dec 4, 2026
Signed-off-by: leeseonghyun21 <33210765+leeseonghyun21@users.noreply.github.com>
@leeseonghyun21
leeseonghyun21 force-pushed the fix/update-shadow-tree-current-root branch from f5c7199 to 4541548 Compare December 4, 2026 03:14

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

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