From f39b3e33719cdb7102e9ae7b67a13de4dd681949 Mon Sep 17 00:00:00 2001 From: Ali Ijaz Sheikh Date: Wed, 26 Dec 2018 16:55:36 -0800 Subject: [PATCH] src: trace_events: fix race with metadata events Multiple threads may be concurrently adding to the metadata_events list. Protect access with a mutex. Fixes: https://github.com/nodejs/node/issues/24129 PR-URL: https://github.com/nodejs/node/pull/25235 Reviewed-By: Anna Henningsen --- src/tracing/agent.cc | 12 ++++++++++-- src/tracing/agent.h | 6 +++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/tracing/agent.cc b/src/tracing/agent.cc index 103df55da5236e..3d19c6c75844fc 100644 --- a/src/tracing/agent.cc +++ b/src/tracing/agent.cc @@ -207,9 +207,17 @@ void Agent::AppendTraceEvent(TraceObject* trace_event) { id_writer.second->AppendTraceEvent(trace_event); } +void Agent::AddMetadataEvent(std::unique_ptr event) { + Mutex::ScopedLock lock(metadata_events_mutex_); + metadata_events_.push_back(std::move(event)); +} + void Agent::Flush(bool blocking) { - for (const auto& event : metadata_events_) - AppendTraceEvent(event.get()); + { + Mutex::ScopedLock lock(metadata_events_mutex_); + for (const auto& event : metadata_events_) + AppendTraceEvent(event.get()); + } for (const auto& id_writer : writers_) id_writer.second->Flush(blocking); diff --git a/src/tracing/agent.h b/src/tracing/agent.h index db5e2a6bfd049f..62073ba6b214ef 100644 --- a/src/tracing/agent.h +++ b/src/tracing/agent.h @@ -104,9 +104,7 @@ class Agent { // Writes to all writers registered through AddClient(). void AppendTraceEvent(TraceObject* trace_event); - void AddMetadataEvent(std::unique_ptr event) { - metadata_events_.push_back(std::move(event)); - } + void AddMetadataEvent(std::unique_ptr event); // Flushes all writers registered through AddClient(). void Flush(bool blocking); @@ -145,6 +143,8 @@ class Agent { ConditionVariable initialize_writer_condvar_; uv_async_t initialize_writer_async_; std::set to_be_initialized_; + + Mutex metadata_events_mutex_; std::list> metadata_events_; };