Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ extension DictationSession {
// .cancelled; the transport then surfaces a cancellation-shaped error
// (URLError(.cancelled) / CancellationError). Leave the claimed phase
// alone rather than repainting the user's cancel as a red failure.
if Task.isCancelled || error is CancellationError { return nil }
if Task.isCancelled || error is CancellationError || (error as? URLError)?.code == .cancelled { return nil }
// The producer refused to close a body below `SyncSTTLimits.minPCMBytes`.
// `performRelease` normally catches that first and idles quietly; when the
// producer gets there first instead, the outcome has to be the same
Expand Down
17 changes: 15 additions & 2 deletions Tests/BlurtEngineTests/CancelRaceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ struct CancelRaceTests {
#expect(await injector.inserted.isEmpty)
}

@Test("transcriber throwing URLError.cancelled without task cancellation is ignored rather than failed")
func transcriberThrowingURLErrorCancelledIsIgnored() async throws {
let stt = GatedTranscriber(text: "Hello world.", throwsCancelled: true)
let (session, injector) = await parkedTranscribing(stt)
await stt.allowToFinish()
await session.awaitPipeline()

#expect(await session.phase != .failed(.sttFailed(underlying: URLError(.cancelled))))
#expect(await injector.inserted.isEmpty)
}

@Test("cancel during injecting discards the transcript and ends .cancelled")
func cancelDuringInjectingDiscards() async throws {
// `expectedCount: 0`: the paste must never be recorded. This is event-driven
Expand Down Expand Up @@ -294,19 +305,21 @@ private actor GatedTranscriber: TranscriberProtocol {
/// `URLError(.cancelled)` — mimicking the real URLSession-backed transcriber,
/// whose in-flight request is torn down by task cancellation.
private let throwsWhenCancelled: Bool
private let throwsCancelled: Bool
private let gate = Gate()

init(text: String, throwsWhenCancelled: Bool = false) {
init(text: String, throwsWhenCancelled: Bool = false, throwsCancelled: Bool = false) {
self.text = text
self.throwsWhenCancelled = throwsWhenCancelled
self.throwsCancelled = throwsCancelled
}

func transcribe(
frames: AsyncStream<Data>, sampleRate: Int, context: TranscriptionContext?
) async throws -> String {
try await drainUntilAbandoned(frames)
await gate.enter()
if throwsWhenCancelled && Task.isCancelled { throw URLError(.cancelled) }
if throwsCancelled || (throwsWhenCancelled && Task.isCancelled) { throw URLError(.cancelled) }
return text
}

Expand Down