Skip to content

Commit eea0c00

Browse files
committed
Fix race condition in the SSE code
1 parent b29465d commit eea0c00

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

src/AsyncEventSource.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -199,16 +199,16 @@ AsyncEventSourceClient::~AsyncEventSourceClient() {
199199
}
200200

201201
bool AsyncEventSourceClient::_queueMessage(const char *message, size_t len) {
202-
if (_messageQueue.size() >= SSE_MAX_QUEUED_MESSAGES) {
203-
async_ws_log_w("Event message queue overflow: discard message");
204-
return false;
205-
}
206-
207202
#ifdef ESP32
208203
// length() is not thread-safe, thus acquiring the lock before this call..
209204
std::lock_guard<std::recursive_mutex> lock(_lockmq);
210205
#endif
211206

207+
if (_messageQueue.size() >= SSE_MAX_QUEUED_MESSAGES) {
208+
async_ws_log_w("Event message queue overflow: discard message");
209+
return false;
210+
}
211+
212212
if (_client) {
213213
_messageQueue.emplace_back(message, len);
214214
} else {
@@ -230,16 +230,16 @@ bool AsyncEventSourceClient::_queueMessage(const char *message, size_t len) {
230230
}
231231

232232
bool AsyncEventSourceClient::_queueMessage(AsyncEvent_SharedData_t &&msg) {
233-
if (_messageQueue.size() >= SSE_MAX_QUEUED_MESSAGES) {
234-
async_ws_log_w("Event message queue overflow: discard message");
235-
return false;
236-
}
237-
238233
#ifdef ESP32
239234
// length() is not thread-safe, thus acquiring the lock before this call..
240235
std::lock_guard<std::recursive_mutex> lock(_lockmq);
241236
#endif
242237

238+
if (_messageQueue.size() >= SSE_MAX_QUEUED_MESSAGES) {
239+
async_ws_log_w("Event message queue overflow: discard message");
240+
return false;
241+
}
242+
243243
if (_client) {
244244
_messageQueue.emplace_back(std::move(msg));
245245
} else {

0 commit comments

Comments
 (0)