From 234f7ca298e30a60945a546cb0b22b446c27d38d Mon Sep 17 00:00:00 2001 From: GreemDev Date: Tue, 4 Nov 2025 20:48:36 -0600 Subject: [PATCH] Fix socket closing on shutdown Previously, sockets were only ever closed when the game specifically requested it. Thanks @comex on GitHub for the patch submitted via the issues page. Co-Authored-By: comex <47517+comex@users.noreply.github.com> --- .../HOS/Services/Sockets/Bsd/BsdContext.cs | 29 +++++++++++++++---- .../HOS/Services/Sockets/Bsd/IClient.cs | 8 +++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/Ryujinx.HLE/HOS/Services/Sockets/Bsd/BsdContext.cs b/src/Ryujinx.HLE/HOS/Services/Sockets/Bsd/BsdContext.cs index d3a88998f..b7bd3e6da 100644 --- a/src/Ryujinx.HLE/HOS/Services/Sockets/Bsd/BsdContext.cs +++ b/src/Ryujinx.HLE/HOS/Services/Sockets/Bsd/BsdContext.cs @@ -7,7 +7,7 @@ using System.Threading; namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd { - class BsdContext + class BsdContext : IDisposable { private static readonly ConcurrentDictionary _registry = new(); @@ -158,6 +158,20 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd return LinuxError.SUCCESS; } + public void Dispose() + { + int count; + + lock (_lock) + { + count = _fds.Count; + } + + for (int fd = 0; fd < count; fd++) { + CloseFileDescriptor(fd); + } + } + public static BsdContext GetOrRegister(ulong processId) { BsdContext context = GetContext(processId); @@ -174,12 +188,15 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd public static BsdContext GetContext(ulong processId) { - if (!_registry.TryGetValue(processId, out BsdContext processContext)) - { - return null; - } + return _registry.GetValueOrDefault(processId); + } - return processContext; + public static void DeleteContext(ulong processId) + { + if (_registry.Remove(processId, out BsdContext context)) + { + context.Dispose(); + } } } } diff --git a/src/Ryujinx.HLE/HOS/Services/Sockets/Bsd/IClient.cs b/src/Ryujinx.HLE/HOS/Services/Sockets/Bsd/IClient.cs index fe37ca4fa..f22b1eb14 100644 --- a/src/Ryujinx.HLE/HOS/Services/Sockets/Bsd/IClient.cs +++ b/src/Ryujinx.HLE/HOS/Services/Sockets/Bsd/IClient.cs @@ -1165,5 +1165,13 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd return WriteBsdResult(context, newSockFd, errno); } + + + public override void DestroyAtExit() + { + if (_context != null) { + _context.Dispose(); + } + } } }