From e8cd1aabca41e4c05105150fb862b4c1836f7419 Mon Sep 17 00:00:00 2001 From: poli0981 <127664709+poli0981@users.noreply.github.com> Date: Sat, 24 Oct 2026 09:04:21 +0700 Subject: [PATCH] fix(desktop): run the file dialogs and their file I/O on the blocking pool SonarCloud's first CI analysis of main, the first one that reads Rust, flagged rust:S7493 in export_text_file: a blocking fs::write inside an async command holds one of the async runtime's worker threads. Waiting for the native Save / Open dialog blocks the same way, for as long as the user takes to choose. Both commands now validate the filter first, then run the dialog and the file I/O through tauri::async_runtime::spawn_blocking. Writing the picked file moves into write_picked, next to read_picked, with a test. The dialogs' blocking API only needs to stay off the main thread, which the blocking pool is, as the runtime's workers were. Co-Authored-By: Claude Opus 5.5 --- src-tauri/src/file_dialog.rs | 72 +++++++++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 17 deletions(-) diff --git a/src-tauri/src/file_dialog.rs b/src-tauri/src/file_dialog.rs index f12e9f9..3670ec4 100644 --- a/src-tauri/src/file_dialog.rs +++ b/src-tauri/src/file_dialog.rs @@ -85,6 +85,9 @@ fn dialog( /// Ask where to save, then write `content` there. Returns the file name that /// was written, or `None` when the user cancelled. +/// +/// Waiting for the dialog and writing the file both block, so they run on the +/// blocking thread pool instead of holding one of the async runtime's workers. #[tauri::command] pub async fn export_text_file( app: AppHandle, @@ -96,15 +99,29 @@ pub async fn export_text_file( title: Option, ) -> Result, String> { let filter = allowed_filter(&extensions)?; - let refs: Vec<&str> = filter.iter().map(String::as_str).collect(); - let picked = dialog(&app, &window, title) - .set_file_name(suggested_file_name(&suggested_name)) - .add_filter(filter_name, &refs) - .blocking_save_file(); - let Some(picked) = picked else { - return Ok(None); - }; - let path = with_allowed_extension(picked_path(picked)?, &filter[0]); + tauri::async_runtime::spawn_blocking(move || { + let refs: Vec<&str> = filter.iter().map(String::as_str).collect(); + let picked = dialog(&app, &window, title) + .set_file_name(suggested_file_name(&suggested_name)) + .add_filter(filter_name, &refs) + .blocking_save_file(); + match picked { + None => Ok(None), + Some(picked) => write_picked(picked, &filter[0], &content), + } + }) + .await + .map_err(|e| e.to_string())? +} + +/// Write `content` where the user chose, ending in an allowed extension, and +/// return the file name that was written. +fn write_picked( + picked: FilePath, + default_ext: &str, + content: &str, +) -> Result, String> { + let path = with_allowed_extension(picked_path(picked)?, default_ext); fs::write(&path, content).map_err(|e| e.to_string())?; Ok(path.file_name().map(|n| n.to_string_lossy().into_owned())) } @@ -141,6 +158,7 @@ fn read_picked(path: &Path, filter: &[String]) -> Result { } /// Ask for a file, then return its name and text. `None` when cancelled. +/// Runs on the blocking thread pool, like `export_text_file`. #[tauri::command] pub async fn import_text_file( app: AppHandle, @@ -150,14 +168,18 @@ pub async fn import_text_file( title: Option, ) -> Result, String> { let filter = allowed_filter(&extensions)?; - let refs: Vec<&str> = filter.iter().map(String::as_str).collect(); - let picked = dialog(&app, &window, title) - .add_filter(filter_name, &refs) - .blocking_pick_file(); - match picked { - None => Ok(None), - Some(picked) => read_picked(&picked_path(picked)?, &filter).map(Some), - } + tauri::async_runtime::spawn_blocking(move || { + let refs: Vec<&str> = filter.iter().map(String::as_str).collect(); + let picked = dialog(&app, &window, title) + .add_filter(filter_name, &refs) + .blocking_pick_file(); + match picked { + None => Ok(None), + Some(picked) => read_picked(&picked_path(picked)?, &filter).map(Some), + } + }) + .await + .map_err(|e| e.to_string())? } #[cfg(test)] @@ -204,6 +226,22 @@ mod tests { ); } + #[test] + fn writing_keeps_the_name_and_adds_an_allowed_extension() { + let dir = std::env::temp_dir().join(format!("ytdescgen-export-{}", std::process::id())); + fs::create_dir_all(&dir).unwrap(); + + let name = write_picked(FilePath::Path(dir.join("backup")), "json", "{}").unwrap(); + assert_eq!(name.as_deref(), Some("backup.json")); + assert_eq!(fs::read_to_string(dir.join("backup.json")).unwrap(), "{}"); + + let name = write_picked(FilePath::Path(dir.join("notes.MD")), "json", "# x").unwrap(); + assert_eq!(name.as_deref(), Some("notes.MD")); + assert_eq!(fs::read_to_string(dir.join("notes.MD")).unwrap(), "# x"); + + let _ = fs::remove_dir_all(&dir); + } + #[test] fn reading_checks_type_size_and_encoding() { let dir = std::env::temp_dir().join(format!("ytdescgen-dialog-{}", std::process::id()));