mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Fix data race on PerformanceObserver entry buffer (#56352)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/56352 Add mutex to protect `PerformanceObserver::buffer_` and `didScheduleFlushBuffer_`, which can be accessed concurrently from a background thread (`handleEntry`) and JS thread (`takeRecords`). Fixes T263429319. Changelog: [Internal] Reviewed By: jorge-cab Differential Revision: D99820359 fbshipit-source-id: 068fa1871b3a0feeb84b950b0c8f5fe7dcf38b7d
This commit is contained in:
1 parent
37a18419b1
commit
5eb1ca15f3
2 files changed
+20
-6
No files matched your search
+18
-6
@@ -25,16 +25,21 @@ void PerformanceObserver::handleEntry(const PerformanceEntry& entry) {
|
||||
return;
|
||||
}
|
||||
|
||||
buffer_.push_back(entry);
|
||||
{
|
||||
std::lock_guard lock(bufferMutex_);
|
||||
buffer_.push_back(entry);
|
||||
}
|
||||
scheduleFlushBuffer();
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<PerformanceEntry> PerformanceObserver::takeRecords() {
|
||||
std::vector<PerformanceEntry> result;
|
||||
buffer_.swap(result);
|
||||
|
||||
didScheduleFlushBuffer_ = false;
|
||||
{
|
||||
std::lock_guard lock(bufferMutex_);
|
||||
buffer_.swap(result);
|
||||
didScheduleFlushBuffer_ = false;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -87,9 +92,16 @@ void PerformanceObserver::disconnect() noexcept {
|
||||
}
|
||||
|
||||
void PerformanceObserver::scheduleFlushBuffer() {
|
||||
if (!didScheduleFlushBuffer_) {
|
||||
didScheduleFlushBuffer_ = true;
|
||||
bool shouldSchedule = false;
|
||||
{
|
||||
std::lock_guard lock(bufferMutex_);
|
||||
if (!didScheduleFlushBuffer_) {
|
||||
didScheduleFlushBuffer_ = true;
|
||||
shouldSchedule = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (shouldSchedule) {
|
||||
callback_();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
|
||||
@@ -126,6 +127,7 @@ class PerformanceObserver : public std::enable_shared_from_this<PerformanceObser
|
||||
|
||||
/// https://www.w3.org/TR/event-timing/#sec-modifications-perf-timeline
|
||||
HighResDuration durationThreshold_ = DEFAULT_DURATION_THRESHOLD;
|
||||
std::mutex bufferMutex_;
|
||||
std::vector<PerformanceEntry> buffer_;
|
||||
bool didScheduleFlushBuffer_ = false;
|
||||
bool requiresDroppedEntries_ = false;
|
||||
|
||||
Reference in new issue
Block a user