From ae97cfc494175547187f71290b022bb04fc186cc Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Tue, 27 Oct 2026 21:40:11 +0100 Subject: [PATCH] Fix PTT playback deadlock on engine stop The buffer completion callback read `engine.isRunning` on the AVAudioPlayerNode completion queue. `AVAudioPlayerNode.stop()` holds the engine lock while it waits synchronously for that queue, so a main-thread stop (category-escalation rebuild, teardown) deadlocked and the watchdog killed the app (0x8BADF00D). Seen on device when opening the video camera while a live stream's audio was playing. Classify completions on a private serial queue instead, so the node's completion queue never touches the engine. (cherry picked from commit bf1e1e70b290e4c7701002e69a5dbe49349f8b82) --- bitchat/Features/voice/PTTBurstPlayer.swift | 43 +++++++++++++-------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/bitchat/Features/voice/PTTBurstPlayer.swift b/bitchat/Features/voice/PTTBurstPlayer.swift index 272906f4..a3225ff5 100644 --- a/bitchat/Features/voice/PTTBurstPlayer.swift +++ b/bitchat/Features/voice/PTTBurstPlayer.swift @@ -85,26 +85,35 @@ private final class SystemPTTPlaybackEngine: PTTPlaybackEngine { } let scheduledEngine = engine node.scheduleBuffer(buffer, completionCallbackType: callbackType) { [weak scheduledEngine] callbackType in - // The API invokes this callback when the player is stopped as - // well. A configuration change can stop the engine before its - // notification reaches MainActor, so do not misclassify that - // flushed tail as audible playback. - guard scheduledEngine?.isRunning == true else { - completionHandler(.playbackStopped) - return - } - switch callbackType { - case .dataConsumed: - completionHandler(.dataConsumed) - case .dataRendered: - completionHandler(.dataConsumed) - case .dataPlayedBack: - completionHandler(.dataPlayedBack) - @unknown default: - completionHandler(.playbackStopped) + // Never touch the engine on the node's completion queue: + // `AVAudioPlayerNode.stop()` holds the engine lock while it waits + // synchronously for that queue, so `isRunning` here deadlocks a + // main-thread stop (category-escalation rebuild, teardown). + Self.completionClassifier.async { + // The API invokes this callback when the player is stopped as + // well. A configuration change can stop the engine before its + // notification reaches MainActor, so do not misclassify that + // flushed tail as audible playback. + guard scheduledEngine?.isRunning == true else { + completionHandler(.playbackStopped) + return + } + switch callbackType { + case .dataConsumed: + completionHandler(.dataConsumed) + case .dataRendered: + completionHandler(.dataConsumed) + case .dataPlayedBack: + completionHandler(.dataPlayedBack) + @unknown default: + completionHandler(.playbackStopped) + } } } } + + /// Serial so completions keep their scheduling order. + nonisolated private static let completionClassifier = DispatchQueue(label: "chat.bitchat.ptt.playback-completion") } /// Completion callbacks arrive off the main actor, while engine rebuilds are