Skip to content

Commit

Permalink
fix TSAN failure
Browse files Browse the repository at this point in the history
  • Loading branch information
markdroth committed Aug 17, 2023
1 parent e0f86c7 commit 62998f5
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/core/lib/gprpp/work_serializer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,16 @@ void WorkSerializer::WorkSerializerImpl::DrainQueueOwned() {
}
if (GetSize(prev_ref_pair) == 2) {
// Queue drained. Give up ownership but only if queue remains empty.
#ifndef NDEBUG
// Reset current_thread_ before giving up ownership to avoid TSAN
// race. If we don't wind up giving up ownership, we'll set this
// again below before we pull the next callback out of the queue.
current_thread_ = std::thread::id();
#endif
uint64_t expected = MakeRefPair(1, 1);
if (refs_.compare_exchange_strong(expected, MakeRefPair(0, 1),
std::memory_order_acq_rel)) {
// Queue is drained.
#ifndef NDEBUG
current_thread_ = std::thread::id();
#endif
return;
}
if (GetSize(expected) == 0) {
Expand All @@ -220,6 +223,10 @@ void WorkSerializer::WorkSerializerImpl::DrainQueueOwned() {
delete this;
return;
}
#ifndef NDEBUG
// Didn't wind up giving up ownership, so set current_thread_ again.
current_thread_ = std::this_thread::get_id();
#endif
}
// There is at least one callback on the queue. Pop the callback from the
// queue and execute it.
Expand Down

0 comments on commit 62998f5

Please sign in to comment.