When the keyboard can't dictate itself (keyboardNeedsHex) its Hex key opens
Hex, which records with AVAudioRecorder. If the microphone fails there (no
input in the simulator, or another app holding it), record() returns false
after AudioQueueStart fails, and the recorder stops and disposes its queue
from inside the queue's own callback, asynchronously. beginCapture threw and
freed the recorder right away, while restoreAudioAfterCapture restarted the
keepalive; an input callback the queue had already fetched then read the freed
recorder and Hex crashed (EXC_BAD_ACCESS in objc_loadWeak,
AudioRecorderAQInputCallback, thread AQClient), instead of showing "Hex
couldn't start the microphone".
- RetiredRecorders holds a recorder that failed to start, or that a capture
stopped, for 5 s before it is freed. Nothing reports when the queue's last
callback ran; disposal takes about a millisecond in the simulator log.
- beginCapture stops and retires the recorder before throwing; the capture
adapter retires it on stop (once, so a second stop is a no-op).
- startKeepalive removes its tap and resets the engine when engine.start()
throws, so a failed start leaves nothing half-installed.
The keyboard's own capture (KeyboardLocalDictation / KeyboardWebCapture)
records through WebKit's getUserMedia and gets its callbacks on the main
thread through WKScriptMessageHandler; it has no audio-thread callback or
weak read like this, so it is unchanged.
Tests (DictationControllerTests): a microphone that fails when the keyboard
opens Hex shows the message in the app and the keyboard, leaves no job,
capture or keepalive behind, and the next Hex key records; a recorder end
arriving after teardown changes nothing; a retired AVAudioRecorder outlives its
owner for the hold and is then freed.
Verified on iOS 27.2 simulators on the Mac Studio (no microphone input):
- Keyboard (keyboardNeedsHex) > Hex key in Messages > Hex: before, 2 crashes
in 21 runs with this stack; after, 0 crashes in 20 runs (10 cold launches,
10 warm), Hex alive and showing "Hex couldn't start the microphone. Try
again." each time.
- In-app Dictate, 5 times: the message each time, same process, no crash.
- An uncommitted stress test of SystemAudioManager's failing start (100 x
beginCapture then startKeepalive): crashed the test host 2 of 2 before,
passed 3 of 3 after.
- xcodebuild test: 391 tests, 0 failures (1 skipped).