Skip to content

Stop a route change from deadlocking the main thread against the engine's queue - #65

Open
poznik wants to merge 5 commits into
gsamat:masterfrom
poznik:fix/route-change-deadlock
Open

poznik wants to merge 5 commits into
gsamat:masterfrom
poznik:fix/route-change-deadlock

Conversation

@poznik

@poznik poznik commented Nov 11, 2026

Copy link
Copy Markdown

Fixes #63.

The deadlock

AVAudioEngine posts AVAudioEngineConfigurationChange synchronously from its own serial queue. MicRecorder observed it with queue: .main, so the post waited for the main thread. The main thread, rebuilding capture for the same route change, was waiting for that queue inside -[AVAudioEngine dealloc] (engine.stop() and removeTap(onBus:) wait for it too). The sample of the hung process is in #63.

What changes

  • The observer is registered with queue: nil. On the poster's thread it only notes which engine posted, as an ObjectIdentifier, and hands the rest to the main queue asynchronously (MicRecorder.observeConfigurationChanges). Everything that acts on the change still runs on the main thread.
  • The engine being replaced is released off the main thread (MicRecorder.Retiring). The caller waits for that, so teardown still precedes setup, but for no more than two seconds; past that it logs a warning and carries on.
  • docs/pitfalls.md gets a section on the deadlock, a section on Voice Isolation (below), and an entry under "What has never been verified".
  • A separate small commit lets the default-input listener build with Swift 6.2 (Xcode 26.3): the nested main-queue closure captures self weakly itself. Happy to drop it from this PR if you would rather not carry it.

Tests

Four tests in MicRestartTests. The first reproduces the shape of the deadlock without a device: the main thread waits for a queue while that queue posts the notification. Against the old queue: .main observer it fails (the wait times out); with the change it passes.

On my machine (macOS 15.8, Xcode 26.3) swift test runs 771 tests with two failures that are also there without this change — MeetingLanguagesTests ("The rest follow by name…") and StatusWindowLayoutTests ("Every row the status window can show…") — and Tests/scripts fails test_app_icon the same way. landing/tests/check.py passes. I have not run any of it on macOS 26.

On a real device

MacBook Air M3, macOS 15.8, a call in another app, AirPods Max taken off mid-recording, with a build of this branch:

  • amanu stays responsive, records the restart in mic_restarts, and stops when asked. On 0.6.5 in the same situation it hung.

One thing I got wrong in #63: the coreaudiod loop is not caused by the deadlock. It goes with Voice Isolation chosen as the microphone mode for amanu itself in Control Center. With that mode on, coreaudiod spun in three recordings out of three (two on 0.6.5, one on this branch), and while it spins the rebuilt engine delivers no buffers. With amanu on Standard and the call app left on Voice Isolation, the same route change was uneventful twice, once on stock 0.6.5 and once on this branch: capture moved to the built-in microphone after about 0.9 s and coreaudiod stayed under 20% CPU. So on 0.6.5 the deadlock only shows up once that loop is running; the fix here is what keeps the app alive when it does. Five recordings on one Mac is all the evidence there is, and I do not know the mechanism.

Not covered here: after the switch the built-in microphone (three discrete channels) records silence, which is #57.

🤖 Generated with Claude Code

poznik and others added 5 commits November 11, 2026 08:48
Xcode 26.3 ships Swift 6.2.4, which rejects the nested main-queue closure
with "sending 'self' risks causing data races": the outer block's weak
capture is a mutable variable there. Capturing weakly again in the inner
closure says the same thing in a form both compilers accept.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…ne's queue

AVAudioEngine posts its configuration change synchronously from its own
queue. The observer was registered on the main operation queue, so the post
waited for the main thread — which, rebuilding capture for the same route
change, was waiting for that queue inside the old engine's dealloc. AirPods
coming off mid-recording left the app hung, the mic track cut short and
coreaudiod spinning (gsamat#63).

The observer now runs on the poster's thread and only hands the change to the
main queue. The engine being replaced is released off the main thread, with a
wait that gives up after two seconds, so teardown still precedes setup without
tying the app to Core Audio.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…dphones

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…t points at

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Amanu's main thread deadlocks during the restart

1 participant