Skip to content

Commit

Permalink
Fix data race in TraceSection.h (facebook#48774)
Browse files Browse the repository at this point in the history
Summary:

[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.

Differential Revision: D68366837
  • Loading branch information
christophpurrer authored and facebook-github-bot committed Jan 18, 2025
1 parent 22e7691 commit c8ba150
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions packages/react-native/ReactCommon/cxxreact/TraceSection.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit c8ba150

Please sign in to comment.