From 05cd7832e87ed1252074497de53899a43b39cbc1 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Wed, 31 Dec 2025 16:13:21 -0800 Subject: [PATCH] fix(wm): process notifications from destroyed windows When closing a window with CMD+W, the AXUIElementDestroyed notification has an empty title. We need to add an is_destroyed override for these AXUIElementDestroyed notifications without a title so that they can be processed. This commit also fixes a race condition exposed by the above fix where the reaper thread sometimes removes the window before the reap function tries to remove it during event processing. When this happens, we can just ignore the error and continue instead of propagating it up and interrupting the event processing loop until the user's next manual interaction with another window. --- komorebi/src/application.rs | 18 ++++++++++++------ komorebi/src/workspace.rs | 3 ++- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/komorebi/src/application.rs b/komorebi/src/application.rs index f34b3b61..8ef73e81 100644 --- a/komorebi/src/application.rs +++ b/komorebi/src/application.rs @@ -58,18 +58,23 @@ unsafe extern "C-unwind" fn application_observer_callback( _context: *mut c_void, ) { unsafe { + let notification_str = notification.as_ref().to_string(); let name = AccessibilityApi::copy_attribute_value::(element.as_ref(), kAXTitleAttribute) .map(|s| s.to_string()); - if let Some(name) = name - && !name.is_empty() - { + // AXUIElementDestroyed fires when the element is already gone, + // so we can't get the title - but we still need to process it + let is_destroyed = matches!( + AccessibilityNotification::from_str(¬ification_str), + Ok(AccessibilityNotification::AXUIElementDestroyed) + ); + + if is_destroyed || name.as_ref().is_some_and(|n| !n.is_empty()) { let mut process_id = 0; element.as_ref().pid(NonNull::from_mut(&mut process_id)); - if let Ok(notification) = - AccessibilityNotification::from_str(¬ification.as_ref().to_string()) + if let Ok(notification) = AccessibilityNotification::from_str(¬ification_str) && let Some(event) = WindowManagerEvent::from_system_notification( SystemNotification::Accessibility(notification), process_id, @@ -77,7 +82,8 @@ unsafe extern "C-unwind" fn application_observer_callback( ) { tracing::debug!( - "notification: {notification}, process: {process_id}, name: \"{name}\"", + "notification: {notification}, process: {process_id}, name: \"{}\"", + name.as_deref().unwrap_or("") ); window_manager_event_listener::send_notification(event); diff --git a/komorebi/src/workspace.rs b/komorebi/src/workspace.rs index b6b490d0..aaf3251a 100644 --- a/komorebi/src/workspace.rs +++ b/komorebi/src/workspace.rs @@ -449,7 +449,8 @@ impl Workspace { } for window_id in &invalid_window_ids { - self.remove_window(*window_id)?; + // Ignore errors - window may have already been removed by the reaper thread + let _ = self.remove_window(*window_id); } for window_id in &invalid_floating_window_ids {