Skip to content

Commit c21d1bc

Browse files
Fix leftovers in locking improvements from past commit 085f278 (#427)
* Fix websocket client lifetime races during broadcast * Address websocket review feedback * Removed behavior changes to keep teh change set to fixes following commit 085f278 --------- Co-authored-by: Mitch Bradley <wmb@firmworks.com>
1 parent b01d236 commit c21d1bc

2 files changed

Lines changed: 21 additions & 6 deletions

File tree

src/AsyncWebSocket.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -357,11 +357,12 @@ void AsyncWebSocketClient::_onAck(size_t len, uint32_t time) {
357357
}
358358

359359
void AsyncWebSocketClient::_onPoll() {
360+
asyncsrv::unique_lock_type lock(_lock);
361+
360362
if (!_client) {
361363
return;
362364
}
363365

364-
asyncsrv::unique_lock_type lock(_lock);
365366
if (_client && _client->canSend() && (!_controlQueue.empty() || !_messageQueue.empty())) {
366367
_runQueue();
367368
} else if (_keepAlivePeriod > 0 && (millis() - _lastMessageTime) >= _keepAlivePeriod && (_controlQueue.empty() && _messageQueue.empty())) {
@@ -444,12 +445,12 @@ bool AsyncWebSocketClient::canSend() const {
444445
}
445446

446447
bool AsyncWebSocketClient::_queueControl(uint8_t opcode, const uint8_t *data, size_t len, bool mask) {
448+
asyncsrv::lock_guard_type lock(_lock);
449+
447450
if (!_client) {
448451
return false;
449452
}
450453

451-
asyncsrv::lock_guard_type lock(_lock);
452-
453454
_controlQueue.emplace_back(opcode, data, len, mask);
454455
async_ws_log_v("[%s][%" PRIu32 "] QUEUE CTRL (%u) << %" PRIu8, _server->url(), _clientId, _controlQueue.size(), opcode);
455456

@@ -461,12 +462,12 @@ bool AsyncWebSocketClient::_queueControl(uint8_t opcode, const uint8_t *data, si
461462
}
462463

463464
bool AsyncWebSocketClient::_queueMessage(AsyncWebSocketSharedBuffer buffer, uint8_t opcode, bool mask) {
464-
if (!_client || buffer->size() == 0 || _status != WS_CONNECTED) {
465+
asyncsrv::unique_lock_type lock(_lock);
466+
467+
if (!_client || !buffer || buffer->empty() || _status != WS_CONNECTED) {
465468
return false;
466469
}
467470

468-
asyncsrv::unique_lock_type lock(_lock);
469-
470471
if (_messageQueue.size() >= WS_MAX_QUEUED_MESSAGES) {
471472
if (closeWhenFull) {
472473
_status = WS_DISCONNECTED;
@@ -545,6 +546,7 @@ void AsyncWebSocketClient::_onError(int8_t err) {
545546
}
546547

547548
void AsyncWebSocketClient::_onTimeout(uint32_t time) {
549+
asyncsrv::lock_guard_type lock(_lock);
548550
if (!_client) {
549551
return;
550552
}
@@ -553,7 +555,9 @@ void AsyncWebSocketClient::_onTimeout(uint32_t time) {
553555
}
554556

555557
void AsyncWebSocketClient::_onDisconnect() {
558+
asyncsrv::lock_guard_type lock(_lock);
556559
async_ws_log_v("[%s][%" PRIu32 "] DISCONNECT", _server->url(), _clientId);
560+
_status = WS_DISCONNECTED;
557561
_client = nullptr;
558562
_server->_handleDisconnect(this);
559563
}
@@ -947,6 +951,8 @@ bool AsyncWebSocketClient::binary(const __FlashStringHelper *data, size_t len) {
947951
#endif
948952

949953
IPAddress AsyncWebSocketClient::remoteIP() const {
954+
asyncsrv::lock_guard_type lock(_lock);
955+
950956
if (!_client) {
951957
return IPAddress((uint32_t)0U);
952958
}
@@ -955,6 +961,8 @@ IPAddress AsyncWebSocketClient::remoteIP() const {
955961
}
956962

957963
uint16_t AsyncWebSocketClient::remotePort() const {
964+
asyncsrv::lock_guard_type lock(_lock);
965+
958966
if (!_client) {
959967
return 0;
960968
}
@@ -1031,6 +1039,7 @@ AsyncWebSocketClient *AsyncWebSocket::client(uint32_t id) {
10311039
}
10321040

10331041
void AsyncWebSocket::close(uint32_t id, uint16_t code, const char *message) {
1042+
asyncsrv::lock_guard_type lock(_lock);
10341043
if (AsyncWebSocketClient *c = client(id)) {
10351044
c->close(code, message);
10361045
}
@@ -1062,6 +1071,7 @@ void AsyncWebSocket::cleanupClients(uint16_t maxClients) {
10621071
}
10631072

10641073
bool AsyncWebSocket::ping(uint32_t id, const uint8_t *data, size_t len) {
1074+
asyncsrv::lock_guard_type lock(_lock);
10651075
AsyncWebSocketClient *c = client(id);
10661076
return c && c->ping(data, len);
10671077
}
@@ -1081,6 +1091,7 @@ AsyncWebSocket::SendStatus AsyncWebSocket::pingAll(const uint8_t *data, size_t l
10811091
}
10821092

10831093
bool AsyncWebSocket::text(uint32_t id, const uint8_t *message, size_t len) {
1094+
asyncsrv::lock_guard_type lock(_lock);
10841095
AsyncWebSocketClient *c = client(id);
10851096
return c && c->text(makeSharedBuffer(message, len));
10861097
}
@@ -1127,6 +1138,7 @@ bool AsyncWebSocket::text(uint32_t id, AsyncWebSocketMessageBuffer *buffer) {
11271138
return enqueued;
11281139
}
11291140
bool AsyncWebSocket::text(uint32_t id, AsyncWebSocketSharedBuffer buffer) {
1141+
asyncsrv::lock_guard_type lock(_lock);
11301142
AsyncWebSocketClient *c = client(id);
11311143
return c && c->text(buffer);
11321144
}
@@ -1190,6 +1202,7 @@ AsyncWebSocket::SendStatus AsyncWebSocket::textAll(AsyncWebSocketSharedBuffer bu
11901202
}
11911203

11921204
bool AsyncWebSocket::binary(uint32_t id, const uint8_t *message, size_t len) {
1205+
asyncsrv::lock_guard_type lock(_lock);
11931206
AsyncWebSocketClient *c = client(id);
11941207
return c && c->binary(makeSharedBuffer(message, len));
11951208
}
@@ -1226,6 +1239,7 @@ bool AsyncWebSocket::binary(uint32_t id, AsyncWebSocketMessageBuffer *buffer) {
12261239
return enqueued;
12271240
}
12281241
bool AsyncWebSocket::binary(uint32_t id, AsyncWebSocketSharedBuffer buffer) {
1242+
asyncsrv::lock_guard_type lock(_lock);
12291243
AsyncWebSocketClient *c = client(id);
12301244
return c && c->binary(buffer);
12311245
}

src/AsyncWebSocket.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ class AsyncWebSocketClient {
303303
uint16_t remotePort() const;
304304

305305
bool shouldBeDeleted() const {
306+
asyncsrv::lock_guard_type lock(_lock);
306307
return !_client;
307308
}
308309

0 commit comments

Comments
 (0)