From 8ad69b97e2ceefc595301b6e552695e79a4a79b3 Mon Sep 17 00:00:00 2001 From: TheLastRar Date: Thu, 28 Nov 2024 01:27:48 +0000 Subject: [PATCH] DEV9: Avoid iterating over modified vector in UDP_FixedPort --- pcsx2/DEV9/Sessions/UDP_Session/UDP_FixedPort.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/pcsx2/DEV9/Sessions/UDP_Session/UDP_FixedPort.cpp b/pcsx2/DEV9/Sessions/UDP_Session/UDP_FixedPort.cpp index 160612a0fc247..323c4d1e5c300 100644 --- a/pcsx2/DEV9/Sessions/UDP_Session/UDP_FixedPort.cpp +++ b/pcsx2/DEV9/Sessions/UDP_Session/UDP_FixedPort.cpp @@ -225,10 +225,18 @@ namespace Sessions void UDP_FixedPort::Reset() { - std::lock_guard numberlock(connectionSentry); + // Reseting a session may cause that session to close itself, + // hen that happens, connections gets modified via our close handler. + // Duplicate the vector to avoid iterating over a modified collection. + // We also avoid the issue of recursive locking (our close handler takes a lock). + std::vector connectionsCopy; + { + std::lock_guard numberlock(connectionSentry); + connectionsCopy = connections; + } - for (size_t i = 0; i < connections.size(); i++) - connections[i]->Reset(); + for (size_t i = 0; i < connectionsCopy.size(); i++) + connectionsCopy[i]->Reset(); } UDP_Session* UDP_FixedPort::NewClientSession(ConnectionKey parNewKey, bool parIsBrodcast, bool parIsMulticast)