Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix(a11y/macos): serialize NSPasteboard access on a dedicated dispatch queue

Louis Beaumont1ea2d4012091
diff --git a/crates/screenpipe-a11y/Cargo.toml b/crates/screenpipe-a11y/Cargo.toml
index d3ad94aa6..d119e9fb2 100644
--- a/crates/screenpipe-a11y/Cargo.toml
+++ b/crates/screenpipe-a11y/Cargo.toml
@@ -33,7 +33,7 @@ regex = "1.10"
arboard = "3"
[target.'cfg(target_os = "macos")'.dependencies]
-cidre = { version = "0.13", features = ["ax", "cg", "blocks", "ns", "app"] }
+cidre = { version = "0.13", features = ["ax", "cg", "blocks", "ns", "app", "dispatch"] }
[target.'cfg(target_os = "windows")'.dependencies]
# Native Windows APIs - no rdev dependency
diff --git a/crates/screenpipe-a11y/src/platform/macos.rs b/crates/screenpipe-a11y/src/platform/macos.rs
index da142cbf2..7b82f3dba 100644
--- a/crates/screenpipe-a11y/src/platform/macos.rs
+++ b/crates/screenpipe-a11y/src/platform/macos.rs
@@ -1147,14 +1147,33 @@ fn get_focused_element_context(config: &UiCaptureConfig) -> Option<ElementContex
})
}
+// Dedicated serial queue for all NSPasteboard access. NSPasteboard / NSPasteboardItem
+// are not thread-safe — calling `[NSPasteboard stringForType:]` from a worker thread
+// races AppKit's internal type-cache invalidation when another app mutates the
+// pasteboard mid-read, segfaulting in `_updateTypeCacheIfNeeded` (seen on macOS 26.x,
+// crash report 57E6EDAB-D2D1-44D3-9BD0-82DCA482DBFF). The queue serializes every
+// pasteboard read through one thread; the `_with_ar_pool` variant wraps each block
+// in an autorelease pool so AppKit's per-call temp objects drain immediately.
+//
+// We use a private serial queue rather than the main queue so heavy main-thread
+// activity (UI hitches, modal dialogs) can't stall clipboard capture.
+static CLIPBOARD_QUEUE: std::sync::OnceLock<cidre::arc::R<cidre::dispatch::Queue>> =
+ std::sync::OnceLock::new();
+
+fn clipboard_queue() -> &'static cidre::dispatch::Queue {
+ CLIPBOARD_QUEUE.get_or_init(cidre::dispatch::Queue::serial_with_ar_pool)
+}
+
fn get_clipboard() -> Option<String> {
- let mut clipboard = arboard::Clipboard::new().ok()?;
- let text = clipboard.get_text().ok()?;
- if text.is_empty() {
- None
- } else {
- Some(text)
- }
+ clipboard_queue().sync_once(|| {
+ let mut clipboard = arboard::Clipboard::new().ok()?;
+ let text = clipboard.get_text().ok()?;
+ if text.is_empty() {
+ None
+ } else {
+ Some(text)
+ }
+ })
}
fn truncate(s: &str, max: usize) -> String {