Make the program-audio append path nonisolated - #3
Merged
Conversation
Feeding the encoder had to hop to the main actor for every hardware
buffer, which is wrong twice over. Ordering: `Task { @mainactor }`
closures complete in no guaranteed order, so ~47 buffers/sec could land
out of sequence — and reordered audio buffers are audible. Latency: the
main actor is also driving SwiftUI, so audio queued behind view updates.
`appendProgramAudio` is now `nonisolated`, callable straight from the
audio thread; downstream it lands on the vendor's serial pipeline queue,
which preserves submission order. The engine keeps the buffered-source id
and its `Media` reference in an `OSAllocatedUnfairLock` so the audio
thread never touches main-actor state.
The fake gets the same shape rather than a main-actor-only convenience —
a fake that only works when called from the main actor would hide exactly
the threading bug it exists to catch.
Co-Authored-By: Claude Opus 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.
Why
ProgramAudioSink.appendProgramAudiowas@MainActor. The consumer calls it once per hardware buffer from anAVAudioEnginetap — roughly 47 times a second — so every buffer needed aTask { @MainActor }hop. That is wrong twice over:Task { @MainActor }closures are not guaranteed to complete in submission order. Reordered audio buffers are audible.What changed
appendProgramAudioisnonisolated;startProgramAudio/stopProgramAudio/isProgramAudioActivestay@MainActor(they are control-plane, called once).ProgramAudioSinkpicks upSendable.IRLStreamEnginekeeps the buffered-source id and itsMediareference in anOSAllocatedUnfairLock<ProgramAudioTarget?>, so the audio thread reads neitherprogramAudionormedia— both are main-actor state. Downstream,AudioUnit.appendBufferedAudioSampleBufferdispatches onto the serialprocessorPipelineQueue, so submission order is what survives.FakeStreamEnginegets the same shape (locked counter,nonisolatedappend) rather than a main-actor-only convenience — a fake that only works from the main actor would hide exactly the bug it exists to catch.Verification
Scripts/check-containment.shclean🤖 Generated with Claude Code