I have a program based on boost::asio and beast with coroutine supports. The program is connecting to a remote server through beast websocket through which it sends requests, which is triggered by its internal logic to the server. So basically the requests are quiet heavy. I use co_spawn(strand_, sendRequest(data), asio::detached)
while sendRequest is a coroutine which sends data to the remote server like this ws_.async_write(strand_, asio::buffer(data), asio::use_awaitable)
, the strand_ at two places are the same one, and the ws_
is initialized with the strand as well:
ssl_context_(asio::ssl::context::tlsv12_client) , ws_(new websocket::stream<beast::ssl_stream<beast::tcp_stream>>(strand_, ssl_context_))
, but I got crashed. It happened at soft_mutex.hpp in the beast:
try_lock(T const*)
{
// If this assert goes off it means you are attempting to
// simultaneously initiate more than one of same asynchronous
// operation, which is not allowed. For example, you must wait
// for an async_read to complete before performing another
// async_read.
//
BOOST_ASSERT(id_ != T::id);
if(id_ != 0)
return false;
id_ = T::id;
return true;
}
if I replaced co_spawn(strand_, sendRequest(data), asio::detached)
with an empty coroutine like this: co_spawn(strand_, []() -> asio::awaitable<void> {co_return;}, asio::detached)
, it’s fine. So it definitely happens at ws_async_write
in the sendRequest
. I tried with asio::post to wrap co_spawn, it is still not working. Very annoying. Any help will be highly appreciated!!!
I do this on Ubuntu 22.04 with Boost 1.85