diff --git a/Source/Core/Core/HW/DVD/AMMediaboard.cpp b/Source/Core/Core/HW/DVD/AMMediaboard.cpp index 60a1cf9554..982084d7b3 100644 --- a/Source/Core/Core/HW/DVD/AMMediaboard.cpp +++ b/Source/Core/Core/HW/DVD/AMMediaboard.cpp @@ -228,14 +228,25 @@ static std::array s_sockets; // TODO: Verify this. static constexpr u32 FIRST_VALID_FD = 1; +static u32 s_next_valid_fd = FIRST_VALID_FD; // Flag: next 128-byte DMA Read from the media buffer should return network config static bool s_netconfig_read_pending = false; static GuestSocket GetAvailableGuestSocket() { - for (u32 i = FIRST_VALID_FD; i < std::size(s_sockets); ++i) + // TODO: This is a workaround to avoid a race. + // + // For some unknown reasons, the fd was: + // - shared between a client (connect) and a server (accept) socket. + // - closed but still used by the other in an invalid state. + u32 count = std::size(s_sockets); + while (count--) { + const u32 i = s_next_valid_fd; + s_next_valid_fd = ++s_next_valid_fd % std::size(s_sockets); + if (i < FIRST_VALID_FD) + continue; if (s_sockets[i] == SOCKET_ERROR) return GuestSocket(i); } @@ -487,6 +498,7 @@ void Init() s_test_menu = false; s_last_error = SSC_SUCCESS; + s_next_valid_fd = FIRST_VALID_FD; s_gcam_key_a = 0; s_gcam_key_b = 0; @@ -2359,6 +2371,7 @@ void DoState(PointerWrap& p) p.Do(s_network_buffer); p.Do(s_allnet_buffer); p.Do(s_allnet_settings); + p.Do(s_next_valid_fd); p.Do(s_game_modified_ip_address); p.Do(s_netconfig_read_pending); diff --git a/Source/Core/Core/State.cpp b/Source/Core/Core/State.cpp index 5b284d1060..29aa1f3aeb 100644 --- a/Source/Core/Core/State.cpp +++ b/Source/Core/Core/State.cpp @@ -95,7 +95,7 @@ struct CompressAndDumpStateArgs static Common::WorkQueueThreadSP s_compress_and_dump_thread; // Don't forget to increase this after doing changes on the savestate system -constexpr u32 STATE_VERSION = 180; // Last changed in PR 14452 +constexpr u32 STATE_VERSION = 181; // Last changed in PR 14400 // Increase this if the StateExtendedHeader definition changes constexpr u32 EXTENDED_HEADER_VERSION = 1; // Last changed in PR 12217