From 42d98b58302dafb484a494ab4fc199f88f215c1d Mon Sep 17 00:00:00 2001 From: Bartlomiej Bloniarz Date: Wed, 18 Mar 2026 11:17:35 -0700 Subject: [PATCH] Fix SIGSEGV in animation backend callback by capturing weak_ptr (#56130) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/56130 The NativeAnimatedNodesManager crashes with SIGSEGV (for example in mid 2e1610331cf5e285e285a2af191fe6f3, but there are also others with similar stacks). When the manager is destroyed on the JS thread while the Choreographer thread is still firing animation frames, `pullAnimationMutations` runs on a freed object, because we captured it as a raw `this` pointer. To fix this I added a `weak_from_this` call there, to ensure the manager is still around. One thing about this issue is that it looks like it should also happen for c++ Animated without the Animation Backend, as right after this call we have: ``` if (startOnRenderCallback_) { startOnRenderCallback_([this]() { onRender(); }, isAsync); stopRenderCallbackIfNeeded(false); } } ``` which also captures `this`. On android it is not a problem though, as Android doesn't use `startOnRenderCallback_` (it only relies on `driveCxxAnimations` which is safe). On iOS it seems to be safe due to D91236980, as it adds locking a pointer to the `_nativeAnimatedNodesManagerProvider` (which owns `nativeAnimatedNodesManager`) before calling the scheduled callback. ## Changelog: [General] [Added] - Add enable_shared_from_this to `NativeAnimatedNodesManager` [General] [Changed] - Use weak_ptr when calling pullAnimationMutations in a callback scheduled with the Animation Backend Reviewed By: zeyap Differential Revision: D97101348 fbshipit-source-id: acdad4e689d72b73a945b49907350c68a67729ab --- .../renderer/animated/NativeAnimatedNodesManager.cpp | 10 +++++++--- .../renderer/animated/NativeAnimatedNodesManager.h | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/react-native/ReactCommon/react/renderer/animated/NativeAnimatedNodesManager.cpp b/packages/react-native/ReactCommon/react/renderer/animated/NativeAnimatedNodesManager.cpp index e9b3ffd7ba5..acf737d6ba9 100644 --- a/packages/react-native/ReactCommon/react/renderer/animated/NativeAnimatedNodesManager.cpp +++ b/packages/react-native/ReactCommon/react/renderer/animated/NativeAnimatedNodesManager.cpp @@ -552,9 +552,13 @@ void NativeAnimatedNodesManager::startRenderCallbackIfNeeded(bool isAsync) { if (ReactNativeFeatureFlags::useSharedAnimatedBackend()) { if (auto animationBackend = animationBackend_.lock()) { - animationBackendCallbackId_ = - animationBackend->start([this](AnimationTimestamp timestamp) { - return pullAnimationMutations(timestamp); + auto weak = weak_from_this(); + animationBackendCallbackId_ = animationBackend->start( + [weak](AnimationTimestamp timestamp) -> AnimationMutations { + if (auto self = weak.lock()) { + return self->pullAnimationMutations(timestamp); + } + return {}; }); } diff --git a/packages/react-native/ReactCommon/react/renderer/animated/NativeAnimatedNodesManager.h b/packages/react-native/ReactCommon/react/renderer/animated/NativeAnimatedNodesManager.h index b5cc98af588..c969eea23b1 100644 --- a/packages/react-native/ReactCommon/react/renderer/animated/NativeAnimatedNodesManager.h +++ b/packages/react-native/ReactCommon/react/renderer/animated/NativeAnimatedNodesManager.h @@ -51,7 +51,7 @@ using AnimationEndCallback = AsyncCallback; template <> struct Bridging : NativeAnimatedTurboModuleEndResultBridging {}; -class NativeAnimatedNodesManager { +class NativeAnimatedNodesManager : public std::enable_shared_from_this { public: using DirectManipulationCallback = std::function; using FabricCommitCallback = std::function &)>;