From b6d049a17358ac584dbc2eea18885c8ccb1bbe86 Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Wed, 28 Oct 2026 13:18:03 -0400
Subject: [PATCH] audio: Add PostOpenDevice method to backend.
Runs after a successful OpenDevice call, _without the device lock held_.
The immediate need is that the JACK backend calls jack_activate() during
device open, which blocks until stuff happens on an internal, JACK-controlled
thread. But several things this thread might do will obtain the SDL audio
device lock, which is being held until after jack_activate returns. So now we
do everything _but_ activate during OpenDevice(), and activate starts
everything flowing during PostOpenDevice() now.
Other backends (PipeWire, PulseAudio, etc) might have similar problems with
other workarounds in place, but might benefit from PostOpenDevice in the same
way.
Fixes #16551.
---
src/audio/SDL_audio.c | 41 +++++++++++++++++++++++++++++-----
src/audio/SDL_sysaudio.h | 1 +
src/audio/jack/SDL_jackaudio.c | 19 ++++++++++------
3 files changed, 49 insertions(+), 12 deletions(-)
diff --git a/src/audio/SDL_audio.c b/src/audio/SDL_audio.c
index 242595c50083e..1c4d5671d109a 100644
--- a/src/audio/SDL_audio.c
+++ b/src/audio/SDL_audio.c
@@ -923,12 +923,18 @@ static bool SDL_AudioOpenDevice_Default(SDL_AudioDevice *device)
return SDL_Unsupported();
}
+static bool SDL_AudioPostOpenDevice_Default(SDL_AudioDevice *device)
+{
+ return true; // just call this success by default. This exists as a special case used by a few backends.
+}
+
// Fill in stub functions for unused driver entry points. This lets us blindly call them without having to check for validity first.
static void CompleteAudioEntryPoints(void)
{
#define FILL_STUB(x) if (!current_audio.impl.x) { current_audio.impl.x = SDL_Audio##x##_Default; }
FILL_STUB(DetectDevices);
FILL_STUB(OpenDevice);
+ FILL_STUB(PostOpenDevice);
FILL_STUB(ThreadInit);
FILL_STUB(ThreadDeinit);
FILL_STUB(WaitDevice);
@@ -1877,8 +1883,10 @@ char *SDL_GetAudioThreadName(SDL_AudioDevice *device, char *buf, size_t buflen)
// this expects the device lock to be held.
-static bool OpenPhysicalAudioDevice(SDL_AudioDevice *device, const SDL_AudioSpec *inspec)
+static bool OpenPhysicalAudioDevice(SDL_AudioDevice *device, const SDL_AudioSpec *inspec, bool *need_postopen)
{
+ *need_postopen = false;
+
SerializePhysicalDeviceClose(device); // make sure another thread that's closing didn't release the lock to let the device thread join...
// Just pretend to open a zombie device. It can still collect logical devices on a default device under the assumption they will all migrate when the default device is officially changed.
@@ -1912,9 +1920,9 @@ static bool OpenPhysicalAudioDevice(SDL_AudioDevice *device, const SDL_AudioSpec
SDL_LogDebug(SDL_LOG_CATEGORY_AUDIO, "AUDIO: attempt to reopen %s device '%s' at higher spec! (%s,%d,%d => %s,%d,%d)", devtypestr, device->name, GetShortAudioFormatName(current.format), current.channels, current.freq, GetShortAudioFormatName(spec.format), spec.channels, spec.freq);
ClosePhysicalAudioDevice(device);
- if (!OpenPhysicalAudioDevice(device, &spec)) {
+ if (!OpenPhysicalAudioDevice(device, &spec, need_postopen)) {
// no good, try to go back to our original spec...
- if (!OpenPhysicalAudioDevice(device, ¤t)) {
+ if (!OpenPhysicalAudioDevice(device, ¤t, need_postopen)) {
// okay, _now_ we're in trouble. Report the device as disconnected, since we've just broken all the existing logical devices. :(
// !!! FIXME: the logical devices need a thread to run the zombie implementation.
SDL_AudioDeviceDisconnected(device);
@@ -1954,6 +1962,8 @@ static bool OpenPhysicalAudioDevice(SDL_AudioDevice *device, const SDL_AudioSpec
return false;
}
+ *need_postopen = true; // definitely ran OpenDevice, so we'll need PostOpenDevice.
+
SDL_UpdatedAudioDeviceFormat(device); // in case the backend changed things and forgot to call this.
UpdateAudioWorkBufferSize(device);
@@ -2013,6 +2023,7 @@ SDL_AudioDeviceID SDL_OpenAudioDevice(SDL_AudioDeviceID devid, const SDL_AudioSp
}
SDL_AudioDeviceID result = 0;
+ bool need_postopen = false;
if (device) {
SDL_LogicalAudioDevice *logdev = NULL;
@@ -2021,7 +2032,7 @@ SDL_AudioDeviceID SDL_OpenAudioDevice(SDL_AudioDeviceID devid, const SDL_AudioSp
SDL_SetError("Device was already lost and can't accept new opens");
} else if ((logdev = (SDL_LogicalAudioDevice *) SDL_calloc(1, sizeof (SDL_LogicalAudioDevice))) == NULL) {
// SDL_calloc already called SDL_OutOfMemory
- } else if (!OpenPhysicalAudioDevice(device, spec)) { // if this is the first thing using this physical device, open at the OS level if necessary...
+ } else if (!OpenPhysicalAudioDevice(device, spec, &need_postopen)) { // if this is the first thing using this physical device, open at the OS level if necessary...
SDL_free(logdev);
} else {
RefPhysicalAudioDevice(device); // unref'd on successful SDL_CloseAudioDevice
@@ -2039,6 +2050,15 @@ SDL_AudioDeviceID SDL_OpenAudioDevice(SDL_AudioDeviceID devid, const SDL_AudioSp
}
ReleaseAudioDevice(device);
+ if (result && need_postopen) {
+ if (!current_audio.impl.PostOpenDevice(device)) {
+ const char *devtypestr = device->recording ? "recording" : "playback";
+ SDL_LogDebug(SDL_LOG_CATEGORY_AUDIO, "AUDIO: post-open of %s device '%s' failed: %s", devtypestr, device->name, SDL_GetError());
+ SDL_CloseAudioDevice(result);
+ result = 0;
+ }
+ }
+
if (result) {
SDL_LockRWLockForWriting(current_audio.subsystem_rwlock);
const bool inserted = SDL_InsertIntoHashTable(current_audio.device_hash_logical, (const void *) (uintptr_t) result, logdev, false);
@@ -2555,8 +2575,19 @@ void SDL_DefaultAudioDeviceChanged(SDL_AudioDevice *new_default_device)
if (needs_migration) {
// New default physical device not been opened yet? Open at the OS level...
- if (!OpenPhysicalAudioDevice(new_default_device, &spec)) {
+ bool need_postopen = false;
+ if (!OpenPhysicalAudioDevice(new_default_device, &spec, &need_postopen)) {
needs_migration = false; // uhoh, just leave everything on the old default, nothing to be done.
+ } else if (need_postopen) {
+ // drop the lock to do PostOpenDevice. This feels risky, but we don't want to migrate things if this fails.
+ ReleaseAudioDevice(new_default_device);
+ if (!current_audio.impl.PostOpenDevice(new_default_device)) {
+ const char *devtypestr = new_default_device->recording ? "recording" : "playback";
+ SDL_LogDebug(SDL_LOG_CATEGORY_AUDIO, "AUDIO: post-open of new default %s device '%s' failed: %s", devtypestr, new_default_device->name, SDL_GetError());
+ ClosePhysicalAudioDevice(new_default_device); // clean up anything the backend left half-initialized.
+ needs_migration = false; // uhoh, just leave everything on the old default, nothing to be done.
+ }
+ ObtainPhysicalAudioDeviceObj(new_default_device);
}
}
diff --git a/src/audio/SDL_sysaudio.h b/src/audio/SDL_sysaudio.h
index 6148d4a4a931d..934595cdac8f6 100644
--- a/src/audio/SDL_sysaudio.h
+++ b/src/audio/SDL_sysaudio.h
@@ -154,6 +154,7 @@ typedef struct SDL_AudioDriverImpl
{
void (*DetectDevices)(SDL_AudioDevice **default_playback, SDL_AudioDevice **default_recording);
bool (*OpenDevice)(SDL_AudioDevice *device);
+ bool (*PostOpenDevice)(SDL_AudioDevice *device); // runs after OpenDevice _without the device lock held_.
void (*ThreadInit)(SDL_AudioDevice *device); // Called by audio thread at start
void (*ThreadDeinit)(SDL_AudioDevice *device); // Called by audio thread at end
bool (*WaitDevice)(SDL_AudioDevice *device);
diff --git a/src/audio/jack/SDL_jackaudio.c b/src/audio/jack/SDL_jackaudio.c
index a5237da0a959b..bc1b3ef08cb9a 100644
--- a/src/audio/jack/SDL_jackaudio.c
+++ b/src/audio/jack/SDL_jackaudio.c
@@ -376,12 +376,6 @@ static bool JACK_OpenDevice(SDL_AudioDevice *device)
JACK_jack_on_shutdown(client, jackShutdownCallback, device);
- if (JACK_jack_activate(client) != 0) {
- SDL_free(audio_ports);
- return SDL_SetError("Failed to activate JACK client");
- }
-
- // once activated, we can connect all the ports.
for (i = 0; i < channels; i++) {
const char *sdlport = JACK_jack_port_name(device->hidden->sdlports[i]);
const char *srcport = recording ? devports[audio_ports[i]] : sdlport;
@@ -396,7 +390,17 @@ static bool JACK_OpenDevice(SDL_AudioDevice *device)
JACK_jack_free(devports);
SDL_free(audio_ports);
- // We're ready to rock and roll. :-)
+ // We're (almost, need PostOpenDevice still!) ready to rock and roll. :-)
+ return true;
+}
+
+// this runs WITHOUT the device lock!
+static bool JACK_PostOpenDevice(SDL_AudioDevice *device)
+{
+ // actually activate the client; this will start callbacks rolling, etc.
+ if (JACK_jack_activate(device->hidden->client) != 0) {
+ return SDL_SetError("Failed to activate JACK client");
+ }
return true;
}
@@ -421,6 +425,7 @@ static bool JACK_Init(SDL_AudioDriverImpl *impl)
}
impl->OpenDevice = JACK_OpenDevice;
+ impl->PostOpenDevice = JACK_PostOpenDevice;
impl->GetDeviceBuf = JACK_GetDeviceBuf;
impl->PlayDevice = JACK_PlayDevice;
impl->CloseDevice = JACK_CloseDevice;