I am spawning a coroutine as shown below.
asio::co_spawn(executor, my_coro(), asio::detached);
How am I supposed to cancel it?
As far as I know, per handler cancellation can be achieved simply by binding a handler with asio::bind_cancellation_slot. This does not work in my specific example (irrespective of using the asio::detached "handler" or some lambda), and it makes sense to me that it does not work.
I found this blog post which basically says that spawning the coroutine from another one and awaiting it (with appropriate asio::bind_cancellation_slot setup) does the trick. This fails for me as my_coro never seems to run in the example below. Besides, I don't even know why this should work at all (with respect to cancellation).
asio::awaitable<void> cancelable(asio::cancellation_signal& sig, asio::awaitable<void>&& awaitable) {
co_await asio::co_spawn(co_await asio::this_coro::executor, std::move(awaitable), asio::bind_cancellation_slot(sig.slot(), asio::use_awaitable));
co_return;
}
// ...
asio::cancellation_signal signal;
asio::co_spawn(executor, cancelable(signal, my_coro()), asio::detached);
What is the correct way to connect a asio::cancellation_signal to the asio::cancellation_state of a coroutine (obtained by co_await asio::this_coro::cancellation_state) in ASIO?
If it helps: I'm using ASIO stand-alone on the most recent master, i.e. not the boost version.