Skip to content

Commit

Permalink
Merge "shared_memory_arbiter: Fix TSAN race on destruction" into main
Browse files Browse the repository at this point in the history
  • Loading branch information
ddiproietto authored and Gerrit Code Review committed Oct 21, 2024
2 parents 0578c3a + e49f781 commit 80acfd9
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/tracing/core/shared_memory_arbiter_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,7 @@ std::unique_ptr<TraceWriter> SharedMemoryArbiterImpl::CreateTraceWriterInternal(

void SharedMemoryArbiterImpl::ReleaseWriterID(WriterID id) {
base::TaskRunner* task_runner = nullptr;
base::WeakPtr<SharedMemoryArbiterImpl> weak_this;
{
std::lock_guard<std::mutex> scoped_lock(lock_);
active_writer_ids_.Free(id);
Expand All @@ -879,12 +880,15 @@ void SharedMemoryArbiterImpl::ReleaseWriterID(WriterID id) {
if (!task_runner_)
return;

// If `active_writer_ids_` is empty, `TryShutdown()` can return true
// and `*this` can be deleted. Let's grab everything we need from `*this`
// before releasing the lock.
weak_this = weak_ptr_factory_.GetWeakPtr();
task_runner = task_runner_;
} // scoped_lock

// We shouldn't post tasks while locked. |task_runner| remains valid after
// unlocking, because |task_runner_| is never reset.
auto weak_this = weak_ptr_factory_.GetWeakPtr();
task_runner->PostTask([weak_this, id] {
if (weak_this)
weak_this->producer_endpoint_->UnregisterTraceWriter(id);
Expand Down

0 comments on commit 80acfd9

Please sign in to comment.