From d154cd5ba1c0206f14654d9b5ec02f24c65d1da7 Mon Sep 17 00:00:00 2001 From: Christoph Purrer Date: Wed, 22 Jan 2025 10:41:42 -0800 Subject: [PATCH] Fix data race in TraceSection.h (#48774) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/48774 [Changelog] [Internal] - Fix data race in TraceSection.h ## Issue The `instrumentsLogHandle` variable is a static variable that is initialized lazily when the `getOrCreateInstrumentsLogHandle()` function is called. However, this initialization is not thread-safe. Multiple threads may call this function simultaneously, leading to a data race on the `instrumentsLogHandle` variable. Reviewed By: lyahdav, javache Differential Revision: D68366837 fbshipit-source-id: d61b85a0299a8d42b9fbcfdbecae78eb410d748f --- .../react-native/ReactCommon/cxxreact/TraceSection.h | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/react-native/ReactCommon/cxxreact/TraceSection.h b/packages/react-native/ReactCommon/cxxreact/TraceSection.h index e635b9645c6..2678aa1c4c9 100644 --- a/packages/react-native/ReactCommon/cxxreact/TraceSection.h +++ b/packages/react-native/ReactCommon/cxxreact/TraceSection.h @@ -117,10 +117,13 @@ static auto render(const T& t) inline os_log_t instrumentsLogHandle = nullptr; static inline os_log_t getOrCreateInstrumentsLogHandle() { - if (!instrumentsLogHandle) { - instrumentsLogHandle = os_log_create( - "dev.reactnative.instruments", OS_LOG_CATEGORY_DYNAMIC_TRACING); - } + static std::once_flag flag{}; + std::call_once(flag, []() { + if (!instrumentsLogHandle) { + instrumentsLogHandle = os_log_create( + "dev.reactnative.instruments", OS_LOG_CATEGORY_DYNAMIC_TRACING); + } + }); return instrumentsLogHandle; }