Repository navigation
fix(output): never lose an audio-toolbox buffer on an empty read - #447
Merged
devgianlu merged 1 commit intoNov 18, 2026
Merged
Conversation
The audio queue owns three buffers and hands each back only through the output callback. When the reader returned no samples, as it does at a switch to a prefetched track, or failed, the buffer was enqueued empty (rejected with kAudioQueueErr_BufferEmpty, -66686) or not at all, and was never seen again. After two such track switches a single buffer was left and playback stuttered into audible distortion until the daemon restarted. Enqueue a buffer of silence instead, and keep samples read along with an error, like the last ones before io.EOF. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
On macOS, playback can turn distorted after a few track changes and stay that way until the daemon restarts. The log then shows:
-66686iskAudioQueueErr_BufferEmpty.Cause
The audio queue owns three buffers and hands each one back only through the output callback. Two reads lose a buffer for good:
SwitchingAudioSource.readDirectreturns(0, nil)when it switches to a prefetched track and the old decoder had no samples left. The buffer is enqueued with zero bytes, and the queue rejects it.After two such switches only one buffer is left. The queue then underruns on every buffer, which sounds like distortion on every output device.
Fix
readSamplesnow does the reading.io.EOF, still play.Error()channel.Testing
TestAudioToolboxEveryReadFillsTheBuffercovers five reads: full, partial, empty at a track switch, failed, and the last samples before EOF. It needs no audio device.go test -race -tags "test_unit test_integration" ./...passes, and so doesTestAudioToolboxDefaultDevicewithGO_LIBRESPOT_TEST_AUDIO_DEVICE=1.-66686errors at track switches in the log.🤖 Generated with Claude Code