Skip to content

Fix race conditions in URLSession streaming and unaligned PCM reads - #359

Merged
alexkroman merged 2 commits into
mainfrom
claude/compassionate-brahmagupta-jlpjjg
Nov 25, 2026
Merged

alexkroman merged 2 commits into
mainfrom
claude/compassionate-brahmagupta-jlpjjg

Conversation

@alexkroman

@alexkroman alexkroman commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #339

What & why

This change fixes two related concurrency issues:

  1. URLSession streaming race condition: The SpeechStreamDelegate had a race where a task cancelled before resume() was called would never settle the continuation waiting for the HTTP response head, causing the caller to hang indefinitely. The fix introduces an arm() 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.

  2. Unaligned PCM sample reads: PCMLevel.levels() and UtteranceFeed.level() used bindMemory(to: Int16.self) to read PCM samples, which assumes 2-byte alignment. However, URLSession can deliver data blocks at arbitrary offsets, and sliced Data objects needn't start on an Int16 boundary. The fix uses loadUnaligned(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 request
    • delegateCancelledBeforeArm: Confirms early cancellation is delivered when the wait arrives
    • delegateSettlesOnce: Ensures the continuation is resumed exactly once despite multiple settle attempts
  • Added unalignedSamples test in PCMLevelTests to verify PCM level calculation works with unaligned data

  • Existing urlSessionCancellation test continues to pass

  • scripts/check.sh passes (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

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
@alexkroman
alexkroman enabled auto-merge November 24, 2026 22:50
@alexkroman
alexkroman added this pull request to the merge queue Nov 24, 2026

Copy link
Copy Markdown
Collaborator Author

gate is red because ios-build hit its 30-minute timeout-minutes twice (first run and one re-run), not because anything failed. Both times every iPhone gate that finished passed — ios-test 365/365, BlurtiOSCore coverage 96.1% — and the job was cancelled mid swiftlint analyze / periphery.

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) swiftlint analyze alone took ~9.5 min and the whole job 22 min; here the test phase took ~18 min and analyze 12+ min, with ~10 PRs' macOS jobs running at once. The iPhone-side diff is a few lines in UtteranceFeed (ListeningSource.swift), nothing that would change build or analyze time.

I've used the one re-run I'll do. Options: re-run once the runners are quieter, or raise ios-build's timeout-minutes in .github/workflows/check.yml (a separate change).


Generated by Claude Code

Merged via the queue into main with commit ea41518 Nov 25, 2026
17 of 21 checks passed
@alexkroman
alexkroman deleted the claude/compassionate-brahmagupta-jlpjjg branch November 25, 2026 00:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TTS Read Aloud: URLSession.stream leaks CheckedContinuation on early cancel, and PCMLevel.levels calls bindMemory on unaligned Data

2 participants