Repository navigation
fix(LayoutAnimations): deadlock in ReanimatedCommitHook surface initialization - #9903
Conversation
c7685da to
5d9d63f
Compare
|
It's worth noting that this PR also fixes #8344, for a reason unrelated to the ANR it's intended to address, so it would be great to merge it. I discovered this because I was using Cursor's Debug mode with Opus 5 to debug an iOS crash, and it applied these changes as a patch over 4.3.3 to fix it. In my case, the crash happened when using an Here (collapsed to avoid a wall of text) are our findings, details about our environment, and repro details
The second bug in
|
a6b8fd1 to
d175703
Compare
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: ASSERTIVE Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
📝 WalkthroughSummary by CodeRabbit
Walkthrough
ChangesFabric surface lifecycle
Estimated code review effort: 3 (Moderate) | ~20 minutes Mergeability Score: 🔵 Low · up to The change reroutes layout-animation delegate initialization for newly created surfaces; if reused surface IDs are not serialized through registration, reload churn could skip or misapply layout animations. The PR is mergeable with explicit owner awareness to verify that the tracker keeps initialization protected until registration completes. Sequence Diagram(s)sequenceDiagram
participant ReanimatedModuleProxy
participant ReanimatedCommitHook
participant ReanimatedSurfaceTracker
participant ShadowTree
participant ReanimatedMountHook
ReanimatedModuleProxy->>ReanimatedCommitHook: initializeFabric with ShadowTree
ReanimatedCommitHook->>ReanimatedSurfaceTracker: add(surfaceId)
ReanimatedCommitHook->>ShadowTree: start and configure layout animations
ReanimatedCommitHook->>ReanimatedCommitHook: handle committed ShadowTree
ReanimatedCommitHook->>ReanimatedSurfaceTracker: skip tracked surface
ReanimatedMountHook->>ReanimatedSurfaceTracker: remove(unmounted surfaceId)
Possibly related issues
Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In
`@packages/react-native-reanimated/Common/cpp/reanimated/Fabric/ReanimatedCommitHook.cpp`:
- Around line 35-48: Serialize the entire initialization sequence in
ReanimatedCommitHook::maybeInitializeLayoutAnimations with surface unmount
handling, keeping mutex_ held from recording the SurfaceId through startSurface
and setMountingOverrideDelegate. Update the corresponding shadowTreeDidUnmount
path to use the same synchronization so a reused SurfaceId cannot begin
initialization until the prior call completes.
- Around line 23-27: Reorder hook registration in the Reanimated initialization
flow so the mount hook is registered before the commit hook, ensuring
commit-created surface IDs can always be removed by unmount handling. Update the
relevant UIManager registration calls in the surrounding constructor or
initialization method, while preserving the existing shadow-tree enumeration and
maybeInitializeLayoutAnimations behavior.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: ASSERTIVE
Plan: Pro Plus
Run ID: 3c9ac5fd-c395-43a4-836f-6beebc73564b
📒 Files selected for processing (2)
packages/react-native-reanimated/Common/cpp/reanimated/Fabric/ReanimatedCommitHook.cpppackages/react-native-reanimated/Common/cpp/reanimated/Fabric/ReanimatedCommitHook.h
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In
`@packages/react-native-reanimated/Common/cpp/reanimated/Fabric/ReanimatedMountHook.cpp`:
- Around line 33-36: Update the USE_ANIMATION_BACKEND branch in the Reanimated
mount hook so it does not bypass handleNodeRemovals without addressing retained
removed nodes. Implement the required cleanup before returning, or explicitly
gate this path behind a tracked issue until cleanup is available.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: ASSERTIVE
Plan: Pro Plus
Run ID: 518e3d7f-db2f-4d40-ac61-0d4e2bf4c044
📒 Files selected for processing (6)
packages/react-native-reanimated/Common/cpp/reanimated/Fabric/ReanimatedCommitHook.cpppackages/react-native-reanimated/Common/cpp/reanimated/Fabric/ReanimatedCommitHook.hpackages/react-native-reanimated/Common/cpp/reanimated/Fabric/ReanimatedMountHook.cpppackages/react-native-reanimated/Common/cpp/reanimated/Fabric/ReanimatedMountHook.hpackages/react-native-reanimated/Common/cpp/reanimated/Fabric/ReanimatedSurfaceTracker.hpackages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.cpp
3c81105 to
976be30
Compare
…alization maybeInitializeLayoutAnimations enumerated the ShadowTreeRegistry from inside a commit hook. Commits usually execute inside ShadowTreeRegistry::visit, which already holds the registry's shared_mutex in shared mode, so the enumerate was a recursive shared acquisition. libc++ writers block new readers while draining existing ones, so a concurrent startSurface/stopSurface (a registry writer) on another thread turns that recursion into a permanent deadlock - reported as production ANRs in #8579. The registry is now read exactly once, at hook construction (module init, never a commit stack), to pick up surfaces that existed before Reanimated loaded. After that, the first commit of each new surface registers the mounting override delegate directly through the committing ShadowTree - no registry access on commit stacks at all. currentMaxSurfaceId_ becomes a seenSurfaces_ set, and a mount hook erases stopped surfaces from it so surface id reuse cannot leave a new surface unregistered. Fixes #8579
Move the seen-surfaces bookkeeping out of ReanimatedCommitHook into a small shared ReanimatedSurfaceTracker. The commit hook adds surfaces (registering the LA mounting override delegate on a surface's first commit) and ReanimatedMountHook removes them in shadowTreeDidUnmount, so a reused surface id registers again. This keeps the existing commit-hook/mount-hook separation instead of making the commit hook implement both interfaces. The mount hook is now also registered with USE_ANIMATION_BACKEND enabled, with its shadowTreeDidMount work compiled out - it only tracks surface unmounts there.
- register the mount hook before the commit hook, so a commit-tracked surface can always be forgotten on unmount - guard against a null layoutAnimationsProxy_ (the ComponentDescriptorRegistry lookup can fail, leaving the proxy unset)
976be30 to
c195500
Compare
…hread (#10265) ## Summary Extracted from #9901 per review feedback: the legacy layout-animations proxy checked `std::this_thread::get_id() != uiThreadId_` with a lazily captured thread id, and `AndroidUIScheduler` kept its own private `thread_local` flag for the same purpose. This promotes the existing tls mechanism into a shared helper: `UIScheduler::isOnUIThread()`, with the flag set in the base `UIScheduler::triggerUI()` (the single funnel all platforms' UI jobs run through). The Android wrapper drops its private flag and its now-redundant `triggerUI` override; the legacy proxy drops `uiThreadId_` and its three lazy assignments. Being a static on `UIScheduler` in worklets, the helper is available globally (e.g. for the CDP profiler). Semantics are preserved: the tls is marked at least as early as `uiThreadId_` was ever assigned, a thread-local read needs no locking, and an unmarked thread still means "not the UI thread". ## Test plan Covered by the existing layout-animation flows exercised in #9901/#9903 test plans; the check sites are unchanged apart from the helper call.
Summary
Fixes #8579.
maybeInitializeLayoutAnimationsenumerated theShadowTreeRegistryfrom inside a commit hook. Commits usually execute insideShadowTreeRegistry::visit, which already holds the registry'sshared_mutexin shared mode, so the enumerate was a recursive shared acquisition. libc++ writers block new readers while draining existing ones, so a concurrentstartSurface/stopSurface(a registry writer) on another thread turns that recursion into a permanent deadlock — the three-thread ANR in the issue (JS inenumerate, pool thread instartSurface, main inconstraintLayout).The registry is now read exactly once, at hook construction (module init — never a commit stack), to pick up surfaces that existed before Reanimated loaded (lazy init, brownfield). After that, the first commit of each new surface registers the mounting override delegate directly through the committing
ShadowTree— no registry access on commit stacks at all.currentMaxSurfaceId_becomes aseenSurfaces_set, and a mount hook erases stopped surfaces from it. That last part is not hypothetical: a JS reload reuses the same surface id, so without the erase the recreated surface would never get the override delegate.Test plan
fabric-example, iOS sim + Android emulator: