Skip to content

The IDE you know, rebuilt in Rust. IntelliJ IDEA–style keymap, tool windows and search-everywhere in a fast native app: editor, code insight, debugger, Git, terminal, database and HTTP clients for 60+ languages.

fix(clipboard): read the macOS pasteboard on the main thread (#629)

l0ng-ai52cb5926928f
diff --git a/crates/ide-clipboard/src/lib.rs b/crates/ide-clipboard/src/lib.rs
index a682260c3..8c6671297 100644
--- a/crates/ide-clipboard/src/lib.rs
+++ b/crates/ide-clipboard/src/lib.rs
@@ -12,6 +12,9 @@
//! macOS keeps one `public.file-url` item per file on the general
//! `NSPasteboard`; `NSURL` reads and writes them, so Finder's copies paste here
//! and ours paste in Finder.
+//! On macOS, [`files`], [`has_files`] and [`set_files`] must run on the main
+//! thread: AppKit and WebKit also access the general pasteboard there, and its
+//! mutable type cache cannot be protected by a mutex in this crate.
//!
//! Other platforms (`text/uri-list` on X11) are not wired up: [`files`] is
//! empty and [`set_files`] fails there.
@@ -265,9 +268,9 @@ mod imp {
#[cfg(target_os = "macos")]
mod imp {
- use objc2::ClassType;
use objc2::rc::{Retained, autoreleasepool};
use objc2::runtime::{AnyClass, AnyObject, ProtocolObject};
+ use objc2::{ClassType, MainThreadMarker};
use objc2_app_kit::{
NSPasteboard, NSPasteboardReadingOptionKey, NSPasteboardTypeString, NSPasteboardURLReadingFileURLsOnlyKey,
NSPasteboardWriting,
@@ -280,6 +283,7 @@ mod imp {
fn read_file_urls<R>(
f: impl FnOnce(&NSPasteboard, &NSArray<AnyClass>, &NSDictionary<NSPasteboardReadingOptionKey, AnyObject>) -> R,
) -> R {
+ debug_assert!(MainThreadMarker::new().is_some(), "NSPasteboard access requires the main thread");
autoreleasepool(|_| {
let board = NSPasteboard::generalPasteboard();
let classes = NSArray::from_slice(&[NSURL::class()]);
@@ -320,6 +324,7 @@ mod imp {
}
pub fn set_files(paths: &[PathBuf]) -> bool {
+ debug_assert!(MainThreadMarker::new().is_some(), "NSPasteboard access requires the main thread");
autoreleasepool(|_| {
// Folders get a directory URL (ending in `/`), as Finder writes them.
let urls: Vec<Retained<NSURL>> = paths
diff --git a/src-tauri/src/commands/clipboard.rs b/src-tauri/src/commands/clipboard.rs
index 813ac40b9..a12cfb306 100644
--- a/src-tauri/src/commands/clipboard.rs
+++ b/src-tauri/src/commands/clipboard.rs
@@ -1,20 +1,25 @@
//! The OS clipboard's file list, so the project view and Explorer exchange files.
+#[cfg(not(target_os = "macos"))]
use crate::commands::doc::blocking;
+#[cfg(target_os = "macos")]
+use ide_core::IdeError;
use ide_core::IdeResult;
use std::path::{Path, PathBuf};
+use tauri::AppHandle;
/// Absolute paths on the clipboard, empty when it holds no file list.
#[tauri::command]
#[specta::specta]
-pub async fn clipboard_file_list() -> IdeResult<Vec<String>> {
- blocking(|| Ok(ide_clipboard::files()?.iter().map(|p| slash(p)).collect())).await
+pub async fn clipboard_file_list(app: AppHandle) -> IdeResult<Vec<String>> {
+ with_clipboard(app, || Ok(ide_clipboard::files()?.iter().map(|p| slash(p)).collect())).await
}
/// Whether the clipboard holds files: cheap, for the Paste item's enabled state.
#[tauri::command]
#[specta::specta]
pub fn clipboard_has_files() -> bool {
+ // Synchronous Tauri commands run on the main thread, as macOS requires.
ide_clipboard::has_files()
}
@@ -22,8 +27,30 @@ pub fn clipboard_has_files() -> bool {
/// false when the clipboard stayed busy; the caller keeps working either way.
#[tauri::command]
#[specta::specta]
-pub async fn clipboard_set_file_list(paths: Vec<PathBuf>) -> IdeResult<bool> {
- blocking(move || Ok(ide_clipboard::set_files(&paths))).await
+pub async fn clipboard_set_file_list(app: AppHandle, paths: Vec<PathBuf>) -> IdeResult<bool> {
+ with_clipboard(app, move || Ok(ide_clipboard::set_files(&paths))).await
+}
+
+/// AppKit and WebKit share the general pasteboard, so a worker-side mutex cannot
+/// protect its type cache. Await the main thread without blocking a command thread.
+async fn with_clipboard<T: Send + 'static>(
+ app: AppHandle,
+ f: impl FnOnce() -> IdeResult<T> + Send + 'static,
+) -> IdeResult<T> {
+ #[cfg(target_os = "macos")]
+ {
+ let (tx, rx) = tokio::sync::oneshot::channel();
+ app.run_on_main_thread(move || {
+ let _ = tx.send(f());
+ })
+ .map_err(|error| IdeError::internal(format!("Cannot dispatch clipboard access to the main thread: {error}")))?;
+ rx.await.map_err(|error| IdeError::internal(format!("Clipboard main-thread task was dropped: {error}")))?
+ }
+ #[cfg(not(target_os = "macos"))]
+ {
+ let _ = app;
+ blocking(f).await
+ }
}
/// Paths cross the IPC boundary `/`-separated, as every other backend path does.