From d8e4306f1a58d2a63d4e1b30b47f6cbe8c8c4664 Mon Sep 17 00:00:00 2001 From: rudrankriyam Date: Sat, 28 Nov 2026 22:08:04 +0530 Subject: [PATCH 1/2] Reproduce a stalled result stream after microphone restart fails --- .../AuralKitTests/SessionLifecycleTests.swift | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/Tests/AuralKitTests/SessionLifecycleTests.swift b/Tests/AuralKitTests/SessionLifecycleTests.swift index b30fe30..4956ac3 100644 --- a/Tests/AuralKitTests/SessionLifecycleTests.swift +++ b/Tests/AuralKitTests/SessionLifecycleTests.swift @@ -212,6 +212,34 @@ struct SpeechSessionLifecycleTests { #expect(session.recognizerTask == nil) } +#if os(macOS) + @Test("A microphone restart failure closes the result stream") + @MainActor + func microphoneRestartFailureClosesResults() async { + let session = SpeechSession(inputProviderPreference: .legacy) + let (results, resultBuilder) = AsyncThrowingStream.makeStream() + session.continuation = .speech(resultBuilder) + session.streamingMode = .liveMicrophone + session.isAudioStreaming = true + session.setStatus(.transcribing) + + await session.handleEngineConfigurationChange() + + do { + try await awaitResult(timeout: 3) { + for try await _ in results { Issue.record("Expected no results") } + } + Issue.record("Expected the microphone restart error") + } catch SpeechSessionError.invalidAudioDataType { + } catch { + Issue.record(error) + } + #expect(session.status == .idle) + #expect(session.continuation == nil) + await session.cancelTranscribing() + } +#endif + /// Pausing is only meaningful for live capture; during file transcription it must be /// refused (previously, resuming a "paused" file session started the microphone). @Test("Pause is refused during file transcription") From 5a2ccfc92cfd2e1a9c4a9e024acb19b50a3411d6 Mon Sep 17 00:00:00 2001 From: rudrankriyam Date: Sat, 28 Nov 2026 22:09:19 +0530 Subject: [PATCH 2/2] End transcription when microphone capture fails to restart --- Sources/AuralKit/SpeechSession+Audio.swift | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Sources/AuralKit/SpeechSession+Audio.swift b/Sources/AuralKit/SpeechSession+Audio.swift index 4d5e394..15fcb2e 100644 --- a/Sources/AuralKit/SpeechSession+Audio.swift +++ b/Sources/AuralKit/SpeechSession+Audio.swift @@ -191,13 +191,14 @@ extension SpeechSession { } do { - try await reset() + try resetAudioCapture() } catch { if Self.shouldLog(.error) { Self.logger.error( "Failed to reset audio engine after route change: \(error.localizedDescription, privacy: .public)" ) } + await endTranscription(error: error, generation: sessionGeneration) } publishCurrentAudioInputInfo() @@ -279,7 +280,7 @@ extension SpeechSession { #elseif os(macOS) func handleEngineConfigurationChange() async { do { - try await reset() + try resetAudioCapture() } catch { if Self.shouldLog(.error) { let desc = error.localizedDescription @@ -287,6 +288,7 @@ extension SpeechSession { "Failed to reset audio engine after configuration change: \(desc, privacy: .public)" ) } + await endTranscription(error: error, generation: sessionGeneration) } publishCurrentAudioInputInfo() @@ -348,7 +350,7 @@ extension SpeechSession { } #endif - func reset() async throws { + private func resetAudioCapture() throws { if Self.shouldLog(.debug) { Self.logger.debug("Resetting audio engine") }