asio
asio copied to clipboard
Operator || on async operations swallows thrown exception.
Here is a simple awaitable coroutine for test purpose:
tcp::socket socket(io);
steady_timer timeout(io, steady_clock::now() + 30s);
tcp::endpoint tcp_endpoint(target.ip, target.port.tcp);
try
{
auto result = co_await (socket.async_connect(tcp_endpoint, use_awaitable) || timeout.async_wait(use_awaitable));
}
catch (exception& e)
{
string msg = e.what();
}
In my test scenario socket.async_connect fails and throws an exception, but it doesn't get to the catch section. It is lost in the bowels of asio. And on top of that, the combined async operation doesn't stop when async_connect fails. It waits for the timer. Is it by design or a bug? What's the best workaround? Will make_parallel_group propagate exceptions?
The document said:
When awaited using &&, the co_await expression waits until both operations have completed successfully. As a "short-circuit" evaluation, if one operation fails with an exception, the other is immediately cancelled.
But there is no description for the operator ||.
There's simply no any synchronization in asio's cancellation mechanism, so if your async ops were running on multithread executors, you have to make sure each op you pass to parallel_group has a strand executor.