Commit bb113630 authored by wangxianzhu's avatar wangxianzhu Committed by Commit bot

Avoid deadlock during trace_event flush

The deadlock occurs because we hold lock when posting flush tasks.
If MessageLoop::PostTask() also calls TRACE_EVENT and the TRACE_EVENT
has never been reached before, the TRACE_EVENT will try to get the
category enabled flag which will also need a lock.

Copy thread_message_loops into a temporary vector of
scoped_refptr<SingleProcessTaskRunner> to avoid lock when calling
PostTask().

BUG=397022
TEST=Existing tests and ThreadSanitizer

Review URL: https://codereview.chromium.org/491393002

Cr-Commit-Position: refs/heads/master@{#293144}
parent 4e7c1817
...@@ -1697,6 +1697,9 @@ void TraceLog::Flush(const TraceLog::OutputCallback& cb) { ...@@ -1697,6 +1697,9 @@ void TraceLog::Flush(const TraceLog::OutputCallback& cb) {
} }
int generation = this->generation(); int generation = this->generation();
// Copy of thread_message_loops_ to be used without locking.
std::vector<scoped_refptr<SingleThreadTaskRunner> >
thread_message_loop_task_runners;
{ {
AutoLock lock(lock_); AutoLock lock(lock_);
DCHECK(!flush_message_loop_proxy_.get()); DCHECK(!flush_message_loop_proxy_.get());
...@@ -1713,7 +1716,14 @@ void TraceLog::Flush(const TraceLog::OutputCallback& cb) { ...@@ -1713,7 +1716,14 @@ void TraceLog::Flush(const TraceLog::OutputCallback& cb) {
for (hash_set<MessageLoop*>::const_iterator it = for (hash_set<MessageLoop*>::const_iterator it =
thread_message_loops_.begin(); thread_message_loops_.begin();
it != thread_message_loops_.end(); ++it) { it != thread_message_loops_.end(); ++it) {
(*it)->PostTask( thread_message_loop_task_runners.push_back((*it)->task_runner());
}
}
}
if (thread_message_loop_task_runners.size()) {
for (size_t i = 0; i < thread_message_loop_task_runners.size(); ++i) {
thread_message_loop_task_runners[i]->PostTask(
FROM_HERE, FROM_HERE,
Bind(&TraceLog::FlushCurrentThread, Unretained(this), generation)); Bind(&TraceLog::FlushCurrentThread, Unretained(this), generation));
} }
...@@ -1723,7 +1733,6 @@ void TraceLog::Flush(const TraceLog::OutputCallback& cb) { ...@@ -1723,7 +1733,6 @@ void TraceLog::Flush(const TraceLog::OutputCallback& cb) {
TimeDelta::FromMilliseconds(kThreadFlushTimeoutMs)); TimeDelta::FromMilliseconds(kThreadFlushTimeoutMs));
return; return;
} }
}
FinishFlush(generation); FinishFlush(generation);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment