Fix AudioProcessor Sendable capture#412
Open
naykutguven wants to merge 3 commits intoargmaxinc:swift-6from
Open
Fix AudioProcessor Sendable capture#412naykutguven wants to merge 3 commits intoargmaxinc:swift-6from
naykutguven wants to merge 3 commits intoargmaxinc:swift-6from
Conversation
There was a problem hiding this comment.
Pull request overview
Improves AudioProcessor concurrency safety by serializing state access behind a lock and marking the class @unchecked Sendable for use across audio callbacks and async streams.
Changes:
- Introduced
stateLockand private*Storagebacking fields; migrated public properties to locked computed accessors. - Updated buffer processing and recording lifecycle methods to lock around shared state and invoke callbacks outside the lock.
- Adjusted async stream termination behavior to stop recording without separately clearing the callback.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
574221a to
1d989b6
Compare
1d989b6 to
af51181
Compare
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.
This pull request makes significant improvements to the thread safety and concurrency handling of the
AudioProcessorclass inAudioProcessor.swift. The main change is the introduction of a lock (stateLock) to serialize access to internal state, making the class safe to use in concurrent contexts such as audio callbacks and async streams. Several properties and methods are updated to use this lock, and the class is marked as@unchecked Sendableto indicate intended concurrency use.The most important changes include:
Thread Safety and Concurrency Improvements
UnfairLock(stateLock) to serialize all mutations and accesses to internal state, ensuring thread safety whenAudioProcessoris used across concurrent contexts. The class is now marked as@unchecked Sendable.audioEngine,audioSamples,audioEnergy,relativeEnergyWindow,audioBufferCallback,minBufferLength, and internal storage for these) are now accessed and mutated exclusively under the lock, preventing race conditions.Method Updates for Locking
processBuffer,purgeAudioSamples,startRecordingLive,resumeRecordingLive,pauseRecording, andstopRecordingto use the lock when accessing or mutating state, ensuring all state changes are thread-safe.Refactoring and Consistency
Minor API Behavior Changes
stopRecording.These changes make
AudioProcessorrobust for use in multi-threaded and asynchronous scenarios, reducing the risk of data races and undefined behavior.