SDL: emscriptenaudio: don't forget to finalize the audio thread

https://github.com/libsdl-org/SDL/commit/fc7ed18ca1ee78c56c89595f66a1b5c13c664e49

From fc7ed18ca1ee78c56c89595f66a1b5c13c664e49 Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Wed, 5 Jul 2023 15:22:56 -0400
Subject: [PATCH] emscriptenaudio: don't forget to finalize the audio thread

---
 src/audio/emscripten/SDL_emscriptenaudio.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/src/audio/emscripten/SDL_emscriptenaudio.c b/src/audio/emscripten/SDL_emscriptenaudio.c
index f24df6dd8d9e..142d9e56637c 100644
--- a/src/audio/emscripten/SDL_emscriptenaudio.c
+++ b/src/audio/emscripten/SDL_emscriptenaudio.c
@@ -99,6 +99,10 @@ static void HandleCaptureProcess(SDL_AudioDevice *device)  // this fires when th
 
 static void EMSCRIPTENAUDIO_CloseDevice(SDL_AudioDevice *device)
 {
+    if (!device->hidden) {
+        return;
+    }
+
     MAIN_THREAD_EM_ASM({
         var SDL3 = Module['SDL3'];
         if ($0) {
@@ -138,11 +142,11 @@ static void EMSCRIPTENAUDIO_CloseDevice(SDL_AudioDevice *device)
         }
     }, device->iscapture);
 
-    if (!device->hidden) {
-        SDL_free(device->hidden->mixbuf);
-        SDL_free(device->hidden);
-        device->hidden = NULL;
-    }
+    SDL_free(device->hidden->mixbuf);
+    SDL_free(device->hidden);
+    device->hidden = NULL;
+
+    SDL_AudioThreadFinalize(device);
 }
 
 EM_JS_DEPS(sdlaudio, "$autoResumeAudioContext,$dynCall");