kvm/internal/audio
Alex P 051950f220 Fix critical deadlock when switching audio sources
Problem:
When switching audio sources (USB to HDMI or vice versa), the application
would hang indefinitely. This was caused by a deadlock between Go and C
layers:

1. Main thread calls SetAudioOutputSource() → stopOutputAudio()
2. stopOutputAudio() calls outputRelay.Stop() which waits for goroutine
3. Goroutine is blocked in ReadMessage() holding Go mutex
4. ReadMessage() calls blocking C function jetkvm_audio_read_encode()
5. C function is blocked reading from ALSA device
6. Disconnect() can't acquire Go mutex to clean up
7. Deadlock: Main thread waiting for goroutine, goroutine waiting for ALSA

Solution:
Release the Go mutex BEFORE calling blocking C functions in ReadMessage()
and WriteMessage(). The C layer has its own pthread mutex protection and
handles stop requests via atomic flags. This allows:
- Disconnect() to acquire the mutex immediately
- C layer to detect stop request and return quickly
- Goroutines to exit cleanly
- Audio source switching to work flawlessly

Fixes:
- internal/audio/cgo_source.go:ReadMessage() - Release mutex before C call
- internal/audio/cgo_source.go:WriteMessage() - Release mutex before C call

This fix eliminates the hang when switching between USB and HDMI audio
sources.
2025-11-18 01:41:46 +02:00
..
c Update audio.c header comment to reflect variable sample rate 2025-11-18 00:54:04 +02:00
cgo_source.go Fix critical deadlock when switching audio sources 2025-11-18 01:41:46 +02:00
cgo_source_stub.go Add runtime configurable audio parameters with UI controls 2025-11-17 21:51:08 +02:00
relay.go Add runtime configurable audio parameters with UI controls 2025-11-17 21:51:08 +02:00
source.go Fix: improve EDID compatibility and add audio configuration options 2025-11-18 00:53:15 +02:00