-
Notifications
You must be signed in to change notification settings - Fork 97
Fix race conditions in the SSE and WS code #414
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -192,23 +192,24 @@ AsyncEventSourceClient::AsyncEventSourceClient(AsyncWebServerRequest *request, A | |
|
|
||
| AsyncEventSourceClient::~AsyncEventSourceClient() { | ||
| #ifdef ESP32 | ||
| // Protect message queue access (size checks and modifications) which is not thread-safe. | ||
| std::lock_guard<std::recursive_mutex> lock(_lockmq); | ||
| #endif | ||
| _messageQueue.clear(); | ||
| close(); | ||
| } | ||
|
|
||
| bool AsyncEventSourceClient::_queueMessage(const char *message, size_t len) { | ||
| #ifdef ESP32 | ||
| // Protect message queue access (size checks and modifications) which is not thread-safe. | ||
| std::lock_guard<std::recursive_mutex> lock(_lockmq); | ||
| #endif | ||
|
|
||
| if (_messageQueue.size() >= SSE_MAX_QUEUED_MESSAGES) { | ||
| async_ws_log_w("Event message queue overflow: discard message"); | ||
| return false; | ||
| } | ||
|
|
||
| #ifdef ESP32 | ||
| // length() is not thread-safe, thus acquiring the lock before this call.. | ||
| std::lock_guard<std::recursive_mutex> lock(_lockmq); | ||
| #endif | ||
|
|
||
| if (_client) { | ||
| _messageQueue.emplace_back(message, len); | ||
| } else { | ||
|
|
@@ -230,16 +231,16 @@ bool AsyncEventSourceClient::_queueMessage(const char *message, size_t len) { | |
| } | ||
|
|
||
| bool AsyncEventSourceClient::_queueMessage(AsyncEvent_SharedData_t &&msg) { | ||
| #ifdef ESP32 | ||
| // Protect message queue access (size checks and modifications) which is not thread-safe. | ||
| std::lock_guard<std::recursive_mutex> lock(_lockmq); | ||
| #endif | ||
|
|
||
| if (_messageQueue.size() >= SSE_MAX_QUEUED_MESSAGES) { | ||
| async_ws_log_w("Event message queue overflow: discard message"); | ||
| return false; | ||
| } | ||
|
|
||
| #ifdef ESP32 | ||
| // length() is not thread-safe, thus acquiring the lock before this call.. | ||
| std::lock_guard<std::recursive_mutex> lock(_lockmq); | ||
| #endif | ||
|
|
||
| if (_client) { | ||
| _messageQueue.emplace_back(std::move(msg)); | ||
| } else { | ||
|
|
@@ -261,7 +262,7 @@ bool AsyncEventSourceClient::_queueMessage(AsyncEvent_SharedData_t &&msg) { | |
|
|
||
| void AsyncEventSourceClient::_onAck(size_t len __attribute__((unused)), uint32_t time __attribute__((unused))) { | ||
| #ifdef ESP32 | ||
| // Same here, acquiring the lock early | ||
| // Protect message queue access (size checks and modifications) which is not thread-safe. | ||
| std::lock_guard<std::recursive_mutex> lock(_lockmq); | ||
| #endif | ||
|
|
||
|
|
@@ -288,11 +289,11 @@ void AsyncEventSourceClient::_onAck(size_t len __attribute__((unused)), uint32_t | |
| } | ||
|
|
||
| void AsyncEventSourceClient::_onPoll() { | ||
| if (_messageQueue.size()) { | ||
| #ifdef ESP32 | ||
| // Same here, acquiring the lock early | ||
| std::lock_guard<std::recursive_mutex> lock(_lockmq); | ||
| // Protect message queue access (size checks and modifications) which is not thread-safe. | ||
| std::lock_guard<std::recursive_mutex> lock(_lockmq); | ||
| #endif | ||
| if (_messageQueue.size()) { | ||
| _runQueue(); | ||
| } | ||
| } | ||
|
|
@@ -379,12 +380,12 @@ void AsyncEventSource::_addClient(AsyncEventSourceClient *client) { | |
| } | ||
|
|
||
| void AsyncEventSource::_handleDisconnect(AsyncEventSourceClient *client) { | ||
| if (_disconnectcb) { | ||
| _disconnectcb(client); | ||
| } | ||
| #ifdef ESP32 | ||
| std::lock_guard<std::recursive_mutex> lock(_client_queue_lock); | ||
| #endif | ||
| if (_disconnectcb) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change may be incorrect. Client callbacks shouldn't be called while holding the lock -- if the client does something like "hmm, I'm done with this server now, lets destruct it" we'll have a bad time.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The same issue existed here: void AsyncEventSource::_addClient(AsyncEventSourceClient *client) { _adjust_inflight_window();
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added 2 commits in #415 to fix the 2 situations. |
||
| _disconnectcb(client); | ||
| } | ||
| for (auto i = _clients.begin(); i != _clients.end(); ++i) { | ||
| if (i->get() == client) { | ||
| _clients.erase(i); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.