From f91f39afcd04a3d88816e46aa6805b66e30a44c4 Mon Sep 17 00:00:00 2001 From: TheLastRar Date: Tue, 3 Dec 2024 22:28:12 +0000 Subject: [PATCH] DEV9: Fix race-condition while handling closed connection --- pcsx2/DEV9/ThreadSafeMap.h | 4 ++-- pcsx2/DEV9/sockets.cpp | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/pcsx2/DEV9/ThreadSafeMap.h b/pcsx2/DEV9/ThreadSafeMap.h index 131e23312bb80..7409d7ca9c352 100644 --- a/pcsx2/DEV9/ThreadSafeMap.h +++ b/pcsx2/DEV9/ThreadSafeMap.h @@ -37,10 +37,10 @@ class ThreadSafeMap map[key] = value; } - void Remove(Key key) + bool Remove(Key key) { std::unique_lock modifyLock(accessMutex); - map.erase(key); + return map.erase(key) == 1; } void Clear() diff --git a/pcsx2/DEV9/sockets.cpp b/pcsx2/DEV9/sockets.cpp index 21aeb4a7978e0..fbd73f1cc43fb 100644 --- a/pcsx2/DEV9/sockets.cpp +++ b/pcsx2/DEV9/sockets.cpp @@ -527,7 +527,8 @@ int SocketAdapter::SendFromConnection(ConnectionKey Key, IP_Packet* ipPkt) void SocketAdapter::HandleConnectionClosed(BaseSession* sender) { const ConnectionKey key = sender->key; - connections.Remove(key); + if (!connections.Remove(key)) + return; // Defer deleting the connection untill we have left the calling session's callstack if (std::this_thread::get_id() == sendThreadId) @@ -558,7 +559,8 @@ void SocketAdapter::HandleConnectionClosed(BaseSession* sender) void SocketAdapter::HandleFixedPortClosed(BaseSession* sender) { const ConnectionKey key = sender->key; - connections.Remove(key); + if (!connections.Remove(key)) + return; fixedUDPPorts.Remove(key.ps2Port); // Defer deleting the connection untill we have left the calling session's callstack