Skip to content

SDL_CloseAudioDevice hangs when closing a stream initialized with a very low audio rate. (SDL2, PulseAudio) #9829

Open
@AliceLR

Description

@AliceLR

Operating system: Fedora KDE Plasma Desktop 40
Device: ASUS X555LA
Branch: SDL2
Release: 2.30.3
Release: 2.30.2
Release: 2.30.1 (repository)

Operating system: Fedora KDE Plasma Desktop 39
Device: Acer Aspire E 15 E5-575G-53VG
Release: 2.28.5 (repository)

After initializing an audio device with a very low audio rate (10Hz used in the example), SDL_CloseAudioDevice temporarily hangs when attempting to close it. The amount of time it hangs seems inversely proportional to the audio rate requested; it takes a couple of seconds to close a device with 1024Hz, several minutes for 10Hz, and up to an hour for 1Hz. This only happens with the PulseAudio driver.

Expected behavior: SDL_CloseAudioDevice should not take this long to close an opened device and/or SDL_OpenAudioDevice should fail for rates where this bug is noticeable.

Example: compile with gcc -g $(sdl2-config --cflags --libs) and run with gdb. SDL_OpenAudioDevice will succeed, and then SDL_CloseAudioDevice will wait on its mutex for a very long time:

#include <SDL.h>
#include <stdio.h>
#include <string.h>

static void sdl_audio_callback(void *userdata, Uint8 *stream, int len)
{
  memset(stream, 0, len);
}

int main(int argc, char *argv[])
{
  SDL_AudioDeviceID audio_device;
  SDL_AudioSpec audio_settings;
  SDL_AudioSpec desired_spec =
  {
    /* rate     */ 10,
    /* format   */ AUDIO_S16SYS,
    /* channels */ 2,
    /* silence  */ 0,
    /* frames   */ 1024,
    /* padding  */ 0,
    /* size     */ 0,
    /* callback */ sdl_audio_callback,
    /* userdata */ NULL
  };

  if(SDL_Init(SDL_INIT_AUDIO) < 0)
  {
    fprintf(stderr, "failed to init SDL: %s\n", SDL_GetError());
    return 1;
  }

  audio_device = SDL_OpenAudioDevice(NULL, 0, &desired_spec, &audio_settings, 0);
  if(!audio_device)
  {
    fprintf(stderr, "failed to init SDL audio device: %s\n", SDL_GetError());
    return 1;
  }

  // hang SDL
  SDL_CloseAudioDevice(audio_device);
  SDL_Quit();
  return 0;
}

(Sorry if this is a duplicate report; I saw similar closed reports for past releases and for SDL3 but none specifically associated with PulseAudio and the requested audio rate.)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions