diff --git a/license/server/LicenseServer.cpp b/license/server/LicenseServer.cpp index b126a23..cdc2408 100644 --- a/license/server/LicenseServer.cpp +++ b/license/server/LicenseServer.cpp @@ -204,8 +204,10 @@ void ConnectedClient::uninit(bool blocking) { network.fileDescriptor = 0; } + std::unique_lock elock{this->network.event_mutex}; auto read_event = std::exchange(this->network.readEvent, nullptr); auto write_event = std::exchange(this->network.writeEvent, nullptr); + elock.unlock(); if(blocking) { if(read_event) event_del_block(read_event); @@ -214,6 +216,8 @@ void ConnectedClient::uninit(bool blocking) { if(read_event) event_del_noblock(read_event); if(write_event) event_del_noblock(write_event); } + if(read_event) event_free(read_event); + if(write_event) event_free(write_event); } void LicenseServer::handleEventRead(int fd, short, void* ptrServer) { @@ -229,12 +233,20 @@ void LicenseServer::handleEventRead(int fd, short, void* ptrServer) { if(read < 0){ if(errno == EWOULDBLOCK) return; logError(LOG_LICENSE_CONTROLL, "Invalid read. Disconnecting remote client. Message: {}/{}", errno, strerror(errno)); - event_del_noblock(client->network.readEvent); + { + std::lock_guard elock{client->network.event_mutex}; + if(client->network.readEvent) + event_del_noblock(client->network.readEvent); + } server->closeConnection(client); return; } else if(read == 0) { logMessage(LOG_LICENSE_CONTROLL, "[CLIENT][" + client->address() + "] Received EOF for client. Removing client."); - event_del_noblock(client->network.readEvent); + { + std::lock_guard elock{client->network.event_mutex}; + if(client->network.readEvent) + event_del_noblock(client->network.readEvent); + } server->closeConnection(client); return; } diff --git a/license/server/LicenseServer.h b/license/server/LicenseServer.h index 0d73adc..e58df33 100644 --- a/license/server/LicenseServer.h +++ b/license/server/LicenseServer.h @@ -31,6 +31,8 @@ namespace license { struct { sockaddr_in remoteAddr; int fileDescriptor = 0; + + std::mutex event_mutex{}; event* readEvent = nullptr; /* protected via state_lock (check state and the use these variables) */ event* writeEvent = nullptr;