Make stopRecording() async to avoid reading incomplete WAV#585
Open
eherrerosj wants to merge 2 commits into
Open
Make stopRecording() async to avoid reading incomplete WAV#585eherrerosj wants to merge 2 commits into
eherrerosj wants to merge 2 commits into
Conversation
The old fire-and-forget dispatch to the audio queue meant callers could read the WAV file before CoreAudioRecorder finished closing it. Use withCheckedContinuation to wait for the stop to complete on the serial hardware queue before returning. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
2 issues found across 2 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="VoiceInk/Recorder.swift">
<violation number="1" location="VoiceInk/Recorder.swift:167">
P1: `stopRecording()` became reentrancy-unsafe: it awaits before clearing shared state, so interleaved `startRecording()` can be overwritten by stale cleanup.</violation>
</file>
<file name="VoiceInk/Whisper/VoiceInkEngine.swift">
<violation number="1" location="VoiceInk/Whisper/VoiceInkEngine.swift:144">
P1: Awaiting stop before clearing `recordedFile` creates a re-entrancy race that can wipe a newer recording URL during rapid cancel/restart.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
Clear shared state (recorder, onAudioChunk) before the await in stopRecording() so a concurrent startRecording() won't have its new state overwritten by stale cleanup when the continuation resumes. In VoiceInkEngine, capture recordedFile before awaiting stop and only nil it out if it hasn't been replaced by a newer recording. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
1 issue found across 2 files (changes from recent commits).
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="VoiceInk/Recorder.swift">
<violation number="1" location="VoiceInk/Recorder.swift:166">
P1: `stopRecording()` applies unconditional post-await cleanup that can run after a new recording starts, causing incorrect recording state and media restoration mid-recording.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
| audioSetupQueue.async { | ||
| currentRecorder?.stopRecording() | ||
| } | ||
| recorder = nil |
Contributor
There was a problem hiding this comment.
P1: stopRecording() applies unconditional post-await cleanup that can run after a new recording starts, causing incorrect recording state and media restoration mid-recording.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At VoiceInk/Recorder.swift, line 166:
<comment>`stopRecording()` applies unconditional post-await cleanup that can run after a new recording starts, causing incorrect recording state and media restoration mid-recording.</comment>
<file context>
@@ -160,9 +160,14 @@ class Recorder: NSObject, ObservableObject {
+ // Capture and clear shared state before awaiting so a concurrent
+ // startRecording() won't have its new state overwritten when we resume.
let currentRecorder = self.recorder
+ recorder = nil
+ onAudioChunk = nil
+
</file context>
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
Recorder.stopRecording()was fire-and-forget: it dispatched the stop to a serial audio queue and returned right awayCoreAudioRecorderhad finished closing itwithCheckedContinuationto actually wait for the stop to finishTest plan
🤖 Generated with Claude Code
Summary by cubic
Make
Recorder.stopRecording()async and wait for the hardware queue to finish so WAV files are fully closed before reading. Also fixes race conditions during cancel and rapid start/stop.Bug Fixes
stopRecording()async withwithCheckedContinuationand wait onaudioSetupQueue; capture and clearrecorder/onAudioChunkbefore awaiting to avoid reentrancy and stale cleanup.awaitthe stop inRecorderandVoiceInkEngine; capturerecordedFilebefore awaiting and only nil it if unchanged to keep cancel paths safe.Migration
await recorder.stopRecording().Written for commit 0478469. Summary will update on new commits.