Skip to content

Fastest and only macOS Dictation app with on-device STT and custom trained AI enhancement model. Windows, iOS testflight! Android, Linux waitlist open! A local Wispr Flow alternative. Follow https://x.com/fluidvoiceapp for more

fix(media): guard against double-resume crash in pauseIfPlaying

guard219b58e7493f6
diff --git a/Sources/Fluid/Services/MediaPlaybackService.swift b/Sources/Fluid/Services/MediaPlaybackService.swift
index d5ae048c..7c12722e 100644
--- a/Sources/Fluid/Services/MediaPlaybackService.swift
+++ b/Sources/Fluid/Services/MediaPlaybackService.swift
@@ -3,6 +3,39 @@ import Foundation
import MediaRemoteAdapter
#endif
+/// Thread-safe guard that ensures a CheckedContinuation is resumed at most once.
+///
+/// MediaRemoteAdapter's `getTrackInfo` callback can fire multiple times under certain
+/// race conditions (e.g., rapid media state changes, system under load). Since
+/// `CheckedContinuation` must be resumed exactly once, a duplicate `resume` call
+/// triggers a fatal `SIGTRAP` (EXC_BREAKPOINT) crash.
+///
+/// This guard serializes access via `NSLock` and silently drops duplicate resumes.
+private final class ContinuationOnceGuard<T: Sendable>: @unchecked Sendable {
+ private var resumed = false
+ private let lock = NSLock()
+
+ /// Attempts to resume the continuation. Returns `true` if this was the first call
+ /// (continuation was actually resumed), `false` if a prior call already resumed it.
+ @discardableResult
+ func resume(_ continuation: CheckedContinuation<T, Never>, returning value: T) -> Bool {
+ lock.lock()
+ let alreadyResumed = resumed
+ if !alreadyResumed { resumed = true }
+ lock.unlock()
+
+ if alreadyResumed {
+ DebugLogger.shared.warning(
+ "ContinuationOnceGuard: Suppressed duplicate resume (MediaRemoteAdapter callback fired more than once)",
+ source: "MediaPlaybackService"
+ )
+ return false
+ }
+ continuation.resume(returning: value)
+ return true
+ }
+}
+
/// Service that wraps MediaRemoteAdapter's MediaController to provide
/// controlled pause/resume functionality during transcription.
///
@@ -25,11 +58,20 @@ final class MediaPlaybackService {
///
/// - Returns: `true` if we successfully paused playback, `false` if nothing was playing
/// or if we couldn't determine playback state.
+ ///
+ /// - Note: Uses `ContinuationOnceGuard` to protect against `MediaRemoteAdapter`
+ /// firing the `getTrackInfo` callback more than once, which would otherwise
+ /// crash with `EXC_BREAKPOINT` (SIGTRAP) due to double-resume of a
+ /// `CheckedContinuation`.
func pauseIfPlaying() async -> Bool {
return await withCheckedContinuation { continuation in
+ // Guard against MediaRemoteAdapter firing the callback multiple times.
+ // A double-resume of CheckedContinuation is a fatal error (SIGTRAP).
+ let once = ContinuationOnceGuard<Bool>()
+
self.mediaController.getTrackInfo { [weak self] trackInfo in
guard let self = self else {
- continuation.resume(returning: false)
+ once.resume(continuation, returning: false)
return
}
@@ -39,7 +81,7 @@ final class MediaPlaybackService {
"MediaPlaybackService: No track info available, nothing to pause",
source: "MediaPlaybackService"
)
- continuation.resume(returning: false)
+ once.resume(continuation, returning: false)
return
}
@@ -73,13 +115,13 @@ final class MediaPlaybackService {
source: "MediaPlaybackService"
)
self.mediaController.pause()
- continuation.resume(returning: true)
+ once.resume(continuation, returning: true)
} else {
DebugLogger.shared.debug(
"MediaPlaybackService: Media is not playing, no action needed",
source: "MediaPlaybackService"
)
- continuation.resume(returning: false)
+ once.resume(continuation, returning: false)
}
}
}