diff --git a/Sources/BlurtEngine/Pipeline/DictationSession+Pipeline.swift b/Sources/BlurtEngine/Pipeline/DictationSession+Pipeline.swift index 7c6799dc..4cb4f890 100644 --- a/Sources/BlurtEngine/Pipeline/DictationSession+Pipeline.swift +++ b/Sources/BlurtEngine/Pipeline/DictationSession+Pipeline.swift @@ -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 diff --git a/Tests/BlurtEngineTests/CancelRaceTests.swift b/Tests/BlurtEngineTests/CancelRaceTests.swift index a4954bdf..c9d60960 100644 --- a/Tests/BlurtEngineTests/CancelRaceTests.swift +++ b/Tests/BlurtEngineTests/CancelRaceTests.swift @@ -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 @@ -294,11 +305,13 @@ 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( @@ -306,7 +319,7 @@ private actor GatedTranscriber: TranscriberProtocol { ) 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 }