fix: prevent race condition in audio output source switching by reading from in-memory state with proper synchronization

This commit is contained in:
Alex P 2025-10-28 22:11:11 +02:00
parent cd7a098f76
commit 65bbcf85ad
2 changed files with 9 additions and 2 deletions

View File

@ -176,6 +176,7 @@ func SetAudioOutputSource(useUSB bool) error {
Bool("new_usb", useUSB).
Msg("Switching audio output source")
oldValue := useUSBForAudioOutput
useUSBForAudioOutput = useUSB
ensureConfigLoaded()
@ -186,6 +187,7 @@ func SetAudioOutputSource(useUSB bool) error {
}
if err := SaveConfig(); err != nil {
audioLogger.Error().Err(err).Msg("Failed to save config")
useUSBForAudioOutput = oldValue
return err
}

View File

@ -970,8 +970,13 @@ func rpcSetUsbDeviceState(device string, enabled bool) error {
}
func rpcGetAudioOutputSource() (string, error) {
ensureConfigLoaded()
return config.AudioOutputSource, nil
audioMutex.Lock()
defer audioMutex.Unlock()
if useUSBForAudioOutput {
return "usb", nil
}
return "hdmi", nil
}
func rpcSetAudioOutputSource(source string) error {