DEV9: Fix race-condition while handling closed connection

This commit is contained in:
TheLastRar 2024-12-03 22:28:12 +00:00 committed by Ty
parent f317ba327c
commit f91f39afcd
2 changed files with 6 additions and 4 deletions

View File

@ -37,10 +37,10 @@ public:
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()

View File

@ -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