diff --git a/src/AsyncEventSource.cpp b/src/AsyncEventSource.cpp index 9e521803..e06c12ec 100644 --- a/src/AsyncEventSource.cpp +++ b/src/AsyncEventSource.cpp @@ -233,9 +233,7 @@ void AsyncEventSourceClient::_onDisconnect() { } void AsyncEventSourceClient::close() { - if (_client) { - _client->close(); - } + _client->close(); } void AsyncEventSourceClient::_detach() { @@ -328,19 +326,9 @@ void AsyncEventSource::authorizeConnect(ArAuthorizeConnectHandler cb) { } void AsyncEventSource::_addClient(const AsyncEventSourceClientPtr &client, AsyncWebServerRequest *request) { - if (!client) { - return; - } - if (!client->_client && request) { - // Adopt the connection from the request. The client was pre-allocated - // (before any bytes were written to the socket) in AsyncEventSourceResponse::_respond(). - client->_client = request->clientRelease(); - } - if (!client->_client) { - // No connection to adopt (e.g. the request was already gone): drop the - // half-constructed client instead of registering a zombie. - return; - } + // The client was pre-allocated in AsyncEventSourceResponse::_respond(), before + // any bytes were written to the socket. + client->_client = request->clientRelease(); // Network callbacks are attached here (not in the constructor) because they rely on // shared_from_this(), which requires shared_ptr ownership. @@ -383,20 +371,16 @@ void AsyncEventSource::_handleDisconnect(AsyncEventSourceClient *client) { } void AsyncEventSource::close() { - // Snapshot the list under the lock, close outside it: a synchronous - // _onDisconnect() inside c->close() erases list nodes, which would - // otherwise invalidate the loop iterator (and could deadlock). - std::vector live; + // Take the list, then close outside the lock. Close unconditionally: a client + // can be disconnected with its report still queued, and closing reports it + // while we hold the handle, rather than from its destructor. + std::list closing; { asyncsrv::lock_guard_type lock(_client_queue_lock); - for (const auto &c : _clients) { - live.push_back(c); - } + closing.splice(closing.end(), _clients); } - for (const auto &c : live) { - if (c->connected()) { - c->close(); - } + for (const auto &c : closing) { + c->close(); } } diff --git a/src/AsyncEventSource.h b/src/AsyncEventSource.h index c15567db..d128e1bb 100644 --- a/src/AsyncEventSource.h +++ b/src/AsyncEventSource.h @@ -72,6 +72,7 @@ using AsyncEvent_SharedData_t = std::shared_ptr; class AsyncEventSourceClient : public std::enable_shared_from_this { private: friend class AsyncEventSource; + friend class AsyncEventSourceResponse; AsyncClient *_client; AsyncEventSource *_server; uint32_t _lastId{0}; @@ -89,19 +90,16 @@ class AsyncEventSourceClient : public std::enable_shared_from_thisconnected(); + return _client->connected(); } uint32_t lastId() const { return _lastId; @@ -188,12 +186,6 @@ class AsyncEventSourceClient : public std::enable_shared_from_this _clients; // Same as for individual messages, protect mutations of _clients list @@ -214,6 +208,10 @@ class AsyncEventSource : public AsyncWebHandler { // this method manipulates in-fligh data size for connected client depending on number of active connections void _adjust_inflight_window(); + // gives a pre-allocated client the request's connection, then registers it + void _addClient(const AsyncEventSourceClientPtr &client, AsyncWebServerRequest *request); + void _handleDisconnect(AsyncEventSourceClient *client); + public: typedef enum { DISCARDED = 0, @@ -293,21 +291,17 @@ class AsyncEventSource : public AsyncWebHandler { */ std::vector getClients() const; - // system callbacks (do not call from user code!) - // registers a pre-allocated client; when a request is supplied, adopts its - // AsyncClient (ownership transfer) before registration - void _addClient(const AsyncEventSourceClientPtr &client, AsyncWebServerRequest *request = nullptr); - void _handleDisconnect(AsyncEventSourceClient *client); bool canHandle(AsyncWebServerRequest *request) const final; void handleRequest(AsyncWebServerRequest *request) final; }; class AsyncEventSourceResponse : public AsyncWebServerResponse { private: + friend class AsyncEventSource; AsyncEventSource *_server; + AsyncEventSourceResponse(AsyncEventSource *server); public: - AsyncEventSourceResponse(AsyncEventSource *server); void _respond(AsyncWebServerRequest *request) override; size_t _ack(AsyncWebServerRequest *request, size_t len, uint32_t time) override { return 0;