Fix data race in AudioProcessor: thread-safe audioSamples and audioEnergy#443
Open
shawnzhu wants to merge 1 commit intoargmaxinc:mainfrom
Open
Fix data race in AudioProcessor: thread-safe audioSamples and audioEnergy#443shawnzhu wants to merge 1 commit intoargmaxinc:mainfrom
shawnzhu wants to merge 1 commit intoargmaxinc:mainfrom
Conversation
AudioProcessor.audioSamples and audioEnergy are written from the audio tap callback thread (in processBuffer) and read from arbitrary threads (e.g. main thread polling relativeEnergy for VAD). This is a data race that can cause stale reads, inconsistent values, or crashes. Add an NSLock to serialize access to these properties. The lock is held only for the minimum critical section — energy calculation happens outside the lock, and the audioBufferCallback is invoked after releasing the lock to avoid potential deadlocks. Fixes argmaxinc#442 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Author
|
human here: manually tested and it works for me. The use case is using Whisper's audio energy to detect speech pause. |
Author
|
@ZachNagengast would you be able to review this? You have the most context on the AudioProcessor code. Happy to address any feedback. |
Contributor
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.
Summary
NSLockto protectaudioSamplesandaudioEnergyfrom concurrent accessprocessBuffer) and read from arbitrary threads (e.g., main thread pollingrelativeEnergyfor VAD)Problem
audioSamplesandaudioEnergyare plainvarproperties with no synchronization, causing data races when:processBuffer()relativeEnergy,audioSamples, orpurgeAudioSamples()This manifests as stale/inconsistent values when polling
relativeEnergyfrom the main thread for voice activity detection, and can potentially crash in optimized builds.Test plan
swift buildsucceedsrelativeEnergyfrom main thread during recording — values should update consistentlyFixes #442
🤖 Generated with Claude Code