mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
move fabric sync in C++ Animated to JS thread (#52904)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/52904 changelog: [internal] There is a race condition in `FabricUIManagerBinding::schedulerDidFinishTransaction`. Until it is addressed properly, let's do synchronisation Fabric commits through JavaScript thread. It can have pretty serious consequences: a crash in better case and undefined behaviour in worse case. The race is between acquiring two mutexes: 1. One inside of `MountingCoordinator::pullTransaction` 2. Second one inside of `FabricMountingManager::executeMount` The logic inside of `FabricUIManagerBinding::schedulerDidFinishTransaction` depends on the fact that whichever thread acquires mutex number 1, will acquire mutex number 2 without interruption. But that is not always the case as threads may be interrupted. Reviewed By: zeyap Differential Revision: D79080739 fbshipit-source-id: c86885aba25825030dc44b60144beb3e3ba18306
This commit is contained in:
1 parent
170ed501f0
commit
4614a0bc17
6 files changed
+139
-22
No files matched your search
@@ -77,15 +77,14 @@ test('moving box by 100 points', () => {
|
||||
|
||||
// Animation is completed now. C++ Animated will commit the final position to the shadow tree.
|
||||
if (ReactNativeFeatureFlags.cxxNativeAnimatedRemoveJsSync()) {
|
||||
expect(viewElement.getBoundingClientRect().x).toBe(100);
|
||||
// TODO(T223344928): this shouldn't be neccessary
|
||||
// TODO(T223344928,T232605345): this shouldn't be neccessary once we disable JS sync and Android's race condition.
|
||||
Fantom.runWorkLoop();
|
||||
expect(viewElement.getBoundingClientRect().x).toBe(100);
|
||||
} else {
|
||||
expect(viewElement.getBoundingClientRect().x).toBe(0);
|
||||
Fantom.runWorkLoop(); // Animated still schedules a React state update for synchronisation to shadow tree
|
||||
expect(viewElement.getBoundingClientRect().x).toBe(100);
|
||||
}
|
||||
|
||||
expect(viewElement.getBoundingClientRect().x).toBe(100);
|
||||
});
|
||||
|
||||
test('animation driven by onScroll event', () => {
|
||||
@@ -148,6 +147,10 @@ test('animation driven by onScroll event', () => {
|
||||
|
||||
expect(transform.translateY).toBeCloseTo(100, 0.001);
|
||||
|
||||
// TODO(T232605345): The following two lines won't be necessary once race condition on Android is fixed
|
||||
expect(viewElement.getBoundingClientRect().y).toBe(0);
|
||||
Fantom.runWorkLoop();
|
||||
|
||||
expect(viewElement.getBoundingClientRect().y).toBe(100);
|
||||
});
|
||||
|
||||
@@ -333,6 +336,8 @@ test('moving box by 50 points with offset 10', () => {
|
||||
).toBeCloseTo(60, 0.001);
|
||||
|
||||
if (ReactNativeFeatureFlags.cxxNativeAnimatedRemoveJsSync()) {
|
||||
// TODO(T232605345): The following line won't be necessary once race condition on Android is fixed.
|
||||
Fantom.runWorkLoop();
|
||||
expect(root.getRenderedOutput({props: ['transform']}).toJSX()).toEqual(
|
||||
<rn-view transform='[{"translateX": 60.000000}]' />,
|
||||
);
|
||||
@@ -730,27 +735,20 @@ test('Animated.sequence', () => {
|
||||
Fantom.unstable_getDirectManipulationProps(element).transform[0].translateY,
|
||||
).toBeCloseTo(-16, 0.001);
|
||||
|
||||
if (!ReactNativeFeatureFlags.cxxNativeAnimatedRemoveJsSync()) {
|
||||
Fantom.runWorkLoop(); // React update to sync end state of 1st timing animation in sequence
|
||||
}
|
||||
expect(_isSequenceFinished).toBe(false);
|
||||
expect(element.getBoundingClientRect().y).toBe(-16);
|
||||
Fantom.runWorkLoop(); // React update to sync end state of 1st timing animation in sequence
|
||||
|
||||
Fantom.runWorkLoop(); // React render
|
||||
expect(_isSequenceFinished).toBe(false);
|
||||
|
||||
expect(element.getBoundingClientRect().y).toBe(-16);
|
||||
|
||||
expect(
|
||||
// $FlowFixMe[incompatible-use]
|
||||
Fantom.unstable_getDirectManipulationProps(element).transform[0].translateY,
|
||||
).toBeCloseTo(0, 0.001);
|
||||
|
||||
if (!ReactNativeFeatureFlags.cxxNativeAnimatedRemoveJsSync()) {
|
||||
Fantom.runWorkLoop(); // React update to sync end state of 2nd timing animation in sequence
|
||||
}
|
||||
Fantom.runWorkLoop(); // React update to sync end state of 2nd timing animation in sequence
|
||||
|
||||
expect(element.getBoundingClientRect().y).toBe(0);
|
||||
|
||||
if (ReactNativeFeatureFlags.cxxNativeAnimatedRemoveJsSync()) {
|
||||
expect(_isSequenceFinished).toBe(false);
|
||||
Fantom.runWorkLoop();
|
||||
}
|
||||
expect(_isSequenceFinished).toBe(true);
|
||||
});
|
||||
@@ -284,7 +284,7 @@ void AnimatedModule::executeOperation(const Operation& operation) {
|
||||
|
||||
void AnimatedModule::installJSIBindingsWithRuntime(jsi::Runtime& runtime) {
|
||||
if (nodesManagerProvider_) {
|
||||
nodesManager_ = nodesManagerProvider_->getOrCreate(runtime);
|
||||
nodesManager_ = nodesManagerProvider_->getOrCreate(runtime, jsInvoker_);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
#include "MergedValueDispatcher.h"
|
||||
#include <react/renderer/core/DynamicPropsUtilities.h>
|
||||
|
||||
namespace facebook::react {
|
||||
|
||||
MergedValueDispatcher::MergedValueDispatcher(
|
||||
DispatchFunction dispatchFunction,
|
||||
MergedValueFunction mergedValueFunction)
|
||||
: dispatchFunction_(std::move(dispatchFunction)),
|
||||
mergedValueFunction_(std::move(mergedValueFunction)) {}
|
||||
|
||||
void MergedValueDispatcher::dispatch(
|
||||
const std::unordered_map<Tag, folly::dynamic>& value) {
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
for (auto& [viewTag, props] : value) {
|
||||
accumulatedValues_[viewTag] = mergeDynamicProps(
|
||||
accumulatedValues_[viewTag], props, NullValueStrategy::Override);
|
||||
}
|
||||
|
||||
if (hasPendingDispatch_) {
|
||||
return;
|
||||
}
|
||||
|
||||
hasPendingDispatch_ = true;
|
||||
}
|
||||
|
||||
dispatchFunction_([this]() {
|
||||
auto accumulatedValuesCopy = std::unordered_map<Tag, folly::dynamic>{};
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
std::swap(accumulatedValues_, accumulatedValuesCopy);
|
||||
hasPendingDispatch_ = false;
|
||||
}
|
||||
mergedValueFunction_(std::move(accumulatedValuesCopy));
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace facebook::react
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <folly/dynamic.h>
|
||||
#include <react/renderer/core/ReactPrimitives.h>
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
|
||||
namespace facebook::react {
|
||||
|
||||
/**
|
||||
* A thread-safe dispatcher that ensures only the latest value is dispatched
|
||||
* to avoid overloading the target thread. Multiple rapid calls are coalesced
|
||||
* into a single dispatch operation merging the values.
|
||||
*/
|
||||
class MergedValueDispatcher {
|
||||
public:
|
||||
using DispatchFunction = std::function<void(std::function<void()>&&)>;
|
||||
using MergedValueFunction =
|
||||
std::function<void(std::unordered_map<Tag, folly::dynamic> tagToProps)>;
|
||||
|
||||
/**
|
||||
* Creates a MergedValueDispatcher with the given dispatch function.
|
||||
*
|
||||
* @param dispatchFunction - function that dispatches to the target
|
||||
* thread.
|
||||
* @param latestValueFunction - function that will be called on the target
|
||||
* thread with all values merged.
|
||||
*/
|
||||
explicit MergedValueDispatcher(
|
||||
DispatchFunction dispatchFunction,
|
||||
MergedValueFunction mergedValueFunction);
|
||||
|
||||
/**
|
||||
* Dispatches the given value. If a dispatch is already pending, this will
|
||||
* merge with the pending value instead of creating a new dispatch.
|
||||
*
|
||||
* @param value - value to be dispatched.
|
||||
*/
|
||||
void dispatch(const std::unordered_map<Tag, folly::dynamic>& value);
|
||||
|
||||
private:
|
||||
DispatchFunction dispatchFunction_;
|
||||
MergedValueFunction mergedValueFunction_;
|
||||
std::mutex mutex_;
|
||||
bool hasPendingDispatch_{false};
|
||||
std::unordered_map<Tag, folly::dynamic> accumulatedValues_;
|
||||
};
|
||||
|
||||
} // namespace facebook::react
|
||||
+16
-3
@@ -10,6 +10,7 @@
|
||||
#include <glog/logging.h>
|
||||
#include <react/featureflags/ReactNativeFeatureFlags.h>
|
||||
#include <react/renderer/animated/AnimatedMountingOverrideDelegate.h>
|
||||
#include <react/renderer/animated/MergedValueDispatcher.h>
|
||||
#include <react/renderer/uimanager/UIManagerBinding.h>
|
||||
|
||||
namespace facebook::react {
|
||||
@@ -34,16 +35,28 @@ NativeAnimatedNodesManagerProvider::NativeAnimatedNodesManagerProvider(
|
||||
stopOnRenderCallback_(std::move(stopOnRenderCallback)) {}
|
||||
|
||||
std::shared_ptr<NativeAnimatedNodesManager>
|
||||
NativeAnimatedNodesManagerProvider::getOrCreate(jsi::Runtime& runtime) {
|
||||
NativeAnimatedNodesManagerProvider::getOrCreate(
|
||||
jsi::Runtime& runtime,
|
||||
std::shared_ptr<CallInvoker> jsInvoker) {
|
||||
if (nativeAnimatedNodesManager_ == nullptr) {
|
||||
auto* uiManager = &UIManagerBinding::getBinding(runtime)->getUIManager();
|
||||
|
||||
NativeAnimatedNodesManager::FabricCommitCallback fabricCommitCallback =
|
||||
nullptr;
|
||||
|
||||
if (!ReactNativeFeatureFlags::disableFabricCommitInCXXAnimated()) {
|
||||
fabricCommitCallback =
|
||||
[uiManager](std::unordered_map<Tag, folly::dynamic>& tagToProps) {
|
||||
mergedValueDispatcher_ = std::make_unique<MergedValueDispatcher>(
|
||||
[jsInvoker](std::function<void()>&& func) {
|
||||
jsInvoker->invokeAsync(std::move(func));
|
||||
},
|
||||
[uiManager](
|
||||
const std::unordered_map<Tag, folly::dynamic>& tagToProps) {
|
||||
uiManager->updateShadowTree(tagToProps);
|
||||
});
|
||||
|
||||
fabricCommitCallback =
|
||||
[this](std::unordered_map<Tag, folly::dynamic>& tagToProps) {
|
||||
mergedValueDispatcher_->dispatch(tagToProps);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
+5
-1
@@ -7,6 +7,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <react/renderer/animated/MergedValueDispatcher.h>
|
||||
#include <react/renderer/uimanager/UIManagerNativeAnimatedDelegate.h>
|
||||
#include "NativeAnimatedNodesManager.h"
|
||||
|
||||
@@ -35,7 +36,8 @@ class NativeAnimatedNodesManagerProvider {
|
||||
nullptr);
|
||||
|
||||
std::shared_ptr<NativeAnimatedNodesManager> getOrCreate(
|
||||
jsi::Runtime& runtime);
|
||||
jsi::Runtime& runtime,
|
||||
std::shared_ptr<CallInvoker> jsInvoker);
|
||||
|
||||
// Native Event Listeners
|
||||
void addEventEmitterListener(
|
||||
@@ -55,6 +57,8 @@ class NativeAnimatedNodesManagerProvider {
|
||||
|
||||
NativeAnimatedNodesManager::StartOnRenderCallback startOnRenderCallback_;
|
||||
NativeAnimatedNodesManager::StopOnRenderCallback stopOnRenderCallback_;
|
||||
|
||||
std::unique_ptr<MergedValueDispatcher> mergedValueDispatcher_;
|
||||
};
|
||||
|
||||
} // namespace facebook::react
|
||||
Reference in new issue
Block a user