Skip to content

Commit

Permalink
src: trace_events: fix race with metadata events
Browse files Browse the repository at this point in the history
Multiple threads may be concurrently adding to the metadata_events list.
Protect access with a mutex.

Fixes: nodejs#24129

PR-URL: nodejs#25235
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
ofrobots authored and addaleax committed Jan 7, 2019
1 parent 86ae2c1 commit f39b3e3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
12 changes: 10 additions & 2 deletions src/tracing/agent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,17 @@ void Agent::AppendTraceEvent(TraceObject* trace_event) {
id_writer.second->AppendTraceEvent(trace_event);
}

void Agent::AddMetadataEvent(std::unique_ptr<TraceObject> 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);
Expand Down
6 changes: 3 additions & 3 deletions src/tracing/agent.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,7 @@ class Agent {
// Writes to all writers registered through AddClient().
void AppendTraceEvent(TraceObject* trace_event);

void AddMetadataEvent(std::unique_ptr<TraceObject> event) {
metadata_events_.push_back(std::move(event));
}
void AddMetadataEvent(std::unique_ptr<TraceObject> event);
// Flushes all writers registered through AddClient().
void Flush(bool blocking);

Expand Down Expand Up @@ -145,6 +143,8 @@ class Agent {
ConditionVariable initialize_writer_condvar_;
uv_async_t initialize_writer_async_;
std::set<AsyncTraceWriter*> to_be_initialized_;

Mutex metadata_events_mutex_;
std::list<std::unique_ptr<TraceObject>> metadata_events_;
};

Expand Down

0 comments on commit f39b3e3

Please sign in to comment.