Fix handle 58 second timeout with full engine restart to prevent teleprompter freeze#59
Open
G10Bi wants to merge 4 commits into
Conversation
## Problem
After ~58 seconds of continuous speech recognition, `SFSpeechRecognizer` hits
its internal timeout (error 1110 / 216) and the teleprompter freezes permanently.
The original code called `restartTask()` — a soft restart that reuses the existing
`AVAudioEngine`. But macOS also **pauses the microphone hardware** during these
timeouts. The engine's internal state becomes unrecoverable, and no amount of
task-level restart can bring it back. The only way out was a manual stop/start.
## Root cause
`AVAudioEngine` caches the audio device format internally. After the system pauses
the mic (triggered by the 58s speech recognition limit), the cached format is
invalidated but the engine doesn't detect this. A soft restart (`restartTask()`)
only recreates the `SFSpeechRecognitionTask`, not the engine. The new task receives
silence or corrupt buffers and fails silently.
## Solution
Added `forceRestartAfterTimeout()` — a method that performs a **complete teardown
and recreation** of the entire audio stack:
```swift
private func forceRestartAfterTimeout() {
// 1. Save state
let savedOffset = recognizedCharCount
let savedSource = sourceText
// 2. Full cleanup (task + engine + tap)
cleanupRecognition()
// 3. Fresh engine — picks up current hardware format
audioEngine = AVAudioEngine()
// 4. Restore state
sourceText = savedSource
recognizedCharCount = savedOffset
matchStartOffset = savedOffset
// 5. Restart after short delay for resource release
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
self.beginRecognition()
}
}
…ine-restart-to-prevent-teleprompter-freeze
Little error...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
After ~58 seconds of continuous speech recognition,
SFSpeechRecognizerhitsits internal timeout (error 1110 / 216) and the teleprompter freezes permanently.
The original code called
restartTask()— a soft restart that reuses the existingAVAudioEngine. But macOS also pauses the microphone hardware during thesetimeouts. The engine's internal state becomes unrecoverable, and no amount of
task-level restart can bring it back. The only way out was a manual stop/start.
Root cause
AVAudioEnginecaches the audio device format internally. After the system pausesthe mic (triggered by the 58s speech recognition limit), the cached format is
invalidated but the engine doesn't detect this. A soft restart (
restartTask())only recreates the
SFSpeechRecognitionTask, not the engine. The new task receivessilence or corrupt buffers and fails silently.
Solution
Added
forceRestartAfterTimeout()— a method that performs a complete teardownand recreation of the entire audio stack: