Skip to content

[BUG] Permanent main-thread deadlock on default audio device change while idle (AVAudioEngine dealloc vs AVAudioEngineConfigurationChange observer) #542

Closed

Description

@davemac

Describe the bug

FluidVoice permanently freezes (spinning beachball on its windows, menu bar item unresponsive, 0% CPU) due to a main-thread deadlock triggered by a default audio device change while the app is idle. The app never recovers; force quit is the only way out.

I captured a sample of the hung process, and every one of the 2,557 snapshots shows the same two threads waiting on each other:

Thread A — main thread. The CoreAudio default-device-change listener (dispatched to main from registerDefaultDeviceChangeListener()) runs scheduleAudioRouteRecovery → retireAudioEngine(reason: "idle_route_change:..."). Inside retireAudioEngine, the final strong reference to the old AVAudioEngine is released on the main thread. -[AVAudioEngine dealloc] then does a synchronous dispatch onto the engine's internal engine serial queue and blocks in __DISPATCH_WAIT_FOR_QUEUE__.

Thread B — the engine's internal engine queue. The same device change is being handled by AVAudioEngineImpl::IOUnitConfigurationChanged(), which posts AVAudioEngineConfigurationChange via NSNotificationCenter. Because ASRService registers that observer with queue: .main (ASRService.swift#L2317-L2322), notification delivery wraps the handler in an NSOperation on the main queue and blocks the posting thread in -[NSOperation waitUntilFinished] until the main thread runs it.

Thread A is waiting for Thread B's queue; Thread B is waiting for Thread A's queue. Circular wait, so the hang is permanent.

