hydrogen-music / hydrogen

The advanced drum machine for Linux, macOS, and Windows
http://www.hydrogen-music.org
GNU General Public License v2.0
1.01k stars 172 forks source link

Improve resilience in audio driver handling #1904

Closed theGreatWhiteShark closed 7 months ago

theGreatWhiteShark commented 7 months ago

The audio driver can be changed/switched and in the process it becomes nullptr .

Between stopping the driver (resetting it to nullptr) and assigning a new one the AudioEngine is unlocked. Especially in case the "Auto" driver was selected and multiple drivers are tried before finding a fitting one there are quite a number of opportunities for causing a crash by attempting to call its methods.

This does not happen on a regular basis, tough, wasn't reported and I never encountered in using "production" code. But due to another bug during local development Hydrogen crashed during startup with almost no way to bring it up again.

To ensure this never happens in productive code, I introduced checks for nullptr on all calls to its members. It's neither atomic nor guarded by a mutex. But it is already more resilent and, I think, sufficiently save.

The most cleanest way would to enclose all calls to audio and MIDI drivers with AudioEngine locks and rewrite the driver restarting in such a way it does not need an unlock before having assigned a valid driver. But this is a lot of rewriting in very essential parts and I will put this in master and not the upcoming patch release.