From f00550920095d90ec01372d04129de108b5f4cce Mon Sep 17 00:00:00 2001 From: Cosmin Date: Tue, 15 Sep 2026 17:24:40 +0300 Subject: [PATCH] Fix race condition during high CPU load where stopping the WebSocket could trigger an automatic reconnect => _thread.join() blocks indefinitely --- ixwebsocket/IXWebSocket.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/ixwebsocket/IXWebSocket.cpp b/ixwebsocket/IXWebSocket.cpp index f9b8075d..a92bd6d2 100644 --- a/ixwebsocket/IXWebSocket.cpp +++ b/ixwebsocket/IXWebSocket.cpp @@ -186,14 +186,23 @@ namespace ix void WebSocket::stop(uint16_t code, const std::string& reason) { + const bool isJoinable = _thread.joinable(); + + if(isJoinable) + { + // we need to set _stop before close() + // otherwise automatic reconnection might restart the connection before _stop has a chance to get set + // this can happen during high CPU load => _thread.join() below blocks indefinitely + _stop = true; + _sleepCondition.notify_one(); + } + close(code, reason); - if (_thread.joinable()) + if (isJoinable) { // wait until working thread will exit // it will exit after close operation is finished - _stop = true; - _sleepCondition.notify_one(); _thread.join(); _stop = false; }