Two observations from the source that may help:

  1. retireAudioEngine already tries to avoid exactly this by deferring the release of engineStorage to a background queue (ASRService.swift#L605-L610). The sample shows -[AVAudioEngine dealloc] occurring on the main thread directly under retireAudioEngine regardless, so some other strong reference to the engine is being dropped synchronously inside that function (Swift release/inlined frames don't show in the sample, so I can't say which one from the trace alone).
  2. The AVAudioEngineConfigurationChange observer body only spawns a Task { @MainActor ... }, so registering it with queue: nil (synchronous delivery on the posting thread) looks safe and would remove the waitUntilFinished on the main queue — breaking the cycle from the other side even if a main-thread dealloc slips through again.

Reproduction steps

It is a race between the app's route-recovery teardown and the engine's own configuration-change handling, so it does not reproduce on demand. Conditions when it hit:

  1. FluidVoice running and idle (not recording), "sync audio devices with system" enabled.
  2. The system default input/output device changes (in my case most likely a Bluetooth headset connecting or disconnecting).
  3. If the engine's internal queue is inside IOUnitConfigurationChanged() posting the configuration-change notification at the moment the main thread releases the engine in retireAudioEngine, the app deadlocks.

Expected behavior

The idle route change retires and re-prewarms the audio engine; the app keeps working.

Actual behavior

The app freezes permanently: UI unresponsive, 0% CPU, all samples identical over a 3-second sample window. It stayed in this state indefinitely (hours) until I killed the process. Plain SIGTERM was enough to terminate it; after relaunch everything worked normally.

App Version

1.6.2 (build 13)

macOS Version

macOS 26.5.1 (25F80)

Architecture

Apple Silicon (arm64)

Logs or crash report

Main thread (Thread A), trimmed from sample output — identical in all 2,557 snapshots:

2557 Thread_2049822   DispatchQueue_1: com.apple.main-thread  (serial)
  ...
  2557 _dispatch_call_block_and_release  (in libdispatch.dylib)
    2557 <deduplicated_symbol>  (in FluidVoice)
      2557 partial apply for closure #1 in closure #1 in ASRService.registerDefaultDeviceChangeListener()  (in FluidVoice)
        2557 ASRService.scheduleAudioRouteRecovery(reason:)  (in FluidVoice)
          2557 ASRService.retireAudioEngine(reason:)  (in FluidVoice)
            2557 -[AVAudioEngine dealloc]  (in AVFAudio)
              2557 _dispatch_sync_f_slow  (in libdispatch.dylib)
                2557 __DISPATCH_WAIT_FOR_QUEUE__  (in libdispatch.dylib)
                  2557 _dispatch_event_loop_wait_for_ownership  (in libdispatch.dylib)
                    2557 _dispatch_kq_poll  (in libdispatch.dylib)
                      2557 kevent_id  (in libsystem_kernel.dylib)

Engine's internal queue (Thread B):

2557 Thread_2967515   DispatchQueue_23295: engine  (serial)
  ...
  2557 _dispatch_block_async_invoke2  (in libdispatch.dylib)
    2557 invocation function for block in AVAudioEngineImpl::IOUnitConfigurationChanged()  (in AVFAudio)
      2557 -[NSNotificationCenter postNotificationName:object:userInfo:]  (in Foundation)
        2557 _CFXNotificationPost  (in CoreFoundation)
          2557 -[NSOperation waitUntilFinished]  (in Foundation)
            2557 _pthread_cond_wait  (in libsystem_pthread.dylib)
              2557 __psynch_cvwait  (in libsystem_kernel.dylib)

All other threads were idle (run loops in mach_msg, workers in semaphore_wait/workq_kernreturn). I have the full sample output and can attach it if useful.

Activity

  1. davemac commented on Jul 13, 2026

    @davemac
    Author

    This reproduced again today, roughly a day after the first occurrence, on a fresh instance of the app (same machine, same 1.6.2 build 13). I sampled the hung process before force quitting and the signature is identical down to the frame offsets:

    2543 Thread_9435   DispatchQueue_1: com.apple.main-thread  (serial)
      ...
      ASRService.scheduleAudioRouteRecovery(reason:)  (in FluidVoice) + 508
        ASRService.retireAudioEngine(reason:)  (in FluidVoice) + 1156
          -[AVAudioEngine dealloc]  (in AVFAudio) + 320
            __DISPATCH_WAIT_FOR_QUEUE__  (in libdispatch.dylib)
    
    2543 Thread_1531972   DispatchQueue_14425: engine  (serial)
      ...
      invocation function for block in AVAudioEngineImpl::IOUnitConfigurationChanged()  (in AVFAudio) + 240
        -[NSOperation waitUntilFinished]  (in Foundation)
    

    So this is hitting at least daily in normal use for me, not a rare one-off race. Happy to attach either full sample file if useful.

  2. altic-dev commented on Jul 13, 2026

    @altic-dev
    Owner

    I have never faced it; appreciate you helping with the logs.Let me take a look at it. If not on the next update, I'll push it in the upcoming update.I made some changes to the core audio in the current one; I hope it helps.

  3. davemac commented on Jul 14, 2026

    @davemac
    Author

    One correction to the reproduction conditions in my original report: I listed "sync audio devices with system enabled" as a precondition, implying it could be disabled as a mitigation. It cannot — in v1.6.2 syncAudioDevicesWithSystem is hardcoded to true (SettingsStore.swift#L1669-L1678, "independent mode doesn't work for Bluetooth/aggregate devices"), and the Settings UI only shows the static "Audio devices are synced with macOS System Settings" notice. So the handleDefaultInputChanged/handleDefaultOutputChanged guards can never bail out, every user is on the affected path, and there is no user-side workaround for this deadlock.

    It hit a third time today (third day in a row), and ~/Library/Logs/Fluid/Fluid.log pins the trigger precisely. The final entries before the app froze:

    [11:26:50.566] [INFO] [ASRService] 🎧 New Bluetooth device detected: 'AirPods Pro'. Auto-switching...
    [11:26:50.566] [INFO] [ASRService] Not recording - Bluetooth device will be used on next recording
    [11:26:50.574] [DEBUG] [ASRService] Audio engine retired (idle_route_change:default input changed)
    

    Then nothing for three hours until the relaunch [RUN] banner. On every healthy occurrence of this path the "Audio engine retired" line is immediately followed by a Prepared direct Core Audio input ... prewarm line; here that line never arrives, consistent with the process deadlocking in a device-change handler fired moments later (AirPods flip input and output in quick succession). My log shows this idle route-change path running half a dozen times a day with AirPods and a Studio Display in the mix, which matches the daily hit rate.

  4. RobinHoodO commented on Jul 14, 2026

    @RobinHoodO

    Independently reproduced this today (v1.6.2, macOS 26.5.1, M-series). Same signature exactly — main thread parked in retireAudioEngine → -[AVAudioEngine dealloc] → __DISPATCH_WAIT_FOR_QUEUE__, 0% CPU, triggered by a Bluetooth headset (Shokz OpenComm2) disconnect/reconnect burst while idle. Confirms this isn't a one-off — PR #543 looks like the right fix (moving the final release off any main-thread local via a dedicated holder). Would be great to see this merged; it's been open a week with no reviews.

  5. mikesplain commented on Jul 14, 2026

    @mikesplain

    I'm running into this exact same issue as well. Hoping to see #543 merged soon as well.

    Thanks for an excellent tool @altic-dev! I appreciate the pace in innovation and improvements you've been able to make every day!

  6. Triangulum9r commented on Jul 14, 2026

    @Triangulum9r

    We're also hitting this on our side.

    Environment: FluidVoice 1.6.4 (build 15), macOS 26.5.2 (25F84), Mac Studio (Apple M3 Ultra).

    Today's incident (2026-07-14 ~13:20 PDT): While idle, AirPods Pro reconnected and the app died during the idle route-change teardown. ~/Library/Logs/Fluid/Fluid.log shows the same missing-prewarm pattern described above — the final lines before termination were:

    [13:20:10.863] [INFO] [ASRService] 🎧 New Bluetooth device detected: 'AirPods Pro'. Auto-switching...
    [13:20:10.863] [INFO] [ASRService] Not recording - Bluetooth device will be used on next recording
    [13:20:10.952] [DEBUG] [ASRService] Audio engine retired (idle_route_change:default input changed)
    [CRASH] Fatal signal SIGABRT
    

    On every healthy route change in our logs, Audio engine retired is immediately followed by Prepared direct Core Audio input ...; here that prewarm line never arrives.

    Diagnostic report (~/Library/Logs/DiagnosticReports/FluidVoice-2026-07-14-132016.ips) matches the deadlock signature from the original report:

    • Engine queue: AVAudioEngineImpl::IOUnitConfigurationChanged() → -[NSOperation waitUntilFinished]
    • Background release path: -[AVAudioEngine dealloc] → __DISPATCH_WAIT_FOR_QUEUE__
    • Main thread: inside ASRService.registerEngineConfigurationChangeObserver() when the process aborted

    We see frequent idle route changes throughout the day (Elgato Wave Link mixes, AirPods Max/Pro, iPhone Microphone), so we're squarely on the affected code path. Happy to attach the full log excerpt or .ips if useful.

    Looking forward to #543 landing — thanks for the great tool and the fast iteration.

  7. altic-dev commented on Jul 14, 2026

    @altic-dev
    Owner

    Merging this next. Did not realize its a little wide spread. So it happens when you change devices - i barely do it so never came across this.

    I gotchu y'all

  8. davemac commented on Jul 26, 2026

    @davemac
    Author

    Looks like this issue is still happening for me ...

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions