Skip to content

Commit 2a29751

Browse files
committed
fix
1 parent cf12741 commit 2a29751

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

async_simple/coro/FutureAwaiter.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818

1919
#ifndef ASYNC_SIMPLE_USE_MODULES
2020
#include <atomic>
21+
#include <exception>
2122
#include <memory>
2223
#include <type_traits>
23-
#include <variant>
2424
#include "async_simple/Future.h"
2525
#include "async_simple/Signal.h"
26+
#include "async_simple/Try.h"
2627
#include "async_simple/coro/Lazy.h"
2728
#include "async_simple/experimental/coroutine.h"
2829

@@ -41,6 +42,10 @@ struct FutureAwaiter {
4142
Executor* ex, Executor::Context ctx,
4243
Slot* cancellationSlot) {
4344
auto future = std::make_unique<Future<T>>(std::move(future_));
45+
future_ = makeReadyFuture<T>(
46+
std::make_exception_ptr(async_simple::SignalException(
47+
SignalType::Terminate,
48+
"FutureAwaiter is only allowed to be called by Lazy")));
4449
auto state = std::make_shared<std::atomic<bool>>(true);
4550

4651
if (!signalHelper{Terminate}.tryEmplace(
@@ -59,12 +64,12 @@ struct FutureAwaiter {
5964
}
6065
auto future_ptr = future.get();
6166
future_ptr->setContinuation(
62-
[continuation, ex, ctx, state = std::move(state),
67+
[this, continuation, ex, ctx, state = std::move(state),
6368
future = std::move(future)](auto&&...) mutable {
6469
if (!state->exchange(false)) {
6570
return;
6671
}
67-
future = std::move(future);
72+
this->future_ = std::move(*future);
6873
if (ex != nullptr) {
6974
ex->checkin(continuation, ctx);
7075
} else {
@@ -101,10 +106,6 @@ struct FutureAwaiter {
101106
return true;
102107
}
103108
auto await_resume() {
104-
if (!future_.valid()) {
105-
throw async_simple::SignalException{
106-
async_simple::Terminate, "async_simple::Future is canceled!"};
107-
}
108109
if constexpr (!std::is_same_v<T, void>) {
109110
return std::move(future_.value());
110111
}

async_simple/coro/test/FutureAwaiterTest.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,20 @@ TEST_F(FutureAwaiterTest, testWithFutureCancel) {
100100
syncAwait(collectAll<async_simple::Terminate>(
101101
lazy().via(&ex1),
102102
async_simple::coro::sleep(std::chrono::microseconds{10}).via(&ex1)));
103+
auto lazy2 = [&]() -> Lazy<> {
104+
Promise<int> pr;
105+
auto fut = pr.getFuture();
106+
sum(1, 1, [pr = std::move(pr)](int val) mutable { pr.setValue(val); });
107+
int ret = 0;
108+
try {
109+
ret = co_await std::move(fut);
110+
} catch (...) {
111+
}
112+
EXPECT_EQ(ret, 2);
113+
};
114+
syncAwait(collectAll<async_simple::Terminate>(
115+
lazy2().via(&ex1),
116+
async_simple::coro::sleep(std::chrono::seconds::max()).via(&ex1)));
103117
}
104118

105119
namespace detail {

0 commit comments

Comments
 (0)