Skip to content

Commit

Permalink
fix for TSAN-reported data-race in coro::timed_wait
Browse files Browse the repository at this point in the history
Summary:
TSAN aborts on a data-race of the form:
```
WARNING: ThreadSanitizer: data race (pid=978596)
  Write of size 8 at 0x7b0400000b40 by thread T2:
    #0 operator delete(void*, unsigned long) <null>
    #1 folly::coro::timed_wait</*...*/>(/*...*/)::'lambda'(/*...*/)::operator()</*...*/>(/*...*/) const folly/experimental/coro/TimedWait.h:51
  Previous atomic write of size 1 at 0x7b0400000b40 by thread T1:
    #0 __tsan_atomic8_exchange <null>
    #1 std::atomic<bool>::exchange(bool, std::memory_order) libgcc/include/c++/bits/atomic_base.h:499
    #2 folly::coro::timed_wait</*...*/>(/*...*/)::'lambda'(/*...*/)::operator()</*...*/>(/*...*/) const folly/experimental/coro/TimedWait.h:64
```

Reviewed By: davidtgoldblatt, iahs

Differential Revision: D36159450

fbshipit-source-id: 860fcbbc1f689f9e166934f75a2ef60abdcad421
  • Loading branch information
yfeldblum authored and facebook-github-bot committed May 10, 2022
1 parent a20d64d commit 0677a1d
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions folly/experimental/coro/TimedWait.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ timed_wait(Awaitable awaitable, Duration duration) {
std::move(sleepFuture)
.setCallback_([posted, &baton, executor = co_await co_current_executor](
auto&&, auto&&) {
if (!posted->exchange(true, std::memory_order_relaxed)) {
if (!posted->exchange(true, std::memory_order_acq_rel)) {
executor->add([&baton] { baton.post(); });
} else {
delete posted;
Expand All @@ -61,7 +61,7 @@ timed_wait(Awaitable awaitable, Duration duration) {
std::move(t)
.scheduleOn(co_await co_current_executor)
.start([posted, &baton, &result](auto&& r) {
if (!posted->exchange(true, std::memory_order_relaxed)) {
if (!posted->exchange(true, std::memory_order_acq_rel)) {
result = std::move(r);
baton.post();
} else {
Expand Down

0 comments on commit 0677a1d

Please sign in to comment.