fix(sdl): clear the PulseAudio write callback before closing a stream #26132
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/26132"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by GeremC on 2026-11-23T10:00:20Z.
What
The bundled SDL2's PulseAudio backend writes to freed memory when an audio device is closed while the server still sends requests for its stream. That is the crash of #23020 (
pa_stream_unref→pa_hashmap_freeon the libpulse mainloop thread). As found there, it is not specific to the test: any title that closes a playing AudioOut port on a PulseAudio orpipewire-pulsehost can hit it.PULSEAUDIO_CloseDevice(3rdparty/SDL2/src/audio/pulseaudio/SDL_pulseaudio.c) callspa_stream_disconnectandpa_stream_unref, then freesthis->hidden. libpulse keeps the stream until the server answersDELETE_PLAYBACK_STREAM, and its write callback still hasthis->hiddenas userdata. AREQUESTthat arrives first runsWriteCallback, which doesh->bytes_requested += nbyteson the freed block. SDL'sSDL2branch and SDL3mainhave the same code.This patches the bundled source the way
cmake/PatchSdlWayland.cmakealready patches SDL's Wayland files:cmake/PatchSdlPulseAudio.cmakecopiesSDL_pulseaudio.cinto the build tree, applies a one-line patch and swaps the file inSDL2-static's sources. The patch callspa_stream_set_write_callback(stream, NULL, NULL)beforepa_stream_disconnect. That runs under the threaded mainloop lock thatPULSEAUDIO_CloseDevicealready holds, and the callbacks run on the mainloop thread with that lock held, so no callback can be running at the same time. Configuration stops ifSDL2-staticno longer builds that file, andgit applyfails it if the context moves. It applies only when SDL is built with PulseAudio (HAVE_PULSEAUDIO), so builds without libpulse are unchanged. The read callback ignores its userdata, and the state callback has none. #23060 keeps the port bounds test on SDL's dummy driver. This PR does not change that and does not depend on it.The other problem reported in #23020,
PULSEAUDIO_OpenDevicewaiting forever forPA_STREAM_READYwhen the server runs out of descriptors, is not addressed here.Tested
Linux Mint 22.3 (Ubuntu 24.04 base), kernel 7.0, i7-13700K, PipeWire 1.0.5 (
pipewire-pulse), libpulse 16.1.AddressSanitizer A/B on the bundled SDL2. I built
3rdparty/SDL2alone twice with clang 18 and-fsanitize=address, with the PulseAudio backend only: once unchanged, and once with the patch text fromcmake/PatchSdlPulseAudio.cmakeapplied by the samegit applycommand. A small program opens 32 devices (48 kHz, stereo, F32), queues audio, unpauses them, waits 5 ms and closes them, for 30 rounds. I ran it 60 times per build, 4 at a time, with 6 busy-loop processes loading the CPU:heap-use-after-freeEvery report has the same stack as #23020's ASan run:
READ of size 4 ... thread T1 (PulseMainloop),#0 WriteCallback SDL_pulseaudio.c:381,pa_pdispatch_run. An earlier run of 40 on the unchanged build gave 26.Full build (GCC 13.3, Release,
BUILD_TESTING=ON): configuration writesbuild/sdl-pulseaudio/src/audio/pulseaudio/SDL_pulseaudio.cwith the added line, and the build compiles that file instead of the submodule's.ctest -R 'audio|Audio|pad_|vibration|voice': 186/186 passed.guest_audio_out_port_bounds_testsagainst the realpipewire-pulse, before the change: 300 runs, 4 at a time with the CPU loaded, all passed. The race is rarer with this PipeWire version than with the 1.6.11 of #23020, which is why I used ASan for the A/B. With 12 at a time, the processes hang inPULSEAUDIO_OpenDevicewhilepipewire-pulselogscan't load spa node: Too many open files. That is the separate hang above, and it happens without this change.python3 tools/check_conventions.py --base origin/mainpasses.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
https://claude.ai/code/session_014W1f1RvDMX4BmYudy9J2Vj
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-23T10:10:42Z.
Review: COMMENTED
Reviewed — no blocking findings
The changed CMake integration and patch are coherent by inspection, but build and runtime checks were not run in this review.
What I ran
Reviewed head
bcdd50b12040a04ac04e60354eec1c5154253888using the provided review reader. No shell or test runner was available.Results
Findings
Blocking
None found in the changed code reviewed.
Non-blocking
Sources
Not verified
The bundled SDL PulseAudio implementation was not readable through the provided reader, so callback locking, callback userdata use, and close ordering were not independently confirmed. Build configuration, compilation, tests, and runtime behavior were not run. The PR description's test results were not independently verified.
Files read:
description.md,commits.md,manifest.json,diff.patch,CMakeLists.txt,cmake/PatchSdlPulseAudio.cmake, and the first 90 lines ofcmake/PatchSdlWayland.cmakeas targeted context. Changed files reviewed:CMakeLists.txtandcmake/PatchSdlPulseAudio.cmake(2 of 2). No changed files were excluded.To get this to a good state
Nothing is required. Optional: attach the reported build and test output for this head.
Imported from boykopovar/AnyPS5. Originally posted by mihiii1337 on 2026-11-23T10:12:49Z.
Review: APPROVED
Reviewed exact head
bcdd50b12040a04ac04e60354eec1c5154253888against the pinned SDL2 source and PulseAudio 16.1 API implementation.I did not run Linux/PipeWire audio, ASan or the reported 186-test suite, so the author's 42/60 to 0/60 UAF A/B remains external runtime evidence. The separate OpenDevice descriptor-exhaustion hang remains out of scope.
AI-assisted: yes.