From e37e69311b9debb7c50dbf6061e73102c3f10753 Mon Sep 17 00:00:00 2001 From: Alexandre Konieczny Date: Mon, 29 Apr 2019 16:18:22 +0200 Subject: [PATCH] fix data race on _thread --- ixwebsocket/IXWebSocket.cpp | 6 +++++- ixwebsocket/IXWebSocket.h | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/ixwebsocket/IXWebSocket.cpp b/ixwebsocket/IXWebSocket.cpp index 2c9d50b8..7582284d 100644 --- a/ixwebsocket/IXWebSocket.cpp +++ b/ixwebsocket/IXWebSocket.cpp @@ -38,6 +38,7 @@ namespace ix WebSocket::WebSocket() : _onMessageCallback(OnMessageCallback()), _stop(false), + _blocking(true), _automaticReconnection(true), _handshakeTimeoutSecs(kDefaultHandShakeTimeoutSecs), _enablePong(kDefaultEnablePong), @@ -135,6 +136,7 @@ namespace ix { if (_thread.joinable()) return; // we've already been started + _blocking = false; _thread = std::thread(&WebSocket::run, this); } @@ -156,6 +158,7 @@ namespace ix _stop = true; _thread.join(); _stop = false; + _blocking = true; _automaticReconnection = automaticReconnection; } @@ -317,7 +320,8 @@ namespace ix // 4. In blocking mode, getting out of this function is triggered by // an explicit disconnection from the callback, or by the remote end // closing the connection, ie isConnected() == false. - if (!_thread.joinable() && !isConnected() && !_automaticReconnection) return; + // closing the connection, ie isConnectedOrClosing() == false. + if (_blocking && !isConnectedOrClosing() && !_automaticReconnection) return; } } diff --git a/ixwebsocket/IXWebSocket.h b/ixwebsocket/IXWebSocket.h index 8c6bf702..a1fc86fa 100644 --- a/ixwebsocket/IXWebSocket.h +++ b/ixwebsocket/IXWebSocket.h @@ -154,6 +154,7 @@ namespace ix static OnTrafficTrackerCallback _onTrafficTrackerCallback; std::atomic _stop; + std::atomic _blocking; std::atomic _automaticReconnection; std::thread _thread; std::mutex _writeMutex;