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-rs)(macos): guard AppKit init so `mcp` runs headless instead of SIGABRT-ing (#1724) (#1781)

Francesco Bonacci57722f709174
diff --git a/libs/cua-driver/rust/crates/platform-macos/src/cursor/overlay.rs b/libs/cua-driver/rust/crates/platform-macos/src/cursor/overlay.rs
index 4dcdb7c68c..034e51ec90 100644
--- a/libs/cua-driver/rust/crates/platform-macos/src/cursor/overlay.rs
+++ b/libs/cua-driver/rust/crates/platform-macos/src/cursor/overlay.rs
@@ -145,6 +145,21 @@ pub fn run_on_main_thread() {
loop { std::thread::park(); }
}
+ // AppKit's `+[NSApplication sharedApplication]` registers the process with
+ // the Window Server and ABORTS the whole process (SIGABRT in
+ // `_RegisterApplication`) when there's no graphic-session access — e.g.
+ // `mcp` run as a stdio child from SSH, a LaunchDaemon, or headless CI.
+ // Detect that without touching AppKit and run headless: the MCP server
+ // keeps serving on its background thread while this thread just parks,
+ // exactly as it does when the overlay is disabled. See issue #1724.
+ if !crate::session::has_graphic_access() {
+ tracing::warn!(
+ "no Window Server / graphic-session access — skipping cursor \
+ overlay and running headless (issue #1724)"
+ );
+ loop { std::thread::park(); }
+ }
+
// ------------------------------------------------------------------
// AppKit setup (all on the main thread).
// ------------------------------------------------------------------
diff --git a/libs/cua-driver/rust/crates/platform-macos/src/lib.rs b/libs/cua-driver/rust/crates/platform-macos/src/lib.rs
index bee67dc831..d91743adb9 100644
--- a/libs/cua-driver/rust/crates/platform-macos/src/lib.rs
+++ b/libs/cua-driver/rust/crates/platform-macos/src/lib.rs
@@ -36,6 +36,8 @@ pub mod recording_hooks;
pub mod video_sckit;
#[cfg(target_os = "macos")]
pub mod pip;
+#[cfg(target_os = "macos")]
+pub mod session;
use cua_driver_core::tool::ToolRegistry;
diff --git a/libs/cua-driver/rust/crates/platform-macos/src/session.rs b/libs/cua-driver/rust/crates/platform-macos/src/session.rs
new file mode 100644
index 0000000000..80a444c913
--- /dev/null
+++ b/libs/cua-driver/rust/crates/platform-macos/src/session.rs
@@ -0,0 +1,58 @@
+//! Security-session capability probe.
+//!
+//! AppKit's `+[NSApplication sharedApplication]` registers the process with
+//! the Window Server (`_RegisterApplication`). When the calling process has
+//! no graphic-session access — `cua-driver mcp` spawned as a stdio child from
+//! an SSH session, a `LaunchDaemon`, or a headless CI runner — that
+//! registration **aborts the whole process with SIGABRT** before any error
+//! reaches the caller (issue #1724).
+//!
+//! `SessionGetInfo` answers "can this session talk to the Window Server?"
+//! *without* touching AppKit, so we can gate overlay init on it and degrade
+//! to a headless MCP server instead of crashing. This is the macOS analogue
+//! of the Session-0 short-circuit guard used on Windows.
+
+// `SecuritySessionId` and `SessionAttributeBits` are both 32-bit ints in
+// <Security/AuthSession.h>.
+type SecuritySessionId = i32;
+type SessionAttributeBits = u32;
+
+/// `callerSecuritySession` — the session of the calling process.
+const CALLER_SECURITY_SESSION: SecuritySessionId = -1;
+/// `sessionHasGraphicAccess` — the session can use the Window Server.
+const SESSION_HAS_GRAPHIC_ACCESS: SessionAttributeBits = 0x0010;
+
+#[link(name = "Security", kind = "framework")]
+extern "C" {
+ fn SessionGetInfo(
+ session: SecuritySessionId,
+ session_id: *mut SecuritySessionId,
+ attributes: *mut SessionAttributeBits,
+ ) -> i32; // OSStatus; errSecSuccess == 0
+}
+
+/// Whether the current security session can use the Window Server (and thus
+/// safely initialize AppKit). On any failure we return `false` — skipping the
+/// overlay is always recoverable, but a wrong `true` re-introduces the
+/// `_RegisterApplication` SIGABRT (issue #1724).
+pub fn has_graphic_access() -> bool {
+ let mut session_id: SecuritySessionId = 0;
+ let mut attributes: SessionAttributeBits = 0;
+ let status =
+ unsafe { SessionGetInfo(CALLER_SECURITY_SESSION, &mut session_id, &mut attributes) };
+ status == 0 && (attributes & SESSION_HAS_GRAPHIC_ACCESS) != 0
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ /// The probe must resolve the `Security` symbol and return a `bool`
+ /// without aborting. We can't assert the value: it's `true` on a dev GUI
+ /// session and `false` on a headless CI runner — which is exactly the
+ /// distinction the guard relies on (issue #1724).
+ #[test]
+ fn graphic_access_probe_does_not_abort() {
+ let _ = has_graphic_access();
+ }
+}