From 1b3f0fe2fb038e44554946feb27a43b6e86eea0a Mon Sep 17 00:00:00 2001 From: Dejan Crnila Date: Tue, 10 Nov 2026 10:56:00 +0100 Subject: [PATCH] Close a connection attempt still in flight when the client is destroyed The destructor cancelled only the connection timeout timer. The handler of a pending connect or handshake holds its own reference to the stream, and the timer that would have cancelled the attempt was the one just cancelled, so the attempt stayed pending until the peer answered or the system gave up. An owner that lets its io_context run until the pending operations have unwound then waited for it. The destructor now posts a close of the stream to the io_context; the stream member is only set between the start of a connect and the creation of the session, so an established session is not affected. --- src/client.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/client.cpp b/src/client.cpp index 5c563d3..c4b273f 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -1,5 +1,6 @@ #include #include +#include #include BEGIN_NAMESPACE_NATIVE_STREAMING @@ -33,6 +34,17 @@ Client::Client(const std::string& host, Client::~Client() { connectionTimeoutTimer.cancel(); + + // The stream is only set while a connection attempt is in flight. Its handler holds the stream too, so + // the attempt is closed here; it would otherwise stay pending until the peer or the system gives up. + if (websocketStream) + { + boost::asio::post(*ioContextPtr, + [stream = std::move(websocketStream)]() + { + stream->next_layer().close(); + }); + } NS_LOG_I("Shutting down the client"); }