From ef57e3a2b14c17b1a05aed0079f55fac2ece4996 Mon Sep 17 00:00:00 2001 From: David Capello Date: Fri, 1 Sep 2023 12:11:36 -0300 Subject: [PATCH] Fix hanging of WebSocket::stop() waiting for its thread to join (#481) This might happen in some special cases where WebSocketTransport::sendOnSocket() fails, closes the socket, and set the ready state to CLOSED, but WebSocket::run() stills wait the interrupt event to happen. Possibly related to #474 --- ixwebsocket/IXWebSocketTransport.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/ixwebsocket/IXWebSocketTransport.cpp b/ixwebsocket/IXWebSocketTransport.cpp index ec25465e..f9d36c52 100644 --- a/ixwebsocket/IXWebSocketTransport.cpp +++ b/ixwebsocket/IXWebSocketTransport.cpp @@ -1174,7 +1174,22 @@ namespace ix { _requestInitCancellation = true; - if (_readyState == ReadyState::CLOSING || _readyState == ReadyState::CLOSED) return; + if (_readyState == ReadyState::CLOSING || _readyState == ReadyState::CLOSED) + { + // Wake up the socket polling thread, as + // Socket::isReadyToRead() might be still waiting the + // interrupt event to happen. + bool wakeUpPoll = false; + { + std::lock_guard lock(_socketMutex); + wakeUpPoll = (_socket && _socket->isWakeUpFromPollSupported()); + } + if (wakeUpPoll) + { + wakeUpFromPoll(SelectInterrupt::kCloseRequest); + } + return; + } if (closeWireSize == 0) {