Skip to content

fix: initialize the light tree from the base revision on attach - #10313

Merged
bartlomiejbloniarz merged 3 commits into
mainfrom
@bartlomiejbloniarz/initialize-light-tree-from-base-revision
Aug 18, 2026
Merged

bartlomiejbloniarz merged 3 commits into
mainfrom
@bartlomiejbloniarz/initialize-light-tree-from-base-revision

Conversation

@bartlomiejbloniarz

@bartlomiejbloniarz bartlomiejbloniarz commented Aug 17, 2026 •

Copy link
Copy Markdown
Member

Summary

Fixes #9021.

The experimental layout-animations proxy builds its light tree only from the mutations it intercepts in pullTransaction. When Reanimated initializes after a surface already has content (a lazy require deep in the app), the tree starts empty. The next batch touches tags the tree never saw and updateLightTree aborts on LightNode not found.

fig1-crash

The fix is to take the base revision (the last commit before the attach) and build the light tree from it. Then carry on from pullTransaction. Some transactions may arrive while we are reading the base revision, but they are recorded and applied before we start the normal pullTransaction path.

Important design is that we set the delegate before the base revision is read. Every transaction is then on one of two paths: its pull finished before the read (its content is inside the base revision), or the delegate received it and recorded it in pendingTransactions_ with its TransactionTelemetry::revisionNumber. Entries newer than base.number are applied on top; older ones are already in the tree and are dropped. Nothing is lost, nothing applies twice.

This is the new flow:

fig2-attach
And here is a full (AI-assisted) analysis of possible thread interleavings obraz

Here is the full AI document on the design and reasoning behind it. Attaching it mostly for future analysis if this turns out to be wrong:
light-tree-initialization.html

Test plan

Repro from #9021: an app that doesn't import reanimated lazily requires it, then resizes a pre-existing view. Crashes on main (LightNode not found), works with this PR. Verified on iOS and Android.

Also on both platforms: a stress run (commits every 16 ms while reanimated loads at a random moment, 12 relaunches each, no crashes) and the usual LA/SET examples — entering/exiting, layout transitions, shared elements - all unchanged.

Repro entry file
// The entry must NOT import react-native-reanimated.
import React, { useState } from 'react';
import { AppRegistry, Button, Text, View } from 'react-native';

function LazyLoadRepro() {
  const [loaded, setLoaded] = useState(false);
  const [grow, setGrow] = useState(0);
  const [LazyAnimated, setLazyAnimated] = useState(null);

  return (
    <View style={{ flex: 1, paddingTop: 120, paddingHorizontal: 24 }}>
      <Text>reanimated loaded: {String(loaded)} / grow: {grow}</Text>
      <View style={{ height: 60 + grow * 20, backgroundColor: 'tomato', marginVertical: 16 }} />
      <Button
        title="1. Load Reanimated"
        onPress={() => {
          // Simulates a screen deep in the app lazily importing reanimated.
          const Reanimated = require('react-native-reanimated');
          setLazyAnimated(() => Reanimated.default.View);
          setLoaded(true);
        }}
      />
      <Button title="2. Grow pre-existing view" onPress={() => setGrow((g) => g + 1)} />
      {LazyAnimated ? <LazyAnimated style={{ height: 40, backgroundColor: 'skyblue', marginTop: 16 }} /> : null}
    </View>
  );
}

AppRegistry.registerComponent('FabricExample', () => LazyLoadRepro);

Replace apps/fabric-example/index.js with this, launch, tap "1. Load
Reanimated", then "2. Grow pre-existing view" twice.

@coderabbitai

coderabbitai Bot commented Aug 17, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: f98d65e9-8b8c-4bb0-9a80-f853ea71b21f

📥 Commits

Reviewing files that changed from the base of the PR and between 1f5b378 and fa07fee.

📒 Files selected for processing (2)
  • packages/react-native-reanimated/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxy_Experimental.cpp
  • packages/react-native-reanimated/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxy_Legacy.cpp

Included review availability: Your plan includes up to 10 reviews per rolling hour; 5 remain after this review.


📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes
    • Improved layout animation initialization when surfaces are created.
    • Prevented layout transactions from being lost before a surface is fully initialized.
    • Applied pending layout updates in the correct order during initialization.
    • Improved mounting coordination for layout animations across supported implementations.
    • Increased reliability of initial layout states and transitions when rendering begins or updates arrive early.
    • Improved consistency when layout updates arrive before rendering is fully ready.

Walkthrough

The change passes ShadowTree instances into layout animation proxies. The experimental proxy buffers pre-initialization transactions and builds its light tree from the mounting coordinator base revision. The legacy proxy registers its mounting override delegate during surface startup.

Changes

Surface initialization

Layer / File(s) Summary
Surface contract and Fabric entrypoint
packages/react-native-reanimated/Common/cpp/reanimated/Fabric/ReanimatedCommitHook.cpp, packages/react-native-reanimated/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxyCommon.*, packages/react-native-reanimated/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxy_Experimental.h, packages/react-native-reanimated/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxy_Legacy.h
startSurface now accepts a ShadowTree. The Fabric commit hook passes the complete tree and no longer assigns the mounting delegate directly.
Experimental light-tree bootstrap
packages/react-native-reanimated/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxy_Experimental.cpp, packages/react-native-reanimated/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxy_Experimental.h, packages/react-native-reanimated/Common/cpp/reanimated/LayoutAnimations/SharedTransitions.cpp
The experimental proxy buffers transactions before light-tree initialization, applies the base revision and newer pending transactions, and directly applies initial mutations. The previous shared-transitions startup implementation was removed.
Legacy surface delegate registration
packages/react-native-reanimated/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxy_Legacy.cpp
The legacy proxy derives the surface ID from ShadowTree and registers its mounting override delegate.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Merge Risk: ⚪ Minimal · up to fa07f

The change initializes the light tree from the existing base revision before processing new transactions, addressing attach-time crashes without any actionable merge-blocking risk remaining after normal checks and review.

Sequence Diagram(s)

sequenceDiagram
  participant ReanimatedCommitHook
  participant LayoutAnimationsProxy_Experimental
  participant MountingCoordinator
  participant LightTree
  ReanimatedCommitHook->>LayoutAnimationsProxy_Experimental: startSurface(shadowTree)
  LayoutAnimationsProxy_Experimental->>MountingCoordinator: getBaseRevision()
  MountingCoordinator-->>LayoutAnimationsProxy_Experimental: base revision
  LayoutAnimationsProxy_Experimental->>LightTree: apply base mutations
  LayoutAnimationsProxy_Experimental->>LightTree: apply newer pending transactions
Loading

Possibly related PRs

Suggested reviewers: tomekzaw, tjzel, pawicao

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the primary fix: initializing the light tree from the base revision when attaching.
Description check ✅ Passed The description directly explains the LightNode crash, the initialization fix, transaction handling, and verification scope.
Linked Issues check ✅ Passed The changes address issue #9021 by preventing LightNode not found crashes when the proxy attaches to a populated surface.
Out of Scope Changes check ✅ Passed All changes support light-tree initialization and transaction handling for the linked crash fix; no unrelated code changes are evident.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch @bartlomiejbloniarz/initialize-light-tree-from-base-revision

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@pawicao pawicao 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.

this looks good to me, i left a nit related to similarity of applyInitialMutationsToLightTree with updateLightTree and whether we should do something about it but I'm giving an approval anyway and will let you decide whether to handle that now, or defer it

@tjzel tjzel 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.

LGTM but I don't really know the deets about how light trees work

@bartlomiejbloniarz
bartlomiejbloniarz force-pushed the @bartlomiejbloniarz/initialize-light-tree-from-base-revision branch from 1f5b378 to fa07fee Compare August 18, 2026 10:40
@bartlomiejbloniarz
bartlomiejbloniarz merged commit 1eb4f43 into main Aug 18, 2026
21 checks passed
@bartlomiejbloniarz
bartlomiejbloniarz deleted the @bartlomiejbloniarz/initialize-light-tree-from-base-revision branch August 18, 2026 13:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 Bug: Crash in LayoutAnimationsProxy_Experimental.cpp when ENABLE_SHARED_ELEMENT_TRANSITIONS is enabled

4 participants