Search code examples
c++boostboost-beastboost-beast-websocket

Do I need to call async_shutdown on beast::ssl_stream<beast::tcp_stream> when experience issues?


https://www.boost.org/doc/libs/1_72_0/libs/beast/example/http/client/async-ssl/http_client_async_ssl.cpp

std::unique_ptr<tcp::resolver> resolver_{nullptr};
std::unique_ptr<beast::ssl_stream<beast::tcp_stream>> stream_{nullptr};

void address_failure()
{
  // without calling stream_.async_shutdown
  //
  resolver_ = std::make_unique<tcp::resolver>(strand);
  stream_ = std::make_unique<websocket::stream<beast::ssl_stream<beast::tcp_stream>>>(strand, ctx);
  ...
}

void on_handshake(beast::error_code ec)
{
  if(ec)
  {
    address_failure();
    return;
  }
  ...
}

Question> When I saw connection issues, can I directly start from scratch without calling the stream_.async_shutdown?

Thank you


Solution

  • You can start from scratch, but it's good practice to try and do a graceful shutdown if possible.

    Note that, conversely, some servers might forego clean shutdown. This often leads to short reads (stream_truncated) or, in some situations, sockets in the LINGERING state. It's something that some servers do get away with, but if you do it, you might cause resource exhaustion on the other end.