A free, open source, and extensible speech-to-text application that works completely offline.
fix: cancel stalled output processing (#1614)
diligentpigf79a907fc0d3
diff --git a/src-tauri/src/actions.rs b/src-tauri/src/actions.rsindex 5d598b58..0a428b5c 100644--- a/src-tauri/src/actions.rs+++ b/src-tauri/src/actions.rs@@ -18,11 +18,14 @@ use ferrous_opencc::{config::BuiltinConfig, OpenCC};use log::{debug, error, warn};use once_cell::sync::Lazy;use std::collections::HashMap;+use std::future::Future;use std::sync::Arc;-use std::time::Instant;+use std::time::{Duration, Instant};use tauri::Manager;use tauri::{AppHandle, Emitter};+const CANCELLATION_POLL_INTERVAL: Duration = Duration::from_millis(25);+#[derive(Clone, serde::Serialize)]struct RecordingErrorEvent {error_type: String,@@ -74,6 +77,26 @@ fn is_blank_transcription(transcription: &str) -> bool {transcription.trim().is_empty()}+async fn complete_unless_cancelled<F, C>(operation: F, is_cancelled: C) -> Option<F::Output>+where+ F: Future,+ C: Fn() -> bool,+{+ tokio::pin!(operation);++ loop {+ if is_cancelled() {+ return None;+ }++ if let Ok(result) =+ tokio::time::timeout(CANCELLATION_POLL_INTERVAL, operation.as_mut()).await+ {+ return Some(result);+ }+ }+}+fn should_use_streaming_overlay(style: OverlayStyle, is_streaming: bool) -> bool {style == OverlayStyle::Live && is_streaming}@@ -727,9 +750,17 @@ impl ShortcutAction for TranscribeAction {show_processing_overlay(&ah);}}- let processed =- process_transcription_output(&ah, &transcription, post_process)- .await;+ let Some(processed) = complete_unless_cancelled(+ process_transcription_output(&ah, &transcription, post_process),+ || rm.was_cancelled_since(cancel_generation),+ )+ .await+ else {+ debug!("Transcription operation cancelled during output handling");+ utils::hide_recording_overlay(&ah);+ change_tray_icon(&ah, TrayIconState::Idle);+ return;+ };if rm.was_cancelled_since(cancel_generation) {debug!("Transcription operation cancelled before paste");@@ -896,8 +927,13 @@ pub static ACTION_MAP: Lazy<HashMap<String, Arc<dyn ShortcutAction>>> = Lazy::ne#[cfg(test)]mod tests {- use super::{is_blank_transcription, should_use_streaming_overlay};+ use super::{complete_unless_cancelled, is_blank_transcription, should_use_streaming_overlay};use crate::settings::OverlayStyle;+ use std::future;+ use std::sync::atomic::{AtomicBool, Ordering};+ use std::sync::Arc;+ use std::thread;+ use std::time::Duration;#[test]fn blank_transcription_is_detected() {@@ -912,6 +948,34 @@ mod tests {assert!(!is_blank_transcription(" hello "));}+ #[test]+ fn completed_operation_returns_its_output() {+ let result = tauri::async_runtime::block_on(complete_unless_cancelled(+ future::ready("done"),+ || false,+ ));++ assert_eq!(result, Some("done"));+ }++ #[test]+ fn pending_operation_stops_after_cancellation() {+ let cancelled = Arc::new(AtomicBool::new(false));+ let cancelled_for_thread = Arc::clone(&cancelled);+ let cancel_thread = thread::spawn(move || {+ thread::sleep(Duration::from_millis(10));+ cancelled_for_thread.store(true, Ordering::Release);+ });++ let result = tauri::async_runtime::block_on(complete_unless_cancelled(+ future::pending::<()>(),+ || cancelled.load(Ordering::Acquire),+ ));++ cancel_thread.join().unwrap();+ assert_eq!(result, None);+ }+#[test]fn live_overlay_uses_streaming_states_only_for_streaming_models() {assert!(should_use_streaming_overlay(OverlayStyle::Live, true));