WhisperKit: lock audio sample/energy buffers in AudioProcessor#482
Open
yemreak wants to merge 2 commits into
Open
WhisperKit: lock audio sample/energy buffers in AudioProcessor#482yemreak wants to merge 2 commits into
yemreak wants to merge 2 commits into
Conversation
`AudioProcessor.audioSamples` and `audioEnergy` are written from the AVAudioEngine tap thread and read from arbitrary threads (VAD polling on main, transcription on a background queue). Under Swift 6 Strict Concurrency the unsynchronised access is flagged; in practice it also produces sporadic data races detectable with TSan. Introduce an `audioLock` (NSLock) and expose `audioSamples` / `audioEnergy` through locked getters and setters. `processBuffer` holds the lock only across the shared-state mutation and releases it before invoking `audioBufferCallback` to avoid potential re-entrant deadlocks. Public surface and semantics are unchanged.
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
AudioProcessor.audioSamplesandaudioEnergyare written from the AVAudioEngine tap thread and read from arbitrary threads (VAD polling on main, transcription on a background queue). Under Swift 6 Strict Concurrency this is flagged; in practice TSan also catches sporadic data races on the underlyingContiguousArrayand[(rel, avg, max, min)].Motivation
We caught this both as warnings under StrictConcurrency and as a real intermittent crash in a long-running dictation app that polls
relativeEnergyfrom the main thread while the tap is running.Changes
audioLock: NSLock._audioSamples/_audioEnergyand expose them through locked getters and setters.processBufferhold the lock only across the shared-state mutation; theaudioBufferCallbackis invoked after the lock is released to avoid potential re-entrant deadlocks.relativeEnergyandpurgeAudioSamplesupdated to go through the lock.Public surface and semantics are unchanged; callers continue to read
audioSamples/audioEnergythe same way.Testing
Built and ran the existing tests; ran our internal dictation pipeline (continuous audio capture + main-thread VAD polling) under TSan with no warnings after the change. Reverting the patch reproduces the race.