From 11de2a3f658d5318e55b821b93ecc7bf874f58bd Mon Sep 17 00:00:00 2001
From: Lewis Baker <lbaker@fb.com>
Date: Wed, 28 Oct 2020 11:00:05 -0700
Subject: [PATCH] Remove workaround for TSAN symmetric-transfer bug

Summary:
Removes the symmetricTransferMaybeReschedule() helper functions as
the workarounds are no longer required now that clang fixes have been
merged.

Reviewed By: andriigrynenko

Differential Revision: D24554145

fbshipit-source-id: ce3a6b6a8ead63b98e605c21d6555950b3b55c9a
---
 folly/experimental/coro/AsyncGenerator.h | 12 +++----
 folly/experimental/coro/Task.h           | 11 +++----
 folly/experimental/coro/detail/Helpers.h | 42 ------------------------
 3 files changed, 9 insertions(+), 56 deletions(-)

diff --git a/folly/experimental/coro/AsyncGenerator.h b/folly/experimental/coro/AsyncGenerator.h
index 77b789296..fdca92b49 100644
--- a/folly/experimental/coro/AsyncGenerator.h
+++ b/folly/experimental/coro/AsyncGenerator.h
@@ -26,7 +26,6 @@
 #include <folly/experimental/coro/Utils.h>
 #include <folly/experimental/coro/ViaIfAsync.h>
 #include <folly/experimental/coro/WithCancellation.h>
-#include <folly/experimental/coro/detail/Helpers.h>
 #include <folly/experimental/coro/detail/Malloc.h>
 #include <folly/experimental/coro/detail/ManualLifetime.h>
 
@@ -51,8 +50,9 @@ class AsyncGeneratorPromise {
     bool await_ready() noexcept { return false; }
     auto await_suspend(
         std::experimental::coroutine_handle<AsyncGeneratorPromise> h) noexcept {
-      return symmetricTransferMaybeReschedule(
-          h.promise().continuation_, h.promise().clearContext());
+      auto& promise = h.promise();
+      promise.clearContext();
+      return promise.continuation_;
     }
     void await_resume() noexcept {}
   };
@@ -211,12 +211,10 @@ class AsyncGeneratorPromise {
   bool hasValue() const noexcept { return state_ == State::VALUE; }
 
  private:
-  folly::Executor::KeepAlive<> clearContext() noexcept {
-    auto executor = std::exchange(executor_, {});
+  void clearContext() noexcept {
+    executor_ = {};
     cancelToken_ = {};
     hasCancelTokenOverride_ = false;
-
-    return executor;
   }
 
   enum class State : std::uint8_t {
diff --git a/folly/experimental/coro/Task.h b/folly/experimental/coro/Task.h
index 8e42cbd72..a94b206d2 100644
--- a/folly/experimental/coro/Task.h
+++ b/folly/experimental/coro/Task.h
@@ -35,7 +35,6 @@
 #include <folly/experimental/coro/Utils.h>
 #include <folly/experimental/coro/ViaIfAsync.h>
 #include <folly/experimental/coro/WithCancellation.h>
-#include <folly/experimental/coro/detail/Helpers.h>
 #include <folly/experimental/coro/detail/InlineTask.h>
 #include <folly/experimental/coro/detail/Malloc.h>
 #include <folly/experimental/coro/detail/Traits.h>
@@ -78,11 +77,9 @@ class TaskPromiseBase {
     bool await_ready() noexcept { return false; }
 
     template <typename Promise>
-    auto await_suspend(
+    std::experimental::coroutine_handle<> await_suspend(
         std::experimental::coroutine_handle<Promise> coro) noexcept {
-      TaskPromiseBase& promise = coro.promise();
-      return symmetricTransferMaybeReschedule(
-          promise.continuation_, promise.executor_);
+      return coro.promise().continuation_;
     }
 
     [[noreturn]] void await_resume() noexcept { folly::assume_unreachable(); }
@@ -563,10 +560,10 @@ class FOLLY_NODISCARD Task {
 
     bool await_ready() noexcept { return false; }
 
-    auto await_suspend(
+    handle_t await_suspend(
         std::experimental::coroutine_handle<> continuation) noexcept {
       coro_.promise().continuation_ = continuation;
-      return symmetricTransferMaybeReschedule(coro_, coro_.promise().executor_);
+      return coro_;
     }
 
     T await_resume() { return await_resume_try().value(); }
diff --git a/folly/experimental/coro/detail/Helpers.h b/folly/experimental/coro/detail/Helpers.h
index dbf5fde26..6e6456f28 100644
--- a/folly/experimental/coro/detail/Helpers.h
+++ b/folly/experimental/coro/detail/Helpers.h
@@ -42,48 +42,6 @@ class UnsafeResumeInlineSemiAwaitable {
   Awaitable awaitable_;
 };
 
-class NestingCounter {
- public:
-  struct GuardDestructor {
-    void operator()(NestingCounter* counter) { --counter->counter_; }
-  };
-  using Guard = std::unique_ptr<NestingCounter, GuardDestructor>;
-  Guard guard() {
-    const size_t maxNestingDepth = 128;
-    if (counter_ + 1 == maxNestingDepth) {
-      return nullptr;
-    }
-    ++counter_;
-    return Guard{this};
-  }
-
- private:
-  size_t counter_{0};
-};
-using NestingCounterSingleton = SingletonThreadLocal<NestingCounter>;
-
-template <typename Promise>
-FOLLY_ALWAYS_INLINE folly::conditional_t<
-    kIsSanitizeThread,
-    void,
-    std::experimental::coroutine_handle<Promise>>
-symmetricTransferMaybeReschedule(
-    std::experimental::coroutine_handle<Promise> ch,
-    const Executor::KeepAlive<>& ex) {
-  if constexpr (kIsSanitizeThread) {
-    if (auto nestingGuard = NestingCounterSingleton::get().guard()) {
-      ch.resume();
-    } else {
-      copy(ex).add([ch, rctx = RequestContext::saveContext()](
-                       Executor::KeepAlive<>&&) mutable {
-        RequestContextScopeGuard guard(std::move(rctx));
-        ch.resume();
-      });
-    }
-  } else {
-    return ch;
-  }
-}
 } // namespace detail
 } // namespace coro
 } // namespace folly
-- 
2.26.2