Avoid async scope lockup
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 2.4k
- Forks
- 270
- Avg merge
- 3d 6h
- Merged PRs (30d)
- 39
Description
exec::task<void> test(asio::io_context &ioContext)
{
asio::steady_timer timer(ioContext);
timer.expires_after(std::chrono::seconds(10));
auto [error] = co_await timer.async_wait(asio::as_tuple(exec::asio::use_sender));
}
exec::task<void> test2(exec::async_scope& scope, asio::io_context &ioContext)
{
#if 1
co_await exec::reschedule_coroutine_on(stdexec::inline_scheduler{});
co_await scope.nest(test(ioContext));
#elif 0
co_await stdexec::on(stdexec::inline_scheduler{}, scope.nest(test(ioContext)));
#elif 0
co_await scope.nest(stdexec::on(stdexec::inline_scheduler{}, test(ioContext)));
#endif
}
exec::task<void> test3(exec::async_scope& scope, asio::io_context &ioContext)
{
co_await test2(scope, ioContext);
}
asio::io_context ioContext;
MyThread recvThread;
std::optional<exec::async_scope> asyncScope;
asyncScope.emplace();
exec::static_thread_pool ctx{1};
asio::executor_work_guard<asio::io_context::executor_type> workGuard(
asio::make_work_guard(ioContext));
recvThread.start([&]
{
ioContext.run();
printf("test\n");
}, "client_network_thread");
exec::start_detached(stdexec::starts_on(MainThreadScheduler(), test3(*asyncScope, ioContext)));
Scheduler::getSingleton()->run(GetTicks());
sleep(5);
workGuard.reset();
asyncScope->request_stop();
recvThread.join();
asyncScope.emplace();
What I am attempting to do is to wrap an async function in an async scope t make it cancellable.
In this example the MainThreadScheduler only schedules one initial time so it is expected that it will never fully finish the test3 task.
However I would still like to successfully cancel it. In the test2 function the first case works as expected. Both case 2 and 3 assert inside the asyncScope destructor since the work isn't considered finished. It wasn't fully clear to me why the work isn't considered finished in these cases. Especially in case 2.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start from the provided async_scope, test2, and test3 reproducer and compare the three scheduling cases while tracing cancellation and scope completion. Reproduce the destructor assertion, determine why cases 2 and 3 remain unfinished after request_stop(), and verify that the intended cancellation behavior completes without the assertion.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100