Skip to content

Commit f821cc0

Browse files
committed
Fix chriskohlhoff#1705: awaiting an experimental::promise with cancel_after doesn't post() completion on cancellation
1 parent 55684d4 commit f821cc0

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

include/asio/experimental/promise.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ struct promise<void(Ts...), Executor, Allocator>
161161
std::shared_ptr<detail::promise_impl<
162162
void(Ts...), Executor, Allocator>> self_;
163163

164+
using executor_type = Executor;
165+
executor_type get_executor() const { return self_->get_executor(); }
166+
164167
template <typename WaitHandler>
165168
void operator()(WaitHandler&& handler) const
166169
{
@@ -202,7 +205,6 @@ struct promise<void(Ts...), Executor, Allocator>
202205
if (auto p = self.lock())
203206
{
204207
p->cancel.emit(level);
205-
p->cancel_();
206208
}
207209
}
208210
};

src/tests/unit/experimental/promise.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "asio/append.hpp"
2121
#include "asio/bind_cancellation_slot.hpp"
22+
#include "asio/cancel_after.hpp"
2223
#include "asio/compose.hpp"
2324
#include "asio/deferred.hpp"
2425
#include "asio/experimental/use_promise.hpp"
@@ -214,6 +215,27 @@ void test_cancel()
214215
ec.message());
215216
}
216217

218+
void test_cancel_after()
219+
{
220+
using namespace asio;
221+
io_context ctx;
222+
steady_timer tim{ctx, std::chrono::seconds(10)};
223+
224+
auto p = tim.async_wait(experimental::use_promise);
225+
226+
bool called = false;
227+
error_code ec;
228+
std::move(p)(cancel_after(std::chrono::milliseconds(1), [&](error_code ec_) {
229+
called = true;
230+
ec = ec_;
231+
}));
232+
233+
ctx.run();
234+
235+
ASIO_CHECK(called);
236+
ASIO_CHECK(ec == error::operation_aborted);
237+
}
238+
217239
} // namespace promise
218240

219241
ASIO_TEST_SUITE
@@ -223,4 +245,5 @@ ASIO_TEST_SUITE
223245
ASIO_TEST_CASE(promise::promise_slot_tester)
224246
ASIO_TEST_CASE(promise::early_completion)
225247
ASIO_TEST_CASE(promise::test_cancel)
248+
ASIO_TEST_CASE(promise::test_cancel_after)
226249
)

0 commit comments

Comments
 (0)