From 0677a1d246540e6bdc0fe598e18451e236d4de2a Mon Sep 17 00:00:00 2001 From: Yedidya Feldblum Date: Tue, 10 May 2022 03:19:07 -0700 Subject: [PATCH] fix for TSAN-reported data-race in coro::timed_wait 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) #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 #1 std::atomic::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 --- folly/experimental/coro/TimedWait.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/folly/experimental/coro/TimedWait.h b/folly/experimental/coro/TimedWait.h index 4316b0bceaa..f3e24972119 100644 --- a/folly/experimental/coro/TimedWait.h +++ b/folly/experimental/coro/TimedWait.h @@ -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; @@ -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 {