Repository navigation
Stop the audio hardware before freeing the render buffers in deinit - #148
Open
chrizzchrizz wants to merge 1 commit into
Open
chrizzchrizz wants to merge 1 commit into
chrizzchrizz wants to merge 1 commit into
Conversation
The IO thread keeps pulling from the render callback until the hardware is stopped. Releasing an AudioPlayer while it was still rendering (e.g. after an error, where pause() is a no-op) freed inOutAudioBufferList while the render block still used it and crashed in AVAudioEngineGraph::RenderToABL. Stop the hardware first, then the engine, so the IO thread does not call the manual rendering block of a stopped engine.
Author
|
Audio streaming isn't my area of expertise. So I had to rely on Claude Code to fix the issue. In my opinion, it worked well and the bug is gone, but I need your expertise for the final assessment. Thx |
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.
Problem
AudioPlayer.deinitfrees the render buffers (rendererContext.clean()) without stopping the engine or the audio hardware first. The IO thread keeps pulling from the render callback until the hardware is stopped, so releasing a player while it is still rendering is a use-after-free.This happens for example after an error:
raiseUnexpectedsets the state to.error, the engine keeps running, andpause()is a no-op in that state because the state no longer contains.running. An app that reacts to the error by releasing the player and creating a new one then crashes on the IO thread:The garbage
mNumberBuffersis the freedinOutAudioBufferList.Change
deinitstops the hardware first and then the engine, before freeing the buffers. Stopping the hardware first also avoids the IO thread calling the manual rendering block of an already stopped engine.Testing
Reproduced in an app (Amperfy) that recreates the player after a stream error. The crash is gone with this change. The full test suite passes on macOS.