FluidSynth / fluidsynth

Software synthesizer based on the SoundFont 2 specifications
https://www.fluidsynth.org
GNU Lesser General Public License v2.1
1.82k stars 254 forks source link

The Pipewire audio output driver does not cleanly destroy when FluidSynth exits #1305

Closed KynikossDragonn closed 6 months ago

KynikossDragonn commented 7 months ago

FluidSynth version

FluidSynth executable version 2.3.4
Sample type=double

Describe the bug

Various PipeWire functions related to destroying the context produce numerous errors when FluidSynth exits or otherwise wants to destroy the audio context.

*** pw_stream_destroy called from wrong context, check thread and locking: Operation not permitted
*** impl_ext_end_proxy called from wrong context, check thread and locking: Operation not permitted
*** impl_ext_end_proxy called from wrong context, check thread and locking: Operation not permitted

Occasionally, this can result in a segfault or abort.

Expected behavior

The PipeWire context should be cleanly removed, and not produce these error messages.

Steps to reproduce

Launch FluidSynth in a interactable shell with the PipeWire audio driver $ fluidsynth -a pipewire Type quit into the shell

Additional context

The Mumble project ran into similiar pitfalls; more information here: https://github.com/mumble-voip/mumble/pull/6103 https://gitlab.freedesktop.org/pipewire/pipewire/-/issues/3150#note_1862593

derselbst commented 6 months ago

I found some time to look into this. However, I must admit that I never got pipewire working on my system and this time wasn't any different (error: PipeWire stream connection failed).

Unfortunately, I don't see what causes this error either. The audio driver is created here: https://github.com/FluidSynth/fluidsynth/blob/eabae3be191279afa3a0221f581d61672732d021/src/fluidsynth.c#L1135

and destroyed here: https://github.com/FluidSynth/fluidsynth/blob/eabae3be191279afa3a0221f581d61672732d021/src/fluidsynth.c#L1198

Both is done by the main-thread. I'd welcome a PR for this.

mawe42 commented 6 months ago

I can reproduce this on my machine with pipewire 1.0.3 and FluidSynth 2.3.4. No errors when playing a MIDI file and exiting via CTRL-C, but when I type in "quit" into the interactive shell the above mentioned errors are shown.

mawe42 commented 6 months ago

When I add code to stop the thread loop before destroying the stream, I don't see any errors from pipewire anymore.

mawe42 commented 6 months ago

Actually, the correct fix seems to be to grab the thread lock before destroying the stream. The error message even hints at that: "check thread and locking". I've updated the PR accordingly.