Skip to content

Commit ba0b2dc

Browse files
committed
fixup! IXWebSocketTransport::setReadyState(): Run under lock
1 parent 81682fe commit ba0b2dc

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

ixwebsocket/IXWebSocketTransport.cpp

+15-11
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,13 @@ namespace ix
200200

201201
WebSocketTransport::ReadyState WebSocketTransport::getReadyState() const
202202
{
203-
std::lock_guard<std::mutex> lock(_setReadyStateMutex);
203+
std::lock_guard<std::recursive_mutex> lock(_readyStateMutex);
204204
return _readyState;
205205
}
206206

207207
void WebSocketTransport::setReadyState(ReadyState readyState)
208208
{
209-
std::lock_guard<std::mutex> lock(_setReadyStateMutex);
209+
std::lock_guard<std::recursive_mutex> lock(_readyStateMutex);
210210

211211
// No state change, return
212212
if (_readyState == readyState) return;
@@ -310,7 +310,7 @@ namespace ix
310310

311311
WebSocketTransport::PollResult WebSocketTransport::poll()
312312
{
313-
if (_readyState == ReadyState::OPEN)
313+
if (getReadyState() == ReadyState::OPEN)
314314
{
315315
if (pingIntervalExceeded())
316316
{
@@ -331,7 +331,7 @@ namespace ix
331331

332332
// No timeout if state is not OPEN, otherwise computed
333333
// pingIntervalOrTimeoutGCD (equals to -1 if no ping and no ping timeout are set)
334-
int lastingTimeoutDelayInMs = (_readyState != ReadyState::OPEN) ? 0 : _pingIntervalSecs;
334+
int lastingTimeoutDelayInMs = (getReadyState() != ReadyState::OPEN) ? 0 : _pingIntervalSecs;
335335

336336
if (_pingIntervalSecs > 0)
337337
{
@@ -384,7 +384,7 @@ namespace ix
384384
closeSocket();
385385
}
386386

387-
if (_readyState == ReadyState::CLOSING && closingDelayExceeded())
387+
if (getReadyState() == ReadyState::CLOSING && closingDelayExceeded())
388388
{
389389
_rxbuf.clear();
390390
// close code and reason were set when calling close()
@@ -700,7 +700,7 @@ namespace ix
700700
}
701701

702702
// We receive a CLOSE frame from remote and are NOT the ones who triggered the close
703-
if (_readyState != ReadyState::CLOSING)
703+
if (getReadyState() != ReadyState::CLOSING)
704704
{
705705
// send back the CLOSE frame
706706
setReadyState(ReadyState::CLOSING);
@@ -742,15 +742,17 @@ namespace ix
742742
{
743743
_rxbuf.clear();
744744

745+
ReadyState rs = getReadyState();
746+
745747
// if we previously closed the connection (CLOSING state), then set state to CLOSED
746748
// (code/reason were set before)
747-
if (_readyState == ReadyState::CLOSING)
749+
if (rs == ReadyState::CLOSING)
748750
{
749751
closeSocket();
750752
setReadyState(ReadyState::CLOSED);
751753
}
752754
// if we weren't closing, then close using abnormal close code and message
753-
else if (_readyState != ReadyState::CLOSED)
755+
else if (rs != ReadyState::CLOSED)
754756
{
755757
closeSocketAndSwitchToClosedState(WebSocketCloseConstants::kAbnormalCloseCode,
756758
WebSocketCloseConstants::kAbnormalCloseMessage,
@@ -828,7 +830,8 @@ namespace ix
828830
bool compress,
829831
const OnProgressCallback& onProgressCallback)
830832
{
831-
if (_readyState != ReadyState::OPEN && _readyState != ReadyState::CLOSING)
833+
ReadyState rs = getReadyState();
834+
if (rs != ReadyState::OPEN && rs != ReadyState::CLOSING)
832835
{
833836
return WebSocketSendInfo(false);
834837
}
@@ -1076,7 +1079,7 @@ namespace ix
10761079
else if (ret <= 0)
10771080
{
10781081
closeSocket();
1079-
if (_readyState != ReadyState::CLOSING)
1082+
if (getReadyState() != ReadyState::CLOSING)
10801083
{
10811084
setReadyState(ReadyState::CLOSED);
10821085
}
@@ -1177,7 +1180,8 @@ namespace ix
11771180
{
11781181
_requestInitCancellation = true;
11791182

1180-
if (_readyState == ReadyState::CLOSING || _readyState == ReadyState::CLOSED)
1183+
ReadyState rs = getReadyState();
1184+
if (rs == ReadyState::CLOSING || rs == ReadyState::CLOSED)
11811185
{
11821186
// Wake up the socket polling thread, as
11831187
// Socket::isReadyToRead() might be still waiting the

ixwebsocket/IXWebSocketTransport.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ namespace ix
186186
// Hold the state of the connection (OPEN, CLOSED, etc...)
187187
ReadyState _readyState;
188188
// Mutex to prevent racing in setReadyState()
189-
mutable std::mutex _setReadyStateMutex;
189+
mutable std::recursive_mutex _readyStateMutex;
190190

191191
OnCloseCallback _onCloseCallback;
192192
std::string _closeReason;

0 commit comments

Comments
 (0)