chriskohlhoff / asio

Asio C++ Library
http://think-async.com/Asio
4.72k stars 1.19k forks source link

problems of co_composed on release mode #1413

Open zhllxt opened 5 months ago

zhllxt commented 5 months ago
#include <asio.hpp>
#include <asio/experimental/co_composed.hpp>

using tcp_socket = asio::as_tuple_t<asio::use_awaitable_t<
    >>::as_default_on_t<asio::ip::tcp::socket>;

std::string url_encode(std::string_view url)
{
    std::string r;
    r.reserve(url.size() * 2);
    return r;
}

struct async_request_op
{
    auto operator()(auto state) -> void
    {
        std::string_view url = "abc";
        url_encode(url);
        co_return asio::error_code{};
    }
};

template<
    typename SendToken = typename asio::default_completion_token<
    typename tcp_socket::executor_type>::type>
inline auto async_request(
    const auto& executor,
    SendToken&& token = typename asio::default_completion_token<
    typename tcp_socket::executor_type>::type())
{
    return asio::async_initiate<SendToken, void(asio::error_code)>(
        asio::experimental::co_composed<void(asio::error_code)>(
            async_request_op{ }, executor),
        token);
}

asio::awaitable<void> do_request()
{
    co_await async_request(co_await asio::this_coro::executor);
}

int main()
{
    asio::io_context ctx;
    asio::co_spawn(ctx.get_executor(), do_request(), asio::detached);
    ctx.run();
}

under release mode, it will cause crash:

002

001

This is just a simple example, and I want to demonstrate this problem through this example.

In fact, I encountered many similar problems while using co_composed. There was no problem in debug mode, but there were problems in release mode. I adjusted the compilation parameters, mainly parameters ‘inline function expansion’ and ‘enable intrinsic functions’, which sometimes solved the problem but sometimes couldn't.