Improve null checks and logging in WebSocket server message handling
- Add additional null checks for user data before and after locking the mutex to prevent potential crashes. - Enhance logging to provide clearer insights when user data is invalid during message queuing. - Ensure proper message copying to a local variable before accessing the message queue, improving thread safety and stability.
This commit is contained in:
committed by
Torsten (PC)
parent
d5a09f359d
commit
3a6d60e9a8
@@ -368,9 +368,33 @@ int WebSocketServer::wsCallback(struct lws *wsi,
|
||||
// Lege Nachricht in die Queue, ohne sofort lws_callback_on_writable aufzurufen
|
||||
try {
|
||||
std::cout << "Versuche Mutex zu locken..." << std::endl;
|
||||
|
||||
// Prüfe ob ud noch gültig ist, bevor wir den Mutex locken
|
||||
if (!ud) {
|
||||
std::cerr << "[RECEIVE] ud ist nullptr vor Mutex-Lock" << std::endl;
|
||||
break;
|
||||
}
|
||||
|
||||
// Kopiere die Nachricht in eine lokale Variable, bevor wir den Mutex locken
|
||||
std::string messageToQueue = errorResponse.dump();
|
||||
std::cout << "Nachricht kopiert: " << messageToQueue.length() << " Bytes" << std::endl;
|
||||
|
||||
// Prüfe ob ud noch gültig ist, bevor wir den Mutex locken
|
||||
if (!ud) {
|
||||
std::cerr << "[RECEIVE] ud ist nullptr nach Kopieren der Nachricht" << std::endl;
|
||||
break;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(ud->messageQueueMutex);
|
||||
std::cout << "Mutex gelockt, füge Nachricht zur Queue hinzu..." << std::endl;
|
||||
ud->messageQueue.push(errorResponse.dump());
|
||||
|
||||
// Prüfe ob ud noch gültig ist, nachdem wir den Mutex gelockt haben
|
||||
if (!ud) {
|
||||
std::cerr << "[RECEIVE] ud ist nullptr nach Mutex-Lock" << std::endl;
|
||||
break;
|
||||
}
|
||||
|
||||
ud->messageQueue.push(messageToQueue);
|
||||
std::cout << "ud->messageQueue.size(): " << ud->messageQueue.size() << std::endl;
|
||||
} catch (const std::exception &e) {
|
||||
std::cerr << "[RECEIVE] Fehler beim Zugriff auf messageQueue: " << e.what() << std::endl;
|
||||
|
||||
Reference in New Issue
Block a user