Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

fix(cua-driver): lazily initialize macos clipboard (#3025)

injaneityc3687bd8dffb
diff --git a/libs/cua-driver/rust/crates/platform-macos/src/tools/clipboard.rs b/libs/cua-driver/rust/crates/platform-macos/src/tools/clipboard.rs
index 601a620f2d..2f51a2ec8e 100644
--- a/libs/cua-driver/rust/crates/platform-macos/src/tools/clipboard.rs
+++ b/libs/cua-driver/rust/crates/platform-macos/src/tools/clipboard.rs
@@ -1,27 +1,67 @@
-use std::{path::Path, sync::Mutex};
+use std::{
+ any::Any,
+ panic::{catch_unwind, AssertUnwindSafe},
+ path::Path,
+ sync::Mutex,
+};
use clipboard_rs::{common::RustImage, Clipboard, ClipboardContext, ContentFormat};
use cua_driver_core::clipboard::ClipboardBackend;
pub struct MacosClipboard {
- context: Result<Mutex<ClipboardContext>, String>,
+ context: Mutex<Option<ClipboardContext>>,
+ initialize: fn() -> Result<ClipboardContext, String>,
}
impl MacosClipboard {
pub fn new() -> Self {
Self {
- context: ClipboardContext::new()
- .map(Mutex::new)
- .map_err(|error| error.to_string()),
+ context: Mutex::new(None),
+ initialize: initialize_context,
}
}
- fn context(&self) -> Result<std::sync::MutexGuard<'_, ClipboardContext>, String> {
- self.context
- .as_ref()
- .map_err(Clone::clone)?
+ #[cfg(test)]
+ fn with_initializer(initialize: fn() -> Result<ClipboardContext, String>) -> Self {
+ Self {
+ context: Mutex::new(None),
+ initialize,
+ }
+ }
+
+ fn with_context<T>(
+ &self,
+ operation: impl FnOnce(&ClipboardContext) -> Result<T, String>,
+ ) -> Result<T, String> {
+ let mut context = self
+ .context
.lock()
- .map_err(|_| "clipboard lock was poisoned".to_owned())
+ .map_err(|_| "clipboard lock was poisoned".to_owned())?;
+ if context.is_none() {
+ let initialized =
+ catch_unwind(AssertUnwindSafe(|| (self.initialize)())).map_err(|panic| {
+ format!(
+ "clipboard initialization panicked: {}",
+ panic_message(panic)
+ )
+ })??;
+ *context = Some(initialized);
+ }
+ operation(context.as_ref().expect("clipboard context was initialized"))
+ }
+}
+
+fn initialize_context() -> Result<ClipboardContext, String> {
+ ClipboardContext::new().map_err(|error| error.to_string())
+}
+
+fn panic_message(panic: Box<dyn Any + Send>) -> String {
+ if let Some(message) = panic.downcast_ref::<&str>() {
+ (*message).to_owned()
+ } else if let Some(message) = panic.downcast_ref::<String>() {
+ message.clone()
+ } else {
+ "unknown native clipboard failure".to_owned()
}
}
@@ -39,41 +79,90 @@ fn absolute_existing_file(path: &str) -> Result<String, String> {
impl ClipboardBackend for MacosClipboard {
fn available_formats(&self) -> Result<Vec<String>, String> {
- self.context()?
- .available_formats()
- .map_err(|e| e.to_string())
+ self.with_context(|context| context.available_formats().map_err(|e| e.to_string()))
}
fn read_text(&self) -> Result<Option<String>, String> {
- let context = self.context()?;
- if context.has(ContentFormat::Text) {
- context.get_text().map(Some).map_err(|e| e.to_string())
- } else {
- Ok(None)
- }
+ self.with_context(|context| {
+ if context.has(ContentFormat::Text) {
+ context.get_text().map(Some).map_err(|e| e.to_string())
+ } else {
+ Ok(None)
+ }
+ })
}
fn write_text(&self, text: String) -> Result<(), String> {
- self.context()?.set_text(text).map_err(|e| e.to_string())
+ self.with_context(|context| context.set_text(text).map_err(|e| e.to_string()))
}
fn write_image(&self, absolute_path: &str) -> Result<(), String> {
let path = absolute_existing_file(absolute_path)?;
let image = clipboard_rs::RustImageData::from_path(&path).map_err(|e| e.to_string())?;
- self.context()?.set_image(image).map_err(|e| e.to_string())
+ self.with_context(|context| context.set_image(image).map_err(|e| e.to_string()))
}
fn write_file_url(&self, absolute_path: &str) -> Result<(), String> {
let path = absolute_existing_file(absolute_path)?;
- self.context()?
- .set_files(vec![path])
- .map_err(|e| e.to_string())
+ self.with_context(|context| context.set_files(vec![path]).map_err(|e| e.to_string()))
}
}
#[cfg(test)]
mod tests {
use super::*;
+ use std::sync::atomic::{AtomicUsize, Ordering};
+
+ static CONSTRUCTION_INITIALIZATION_ATTEMPTS: AtomicUsize = AtomicUsize::new(0);
+ static UNAVAILABLE_INITIALIZATION_ATTEMPTS: AtomicUsize = AtomicUsize::new(0);
+
+ fn construction_probe_context() -> Result<ClipboardContext, String> {
+ CONSTRUCTION_INITIALIZATION_ATTEMPTS.fetch_add(1, Ordering::SeqCst);
+ Err("construction unexpectedly initialized the clipboard".into())
+ }
+
+ fn unavailable_context() -> Result<ClipboardContext, String> {
+ UNAVAILABLE_INITIALIZATION_ATTEMPTS.fetch_add(1, Ordering::SeqCst);
+ Err("general pasteboard is unavailable".into())
+ }
+
+ fn panicking_context() -> Result<ClipboardContext, String> {
+ panic!("unexpected NULL returned from +[NSPasteboard generalPasteboard]")
+ }
+
+ #[test]
+ fn construction_does_not_initialize_the_native_clipboard() {
+ CONSTRUCTION_INITIALIZATION_ATTEMPTS.store(0, Ordering::SeqCst);
+ let _backend = MacosClipboard::with_initializer(construction_probe_context);
+ assert_eq!(
+ CONSTRUCTION_INITIALIZATION_ATTEMPTS.load(Ordering::SeqCst),
+ 0
+ );
+ }
+
+ #[test]
+ fn unavailable_initialization_is_retryable() {
+ UNAVAILABLE_INITIALIZATION_ATTEMPTS.store(0, Ordering::SeqCst);
+ let backend = MacosClipboard::with_initializer(unavailable_context);
+ for _ in 0..2 {
+ assert!(backend
+ .available_formats()
+ .unwrap_err()
+ .contains("general pasteboard is unavailable"));
+ }
+ assert_eq!(
+ UNAVAILABLE_INITIALIZATION_ATTEMPTS.load(Ordering::SeqCst),
+ 2
+ );
+ }
+
+ #[test]
+ fn native_initialization_panic_becomes_an_error() {
+ let backend = MacosClipboard::with_initializer(panicking_context);
+ let error = backend.available_formats().unwrap_err();
+ assert!(error.contains("clipboard initialization panicked"));
+ assert!(error.contains("unexpected NULL returned"));
+ }
#[test]
fn rejects_relative_local_paths_before_clipboard_access() {