diff --git a/async_simple/coro/FutureAwaiter.h b/async_simple/coro/FutureAwaiter.h index d17dde2f..dcb8b240 100644 --- a/async_simple/coro/FutureAwaiter.h +++ b/async_simple/coro/FutureAwaiter.h @@ -44,9 +44,11 @@ struct FutureAwaiter { future_ = makeReadyFuture( std::make_exception_ptr(async_simple::SignalException( SignalType::Terminate, - "FutureAwaiter is only allowed to be called by Lazy"))); + "FutureAwaiter is cancelled by signal"))); + // Add thread fence, make sure future_ has being assigned before + // continuation or cancellation in arm. + std::atomic_thread_fence(std::memory_order_acq_rel); auto state = std::make_shared>(true); - if (!signalHelper{Terminate}.tryEmplace( cancellationSlot, [continuation, ex, ctx, state](auto&&...) mutable { @@ -116,8 +118,9 @@ auto operator co_await(Future&& future) { } template -[[deprecated("Require an rvalue future.")]] -auto operator co_await(T&& future) requires IsFuture>::value { +[[deprecated("Require an rvalue future.")]] auto operator co_await(T&& future) + requires IsFuture>::value +{ return std::move(operator co_await(std::move(future))); } diff --git a/async_simple/coro/test/FutureAwaiterTest.cpp b/async_simple/coro/test/FutureAwaiterTest.cpp index 68405b6f..5316597d 100644 --- a/async_simple/coro/test/FutureAwaiterTest.cpp +++ b/async_simple/coro/test/FutureAwaiterTest.cpp @@ -79,8 +79,6 @@ TEST_F(FutureAwaiterTest, testWithFuture) { syncAwait(lazy3().via(&ex2)); } -// The test fails in debug mode with internal CI. Disable it to keep the CI green. -#if 0 TEST_F(FutureAwaiterTest, testWithFutureCancel) { async_simple::executors::SimpleExecutor ex1(2); auto lazy = [&]() -> Lazy<> { @@ -109,7 +107,8 @@ TEST_F(FutureAwaiterTest, testWithFutureCancel) { int ret = 0; try { ret = co_await std::move(fut); - } catch (...) { + } catch (const std::exception& e) { + std::cout << e.what() << std::endl; } EXPECT_EQ(ret, 2); }; @@ -117,7 +116,6 @@ TEST_F(FutureAwaiterTest, testWithFutureCancel) { lazy2().via(&ex1), async_simple::coro::sleep(std::chrono::seconds::max()).via(&ex1))); } -#endif namespace detail {