Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio desktop: survive AppKit exceptions during event dispatch on macOS (#12277)
Michael Han97dc0c47ca00
diff --git a/studio/src-tauri/Cargo.toml b/studio/src-tauri/Cargo.tomlindex fa12c13f99a..b1a7f9fd18e 100644--- a/studio/src-tauri/Cargo.toml+++ b/studio/src-tauri/Cargo.toml@@ -51,7 +51,7 @@ libc = "0.2"signal-hook = "0.4"[target.'cfg(target_os = "macos")'.dependencies]-objc2 = "0.6"+objc2 = { version = "0.6", features = ["exception"] }objc2-app-kit = { version = "0.3.2", default-features = false, features = ["std", "NSAppearance", "NSButton", "NSControl", "NSResponder", "NSStatusBarButton", "NSStatusItem", "NSView"] }objc2-foundation = { version = "0.3.2", default-features = false, features = ["std", "NSArray", "NSDictionary", "NSKeyValueObserving", "NSObject", "NSString"] }diff --git a/studio/src-tauri/src/macos_event_guard.rs b/studio/src-tauri/src/macos_event_guard.rsnew file mode 100644index 00000000000..f958bd20101--- /dev/null+++ b/studio/src-tauri/src/macos_event_guard.rs@@ -0,0 +1,163 @@+//! tao's `extern "C"` `sendEvent:` overrides abort on any Objective-C exception beneath them+//! (macOS 27.0 NSCampoLightweightUIController throws one); catch it at AppKit's `sendEvent:`.+//! Upstream fix: tauri-apps/tao#1354.++use std::ffi::CStr;+use std::panic::AssertUnwindSafe;+use std::sync::atomic::{AtomicU64, Ordering};+use std::sync::OnceLock;++use log::{error, warn};+use objc2::exception::Exception;+use objc2::rc::Retained;+use objc2::runtime::{AnyClass, AnyObject, Imp, Sel};+use objc2::{class, msg_send, sel};++type SendEvent = unsafe extern "C-unwind" fn(*mut AnyObject, Sel, *mut AnyObject);++/// NSWindow also covers TaoApp's Cmd+key-up path, which calls the key window's `sendEvent:`.+const GUARDED_CLASSES: [&CStr; 2] = [c"NSApplication", c"NSWindow"];+static APPKIT_SEND_EVENT: [OnceLock<SendEvent>; 2] = [OnceLock::new(), OnceLock::new()];+static CAUGHT_EXCEPTIONS: AtomicU64 = AtomicU64::new(0);++/// Call on the main thread before the event loop starts.+pub fn install() {+ install_guard::<0>();+ install_guard::<1>();+}++fn install_guard<const I: usize>() {+ let name = GUARDED_CLASSES[I];+ if APPKIT_SEND_EVENT[I].get().is_some() {+ return;+ }+ let Some((class, method)) = AnyClass::get(name)+ .and_then(|class| Some((class, class.instance_method(sel!(sendEvent:))?)))+ else {+ warn!("{name:?} has no sendEvent:; AppKit exceptions beneath it stay fatal");+ return;+ };+ // SAFETY: Same signature, and the original is stored before the swap can call it.+ unsafe {+ let appkit: SendEvent = std::mem::transmute::<Imp, SendEvent>(method.implementation());+ let _ = APPKIT_SEND_EVENT[I].set(appkit);+ objc2::ffi::class_replaceMethod(+ (class as *const AnyClass).cast_mut(),+ sel!(sendEvent:),+ std::mem::transmute::<SendEvent, Imp>(guarded_send_event::<I>),+ objc2::ffi::method_getTypeEncoding(method),+ );+ }+}++unsafe extern "C-unwind" fn guarded_send_event<const I: usize>(+ this: *mut AnyObject,+ cmd: Sel,+ event: *mut AnyObject,+) {+ if let Some(appkit) = APPKIT_SEND_EVENT[I].get() {+ // SAFETY: AppKit's arguments, forwarded to AppKit's implementation.+ unsafe { dispatch(*appkit, this, cmd, event) };+ }+}++/// Catches Objective-C exceptions only. Rust panics pass through and stay fatal.+unsafe fn dispatch(send_event: SendEvent, this: *mut AnyObject, cmd: Sel, event: *mut AnyObject) {+ // SAFETY: Upheld by the caller.+ let result =+ objc2::exception::catch(AssertUnwindSafe(|| unsafe { send_event(this, cmd, event) }));+ if let Err(exception) = result {+ // Null only in tests, which must not touch NSApp off the main thread.+ unsafe { report(!this.is_null(), exception) };+ }+}++/// Like `-[NSApplication run]`, honoring NSApplicationCrashOnExceptions. Powers of two only.+unsafe fn report(to_app: bool, exception: Option<Retained<Exception>>) {+ let count = CAUGHT_EXCEPTIONS.fetch_add(1, Ordering::Relaxed) + 1;+ if !count.is_power_of_two() {+ return;+ }+ let Some(exception) = exception else {+ error!("Caught a nil AppKit exception during event dispatch (#{count})");+ return;+ };+ error!("Caught an AppKit exception during event dispatch (#{count}): {exception:?}");+ if !to_app {+ return;+ }+ // Skip nil: msg_send! panics on it in debug builds, and a panic here aborts.+ let app: Option<Retained<AnyObject>> =+ unsafe { msg_send![class!(NSApplication), sharedApplication] };+ if let Some(app) = app {+ let () = unsafe { msg_send![&*app, reportException: &*exception] };+ }+}++#[cfg(test)]+mod tests {+ use super::*;+ use objc2_foundation::NSString;++ static REACHED_APPKIT: AtomicU64 = AtomicU64::new(0);++ unsafe extern "C-unwind" fn throwing_send_event(_: *mut AnyObject, _: Sel, _: *mut AnyObject) {+ REACHED_APPKIT.fetch_add(1, Ordering::SeqCst);+ let name = NSString::from_str("NSInternalInconsistencyException");+ let reason = NSString::from_str("simulated NSCampoLightweightUIController assertion");+ let exception: Retained<AnyObject> = unsafe {+ msg_send![+ class!(NSException),+ exceptionWithName: &*name,+ reason: &*reason,+ userInfo: std::ptr::null::<AnyObject>()+ ]+ };+ // SAFETY: An NSException instance.+ objc2::exception::throw(unsafe { Retained::cast_unchecked::<Exception>(exception) });+ }++ /// Stands in for tao's `extern "C"` override. Without the catch, this aborts the test.+ extern "C" fn tao_send_event(send_event: SendEvent) {+ unsafe {+ dispatch(+ send_event,+ std::ptr::null_mut(),+ sel!(sendEvent:),+ std::ptr::null_mut(),+ )+ };+ }++ #[test]+ fn appkit_exceptions_beneath_an_extern_c_frame_do_not_abort() {+ for _ in 0..5 {+ tao_send_event(throwing_send_event);+ }+ assert_eq!(REACHED_APPKIT.load(Ordering::SeqCst), 5);+ assert_eq!(CAUGHT_EXCEPTIONS.load(Ordering::SeqCst), 5);+ }++ #[test]+ fn install_wraps_appkit_send_event_once() {+ let method = |name: &CStr| {+ AnyClass::get(name)+ .and_then(|class| class.instance_method(sel!(sendEvent:)))+ .expect("AppKit class implements sendEvent:")+ };+ let appkit = GUARDED_CLASSES.map(|name| method(name).implementation() as usize);+ install();+ install();+ let guarded: [Imp; 2] = unsafe {+ [+ std::mem::transmute::<SendEvent, Imp>(guarded_send_event::<0>),+ std::mem::transmute::<SendEvent, Imp>(guarded_send_event::<1>),+ ]+ };+ for (i, name) in GUARDED_CLASSES.into_iter().enumerate() {+ assert_eq!(method(name).implementation() as usize, guarded[i] as usize);+ let stored = *APPKIT_SEND_EVENT[i].get().expect("original stored");+ assert_eq!(stored as usize, appkit[i]);+ }+ }+}diff --git a/studio/src-tauri/src/main.rs b/studio/src-tauri/src/main.rsindex 0a03dd63bc0..b5a97b2b9e4 100644--- a/studio/src-tauri/src/main.rs+++ b/studio/src-tauri/src/main.rs@@ -16,6 +16,8 @@ mod install_watchdog;mod linux_webkit;mod loopback_http;#[cfg(target_os = "macos")]+mod macos_event_guard;+#[cfg(target_os = "macos")]mod macos_tray;mod native_backend_lease;mod native_clipboard;@@ -40,7 +42,7 @@ use std::ffi::OsStr;use std::fs;use std::io::Write;use std::path::{Path, PathBuf};-use std::sync::atomic::{AtomicBool, Ordering};+use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};use std::sync::{Mutex, MutexGuard, OnceLock};use tauri::menu::{MenuBuilder, MenuItem, MenuItemBuilder};use tauri::tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent};@@ -788,8 +790,9 @@ fn setup_logging() {let log_path = log_dir.join("tauri.log");let rotated_path = log_dir.join("tauri.log.1");let max_log_bytes = 5 * 1024 * 1024;- if let Ok(file) = RotatingLogFile::open(log_path, rotated_path, max_log_bytes) {+ if let Ok(file) = RotatingLogFile::open(log_path.clone(), rotated_path, max_log_bytes) {loggers.push(WriteLogger::new(LevelFilter::Info, Config::default(), file));+ let _ = PANIC_LOG_PATH.set(log_path);}}}@@ -799,6 +802,37 @@ fn setup_logging() {}}+static PANIC_LOG_PATH: OnceLock<PathBuf> = OnceLock::new();++/// Own file handle, not `log`: a panic raised inside the logger's lock would deadlock.+fn log_panics() {+ static PANICS: AtomicU64 = AtomicU64::new(0);+ let default_hook = std::panic::take_hook();+ std::panic::set_hook(Box::new(move |info| {+ if let Some(path) = PANIC_LOG_PATH.get() {+ // Symbolizing is slow, so only the first few panics get a backtrace.+ let backtrace = if PANICS.fetch_add(1, Ordering::Relaxed) < 4 {+ format!("\n{}", std::backtrace::Backtrace::force_capture())+ } else {+ String::new()+ };+ let now = time::OffsetDateTime::now_utc();+ let thread = std::thread::current();+ if let Ok(mut file) = fs::OpenOptions::new().append(true).open(path) {+ let _ = writeln!(+ file,+ "{:02}:{:02}:{:02} [ERROR] thread '{}' {info}{backtrace}",+ now.hour(),+ now.minute(),+ now.second(),+ thread.name().unwrap_or("<unnamed>"),+ );+ }+ }+ default_hook(info);+ }));+}+#[cfg(any(target_os = "windows", target_os = "linux"))]fn setup_custom_titlebar(app: &tauri::App) -> Result<(), Box<dyn std::error::Error>> {let window = app.get_webview_window("main").ok_or_else(|| {@@ -2097,7 +2131,10 @@ fn main() {let _ = fix_path_env::fix();setup_logging();+ log_panics();info!("Unsloth desktop app starting");+ #[cfg(target_os = "macos")]+ macos_event_guard::install();#[cfg(target_os = "linux")]if let Some((variables, reason)) = webkit_rendering_workaround {