Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iOS][FIXED] Data race related to read/write on ReactMarker::logTaggedMarkerImpl #45557

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/react-native/React/CxxBridge/RCTCxxBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,13 @@ static void mapReactMarkerToPerformanceLogger(

static void registerPerformanceLoggerHooks(RCTPerformanceLogger *performanceLogger)
{
std::unique_lock lock(ReactMarker::logTaggedMarkerImplMutex);
__weak RCTPerformanceLogger *weakPerformanceLogger = performanceLogger;
ReactMarker::logTaggedMarkerImpl = [weakPerformanceLogger](
ReactMarker::LogTaggedMarker newMarker = [weakPerformanceLogger](
const ReactMarker::ReactMarkerId markerId, const char *tag) {
mapReactMarkerToPerformanceLogger(markerId, weakPerformanceLogger, tag);
};
ReactMarker::logTaggedMarkerImpl = newMarker;
}

@interface RCTCxxBridge () <RCTModuleDataCallInvokerProvider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace facebook::react {
void JReactMarker::setLogPerfMarkerIfNeeded() {
static std::once_flag flag{};
std::call_once(flag, []() {
std::unique_lock lock(ReactMarker::logTaggedMarkerImplMutex);
ReactMarker::logTaggedMarkerImpl = JReactMarker::logPerfMarker;
ReactMarker::logTaggedMarkerBridgelessImpl =
JReactMarker::logPerfMarkerBridgeless;
Expand Down
12 changes: 10 additions & 2 deletions packages/react-native/ReactCommon/cxxreact/ReactMarker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ namespace ReactMarker {
#pragma clang diagnostic ignored "-Wglobal-constructors"
#endif

LogTaggedMarker logTaggedMarkerImpl = nullptr;
LogTaggedMarker logTaggedMarkerBridgelessImpl = nullptr;
LogTaggedMarker logTaggedMarkerImpl = nullptr;
std::shared_mutex logTaggedMarkerImplMutex;

#if __clang__
#pragma clang diagnostic pop
Expand All @@ -28,7 +29,14 @@ void logMarker(const ReactMarkerId markerId) {
}

void logTaggedMarker(const ReactMarkerId markerId, const char* tag) {
logTaggedMarkerImpl(markerId, tag);
LogTaggedMarker marker = nullptr;
{
std::shared_lock lock(logTaggedMarkerImplMutex);
marker = logTaggedMarkerImpl;
}
if (marker) {
marker(markerId, tag);
}
}

void logMarkerBridgeless(const ReactMarkerId markerId) {
Expand Down
5 changes: 4 additions & 1 deletion packages/react-native/ReactCommon/cxxreact/ReactMarker.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#pragma once

#include <cmath>
#include <shared_mutex>

#ifdef __APPLE__
#include <functional>
Expand Down Expand Up @@ -51,7 +52,9 @@ typedef void (*LogTaggedMarkerBridgeless)(const ReactMarkerId, const char* tag);
#define RN_EXPORT __attribute__((visibility("default")))
#endif

extern RN_EXPORT LogTaggedMarker logTaggedMarkerImpl; // Bridge only
extern RN_EXPORT std::shared_mutex logTaggedMarkerImplMutex;
/// - important: To ensure this gets read and written to in a thread safe manner, make use of `logTaggedMarkerImplMutex`.
extern RN_EXPORT LogTaggedMarker logTaggedMarkerImpl;
extern RN_EXPORT LogTaggedMarker logTaggedMarkerBridgelessImpl;

extern RN_EXPORT void logMarker(const ReactMarkerId markerId); // Bridge only
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,11 @@ void JSIExecutor::initializeRuntime() {
if (runtimeInstaller_) {
runtimeInstaller_(*runtime_);
}

bool hasLogger(ReactMarker::logTaggedMarkerImpl);
bool hasLogger = false;
{
std::shared_lock lock(ReactMarker::logTaggedMarkerImplMutex);
hasLogger = ReactMarker::logTaggedMarkerImpl != nullptr;
}
if (hasLogger) {
ReactMarker::logMarker(ReactMarker::CREATE_REACT_CONTEXT_STOP);
}
Expand All @@ -150,8 +153,11 @@ void JSIExecutor::loadBundle(
std::unique_ptr<const JSBigString> script,
std::string sourceURL) {
SystraceSection s("JSIExecutor::loadBundle");

bool hasLogger(ReactMarker::logTaggedMarkerImpl);
bool hasLogger = false;
{
std::shared_lock lock(ReactMarker::logTaggedMarkerImplMutex);
hasLogger = ReactMarker::logTaggedMarkerImpl != nullptr;
}
std::string scriptName = simpleBasename(sourceURL);
if (hasLogger) {
ReactMarker::logTaggedMarker(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ void JSINativeModules::reset() {
std::optional<Object> JSINativeModules::createModule(
Runtime& rt,
const std::string& name) {
bool hasLogger(ReactMarker::logTaggedMarkerImpl);
bool hasLogger = false;
{
std::shared_lock lock(ReactMarker::logTaggedMarkerImplMutex);
hasLogger = ReactMarker::logTaggedMarkerImpl != nullptr;
}
if (hasLogger) {
ReactMarker::logTaggedMarker(
ReactMarker::NATIVE_MODULE_SETUP_START, name.c_str());
Expand Down
Loading