Repository navigation
Fix race conditions in URLSession streaming and unaligned PCM reads - #359
Conversation
URLSession.stream attached its delegate inside the continuation, so a task cancelled before the operation ran (onCancel fires first) cancelled a delegate-less, never-resumed data task: no completion arrived and the wait for the response head never ended, wedging the read. Check cancellation up front, attach the delegate before anything can cancel the task, and let onCancel settle the head itself. The delegate now holds the head's outcome until the wait arms, so the continuation resumes exactly once whichever side arrives first. PCMLevel bound Data bytes to Int16, but URLSession blocks and chunks with a carried byte prepended needn't be 2-byte aligned. Load samples unaligned instead; same fix for the iPhone's UtteranceFeed meter and silence check. Fixes #339 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DS5B1SDi9QWs3nLoY71cRM
The URLSession test cancels before stream() and so only reaches the up-front check; these exercise the race the delegate fix closes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DS5B1SDi9QWs3nLoY71cRM
|
This looks like runner load rather than this PR: the job sits close to its limit on every PR. On AssemblyAI/blurt#375 (passing, same window) I've used the one re-run I'll do. Options: re-run once the runners are quieter, or raise Generated by Claude Code |
Fixes #339
What & why
This change fixes two related concurrency issues:
URLSession streaming race condition: The
SpeechStreamDelegatehad a race where a task cancelled beforeresume()was called would never settle the continuation waiting for the HTTP response head, causing the caller to hang indefinitely. The fix introduces anarm()method that decouples settling the outcome from waiting on it, allowing cancellations to be recorded before the wait begins. The delegate now checks for early cancellation before resuming the task.Unaligned PCM sample reads:
PCMLevel.levels()andUtteranceFeed.level()usedbindMemory(to: Int16.self)to read PCM samples, which assumes 2-byte alignment. However, URLSession can deliver data blocks at arbitrary offsets, and slicedDataobjects needn't start on anInt16boundary. The fix usesloadUnaligned(fromByteOffset:as:)to safely read samples from unaligned memory.Both issues are subtle but real: the first causes hangs on cancellation, the second causes crashes or incorrect audio levels on unaligned reads.
How it was tested
Added three new tests in
SpeechTransportTests:urlSessionCancelledBeforeStart: Verifies a cancelled task throws immediately and never sends the requestdelegateCancelledBeforeArm: Confirms early cancellation is delivered when the wait arrivesdelegateSettlesOnce: Ensures the continuation is resumed exactly once despite multiple settle attemptsAdded
unalignedSamplestest inPCMLevelTeststo verify PCM level calculation works with unaligned dataExisting
urlSessionCancellationtest continues to passscripts/check.shpasses (or CI will, if I'm not on a Mac)I read AGENTS.md and this doesn't reintroduce anything deliberately removed
Docs updated if behavior changed
🤖 Generated with Claude Code
https://claude.ai/code/session_01DS5B1SDi9QWs3nLoY71cRM