asio icon indicating copy to clipboard operation
asio copied to clipboard

Connection reset issues with tcp_stream shutdown() after `async_read_some`

Open hoyhoy opened this issue 2 months ago • 1 comments

We have an async reader defined like this...

read_result = _stream->async_read_some(
    boost::asio::buffer(buf, len),
    boost::asio::use_future([&, this_connection](boost::beast::error_code ec, std::size_t n) {
        set_errno(ec.value());
        _sync_read_rc = ec;
        return ec.value() ? 0 : n;
    }));

if (read_result.wait_for(std::chrono::milliseconds(http::socket_io::s_read_timeout_ms)) ==
    std::future_status::timeout)
{
    _socketio_failed = true;
    _stream->cancel();
    custom_log("http server read timeout port %d", get_remote_port());
    return ETIMEDOUT;
}

At the end of it, we're shutting down a connection to the asio service like this...

//     std::shared_ptr<boost::beast::tcp_stream> _stream;
    _stream->socket().shutdown(
        boost::asio::ip::tcp::socket::shutdown_both, ec);
    _stream->socket().close(ec);

This seems to work on Linux, macOS, and even AIX. But on Windows, rest clients are failing with ECONNRESET.

There are a couple of odd patterns in our implementation -- like returning a future from async_read_some...

hoyhoy avatar Nov 23 '25 18:11 hoyhoy