Search code examples
deadlockaudiokit

Deadlock in AudioKit after connecting/disonnecting audio hardware


I'm using AudioKit (4.11.2) in my app to replay audio files.

When try to start a player after connecting/disconnecting audio hardware, there is often a deadlock in main thread. The same also happens when loading or unloading a player after connecting/disconnecting audio hardware.

Especially, when the connected/disconnected device is an Apple AirPod Pro headset, then it's reproducible by 100%.

See image with call stack on deadlock.

Image with call stack of deadlocks

I know that AudioKit 4.11.2 is not the latest, but it is the latest 4.x release. I cannot yet switch to 5.x due to the breaking change of missing AKPlayer class.

Is there any known issue with deadlocks after connecting/disconnecting audio hardware?

I already switched off AKSettings.enableCategoryChangeHandling and AKSettings.enableRouteChangeHandling, but that didn't help.

P.S: In the mean time I migrated to AudioKit 5.3.2 and use the class AudioPlayer instead of AKPlayer and the issue is still the same.


Solution

  • After many hours and hours of debugging I found that it must be an Apple bug.

    Reading lastRenderTime (AKManager.engine.outputNode.lastRenderTime on AudioKit 4 or avEngine.outputNode.lastRenderTime on an instance of AudioEngine on AudioKit 5) causes the problem.

    As long as I never read lastRenderTime, I can connect/disconnect the AirPod Pro headset and play the audio file without any problems.

    Once I read lastRenderTime, after next connect/disconnect of the AirPod Pro headset the main thread freezes when trying to start the player.

    (lastRenderTime on AVAudioNode is an Apple function, not an AudioKit function.)