From 00ce9eea620652b97b4d3e8cd9218c6fccff8b1c Mon Sep 17 00:00:00 2001
From: Mary <me@thog.eu>
Date: Tue, 29 Jun 2021 19:37:13 +0200
Subject: Fix disposing of IPC sessions server at emulation stop (#2334)

---
 Ryujinx.Audio/Input/AudioInputManager.cs | 25 +++++++++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)

(limited to 'Ryujinx.Audio/Input/AudioInputManager.cs')

diff --git a/Ryujinx.Audio/Input/AudioInputManager.cs b/Ryujinx.Audio/Input/AudioInputManager.cs
index e098ae9e..5c1f01db 100644
--- a/Ryujinx.Audio/Input/AudioInputManager.cs
+++ b/Ryujinx.Audio/Input/AudioInputManager.cs
@@ -21,6 +21,8 @@ using Ryujinx.Common.Logging;
 using Ryujinx.Memory;
 using System;
 using System.Diagnostics;
+using System.Linq;
+using System.Threading;
 
 namespace Ryujinx.Audio.Input
 {
@@ -61,6 +63,11 @@ namespace Ryujinx.Audio.Input
         /// </summary>
         private int _activeSessionCount;
 
+        /// <summary>
+        /// The dispose state.
+        /// </summary>
+        private int _disposeState;
+
         /// <summary>
         /// Create a new <see cref="AudioInputManager"/>.
         /// </summary>
@@ -248,14 +255,28 @@ namespace Ryujinx.Audio.Input
 
         public void Dispose()
         {
-            Dispose(true);
+            if (Interlocked.CompareExchange(ref _disposeState, 1, 0) == 0)
+            {
+                Dispose(true);
+            }
         }
 
         protected virtual void Dispose(bool disposing)
         {
             if (disposing)
             {
-                // Nothing to do here.
+                // Clone the sessions array to dispose them outside the lock.
+                AudioInputSystem[] sessions;
+
+                lock (_sessionLock)
+                {
+                    sessions = _sessions.ToArray();
+                }
+
+                foreach (AudioInputSystem input in sessions)
+                {
+                    input?.Dispose();
+                }
             }
         }
     }
-- 
cgit v1.2.3-70-g09d2