SDL: pulseaudio: Stop the threaded mainloop before destroying the context. (d8b34)

From d8b34b099333780b048b5f59cff29e437a4f32b8 Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Wed, 11 Oct 2023 14:31:37 -0400
Subject: [PATCH] pulseaudio: Stop the threaded mainloop before destroying the
 context.

Otherwise, we might trigger an assertion in libpulse.

Reference Issue #8348.

(cherry picked from commit 10fab3a39e2bd163cc45147b6afd517501801002)
---
 src/audio/pulseaudio/SDL_pulseaudio.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/src/audio/pulseaudio/SDL_pulseaudio.c b/src/audio/pulseaudio/SDL_pulseaudio.c
index 31313303abe8..2bb4409e0824 100644
--- a/src/audio/pulseaudio/SDL_pulseaudio.c
+++ b/src/audio/pulseaudio/SDL_pulseaudio.c
@@ -286,13 +286,15 @@ static void WaitForPulseOperation(pa_operation *o)
 
 static void DisconnectFromPulseServer(void)
 {
+    if (pulseaudio_threaded_mainloop != NULL) {
+        PULSEAUDIO_pa_threaded_mainloop_stop(pulseaudio_threaded_mainloop);
+    }
     if (pulseaudio_context) {
         PULSEAUDIO_pa_context_disconnect(pulseaudio_context);
         PULSEAUDIO_pa_context_unref(pulseaudio_context);
         pulseaudio_context = NULL;
     }
     if (pulseaudio_threaded_mainloop != NULL) {
-        PULSEAUDIO_pa_threaded_mainloop_stop(pulseaudio_threaded_mainloop);
         PULSEAUDIO_pa_threaded_mainloop_free(pulseaudio_threaded_mainloop);
         pulseaudio_threaded_mainloop = NULL;
     }