Skip to content

Commit 50f1e28

Browse files
committed
Consistent locking around all uses of _status
1 parent a5d0afd commit 50f1e28

1 file changed

Lines changed: 19 additions & 9 deletions

File tree

src/AsyncWebSocket.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -499,14 +499,16 @@ bool AsyncWebSocketClient::_queueMessage(AsyncWebSocketSharedBuffer buffer, uint
499499
}
500500

501501
void AsyncWebSocketClient::close(uint16_t code, const char *message) {
502-
if (_status != WS_CONNECTED) {
503-
return;
502+
{
503+
asyncsrv::lock_guard_type lock(_lock);
504+
if (_status != WS_CONNECTED) {
505+
return;
506+
}
507+
_status = WS_DISCONNECTING;
504508
}
505509

506510
async_ws_log_w("[%s][%" PRIu32 "] CLOSE", _server->url(), _clientId);
507511

508-
_status = WS_DISCONNECTING;
509-
510512
if (code) {
511513
uint8_t packetLen = 2;
512514
if (message != NULL) {
@@ -535,6 +537,7 @@ void AsyncWebSocketClient::close(uint16_t code, const char *message) {
535537
}
536538

537539
bool AsyncWebSocketClient::ping(const uint8_t *data, size_t len) {
540+
asyncsrv::lock_guard_type lock(_lock);
538541
return _status == WS_CONNECTED && _queueControl(WS_PING, data, len);
539542
}
540543

@@ -564,9 +567,10 @@ void AsyncWebSocketClient::_onData(void *pbuf, size_t plen) {
564567
uint8_t *data = (uint8_t *)pbuf;
565568

566569
while (plen > 0) {
570+
const AwsClientStatus client_status = status();
567571
async_ws_log_v(
568572
"[%s][%" PRIu32 "] DATA plen: %" PRIu32 ", _pstate: %" PRIu8 ", _status: %" PRIu8, _server->url(), _clientId, static_cast<uint32_t>(plen), _pstate,
569-
static_cast<uint8_t>(_status)
573+
static_cast<uint8_t>(client_status)
570574
);
571575

572576
if (_pstate == STATE_FRAME_START) {
@@ -685,10 +689,13 @@ void AsyncWebSocketClient::_onData(void *pbuf, size_t plen) {
685689
_server->_handleEvent(this, WS_EVT_ERROR, (void *)&reasonCode, (uint8_t *)reasonString, strlen(reasonString));
686690
}
687691
}
692+
asyncsrv::unique_lock_type lock(_lock);
688693
if (_status == WS_DISCONNECTING) {
689694
_status = WS_DISCONNECTED;
690695
if (_client) {
691-
_client->close();
696+
auto *client = _client;
697+
lock.unlock();
698+
client->close();
692699
}
693700
} else {
694701
_status = WS_DISCONNECTING;
@@ -732,9 +739,12 @@ void AsyncWebSocketClient::_onData(void *pbuf, size_t plen) {
732739
"[%s][%" PRIu32 "] DATA frame error: len: %u, index: %" PRIu64 ", total: %" PRIu64 "\n", _server->url(), _clientId, datalen, _pinfo.index, _pinfo.len
733740
);
734741

735-
_status = WS_DISCONNECTING;
736-
if (_client) {
737-
_client->ackLater();
742+
{
743+
asyncsrv::lock_guard_type lock(_lock);
744+
_status = WS_DISCONNECTING;
745+
if (_client) {
746+
_client->ackLater();
747+
}
738748
}
739749
_queueControl(WS_DISCONNECT, data, datalen);
740750
break;

0 commit comments

Comments
 (0)