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(meeting): cancel queued model preparation promptly (#1099)

Kirolos Esmat1b3cba250bff
diff --git a/.github/screenshots/meeting-preparation-cancellation-tests.png b/.github/screenshots/meeting-preparation-cancellation-tests.png
new file mode 100644
index 00000000..e676edb7
Binary files /dev/null and b/.github/screenshots/meeting-preparation-cancellation-tests.png differ
diff --git a/Sources/Fluid/Services/Meeting/LiveTranscription/MeetingModelPreparationQueue.swift b/Sources/Fluid/Services/Meeting/LiveTranscription/MeetingModelPreparationQueue.swift
index 71e70c05..569021b4 100644
--- a/Sources/Fluid/Services/Meeting/LiveTranscription/MeetingModelPreparationQueue.swift
+++ b/Sources/Fluid/Services/Meeting/LiveTranscription/MeetingModelPreparationQueue.swift
@@ -1,34 +1,60 @@
import Foundation
-/// Serializes model preparation across suspension points, not just actor entry.
-/// Separate recognizers retain independent decoder state but never write the cache concurrently.
+/// Separate recognizers retain independent decoder state but must not write the model cache concurrently.
actor MeetingModelPreparationQueue {
static let shared = MeetingModelPreparationQueue()
- private var tail: Task<Void, Error>?
- private var tailID: UUID?
+ private struct Waiter {
+ let id: UUID
+ let continuation: CheckedContinuation<Void, Error>
+ }
+ private var isPreparing = false
+ private var waiters: [Waiter] = []
+
+ #if DEBUG
+ /// Allows regression tests to confirm admission before cancelling a waiter.
+ var queuedPreparationCount: Int { self.waiters.count }
+ #endif
func run(_ operation: @escaping @Sendable () async throws -> Void) async throws {
+ try await self.acquire()
+ defer { self.release() }
+ try Task.checkCancellation()
+ try await operation()
try Task.checkCancellation()
- let previous = self.tail
+ }
+
+ private func acquire() async throws {
let id = UUID()
- let task = Task {
- _ = await previous?.result
- try Task.checkCancellation()
- try await operation()
- }
- self.tail = task
- self.tailID = id
- defer {
- if self.tailID == id {
- self.tail = nil
- self.tailID = nil
- }
- }
try await withTaskCancellationHandler {
- try await task.value
- try Task.checkCancellation()
+ try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Error>) in
+ self.enqueue(id: id, continuation: continuation)
+ }
} onCancel: {
- task.cancel()
+ Task { await self.cancelWaiter(id) }
+ }
+ }
+
+ private func enqueue(id: UUID, continuation: CheckedContinuation<Void, Error>) {
+ if Task.isCancelled {
+ continuation.resume(throwing: CancellationError())
+ } else if !self.isPreparing {
+ self.isPreparing = true
+ continuation.resume()
+ } else {
+ self.waiters.append(Waiter(id: id, continuation: continuation))
+ }
+ }
+
+ private func cancelWaiter(_ id: UUID) {
+ guard let index = self.waiters.firstIndex(where: { $0.id == id }) else { return }
+ self.waiters.remove(at: index).continuation.resume(throwing: CancellationError())
+ }
+
+ private func release() {
+ if self.waiters.isEmpty {
+ self.isPreparing = false
+ } else {
+ self.waiters.removeFirst().continuation.resume()
}
}
}
diff --git a/Tests/FluidDictationIntegrationTests/MeetingLiveTranscriptionTests.swift b/Tests/FluidDictationIntegrationTests/MeetingLiveTranscriptionTests.swift
index 343c5968..870c2c3c 100644
--- a/Tests/FluidDictationIntegrationTests/MeetingLiveTranscriptionTests.swift
+++ b/Tests/FluidDictationIntegrationTests/MeetingLiveTranscriptionTests.swift
@@ -35,6 +35,67 @@ final class MeetingModelPreparationQueueTests: XCTestCase {
XCTAssertEqual(completed, 20)
}
+ private actor PreparationGate {
+ private var continuation: CheckedContinuation<Void, Never>?
+ private var isOpen = false
+ func wait() async {
+ if self.isOpen { return }
+ await withCheckedContinuation { self.continuation = $0 }
+ }
+ func open() {
+ self.isOpen = true
+ self.continuation?.resume()
+ self.continuation = nil
+ }
+ }
+
+ func testCancelledQueuedPreparationReturnsBeforeActivePreparationFinishes() async throws {
+ let queue = MeetingModelPreparationQueue()
+ let gate = PreparationGate()
+ let active = expectation(description: "active preparation holds the cache")
+ let cancelled = expectation(description: "cancelled waiter returns while cache is occupied")
+ let waiting = expectation(description: "second preparation is admitted to the queue")
+ let first = Task {
+ try await queue.run {
+ active.fulfill()
+ await gate.wait()
+ }
+ }
+ await fulfillment(of: [active], timeout: 2)
+ let second = Task {
+ do {
+ try await queue.run { XCTFail("Cancelled preparation must not load a model") }
+ XCTFail("Expected cancellation")
+ } catch is CancellationError {
+ cancelled.fulfill()
+ } catch {
+ XCTFail("Unexpected error: \(error)")
+ }
+ }
+ let admission = Task {
+ while !Task.isCancelled {
+ if await queue.queuedPreparationCount == 1 {
+ waiting.fulfill()
+ return
+ }
+ await Task.yield()
+ }
+ }
+ await fulfillment(of: [waiting], timeout: 2)
+ admission.cancel()
+ let queuedCount = await queue.queuedPreparationCount
+ XCTAssertEqual(queuedCount, 1)
+ second.cancel()
+ await fulfillment(of: [cancelled], timeout: 2)
+ await gate.open()
+ _ = await first.result
+ _ = await second.result
+ let counter = Counter()
+ try await queue.run { await counter.enter(); await counter.leave() }
+ let completed = await counter.completed
+ XCTAssertEqual(completed, 1)
+ }
+
func testFailureAndCancellationDoNotPoisonNextPreparation() async throws {
let queue = MeetingModelPreparationQueue()
let counter = Counter()