Skip to content

Commit 5cd96b3

Browse files
addaleaxBethGriggs
authored andcommittedApr 16, 2019
src: avoid race condition in tracing code
`json_trace_writer_` is protected by `stream_mutex_`, but one access to it was not guarded by a lock on said mutex. Refs: #25512 PR-URL: #25624 Reviewed-By: Denys Otrishko <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Richard Lau <[email protected]>
1 parent a87c605 commit 5cd96b3

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed
 

‎src/tracing/node_trace_writer.cc

+7-2
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,13 @@ void NodeTraceWriter::FlushPrivate() {
138138

139139
void NodeTraceWriter::Flush(bool blocking) {
140140
Mutex::ScopedLock scoped_lock(request_mutex_);
141-
if (!json_trace_writer_) {
142-
return;
141+
{
142+
// We need to lock the mutexes here in a nested fashion; stream_mutex_
143+
// protects json_trace_writer_, and without request_mutex_ there might be
144+
// a time window in which the stream state changes?
145+
Mutex::ScopedLock stream_mutex_lock(stream_mutex_);
146+
if (!json_trace_writer_)
147+
return;
143148
}
144149
int request_id = ++num_write_requests_;
145150
int err = uv_async_send(&flush_signal_);

‎src/tracing/node_trace_writer.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,11 @@ class NodeTraceWriter : public AsyncTraceWriter {
4545
// Triggers callback to close async objects, ending the tracing thread.
4646
uv_async_t exit_signal_;
4747
// Prevents concurrent R/W on state related to serialized trace data
48-
// before it's written to disk, namely stream_ and total_traces_.
48+
// before it's written to disk, namely stream_ and total_traces_
49+
// as well as json_trace_writer_.
4950
Mutex stream_mutex_;
5051
// Prevents concurrent R/W on state related to write requests.
52+
// If both mutexes are locked, request_mutex_ has to be locked first.
5153
Mutex request_mutex_;
5254
// Allows blocking calls to Flush() to wait on a condition for
5355
// trace events to be written to disk.

0 commit comments

Comments
 (0)
Please sign in to comment.