From a501c05809162274614ddf04b61c01d1b7d79542 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Sat, 31 Oct 2026 15:08:48 -0700 Subject: [PATCH] fix(mac): the registry keeps inert copies of the application BEHAVIOUR (cost only; nothing reads the registry yet except the unused MacosBackend). Review finding 1. Application::drop does a blocking Accessibility round trip (is_valid -> copy_attribute_names) when is_observable, and invalidates the observer that every clone shares if that fails. AxEntry held a clone of the Application, so every register (which drops the entry it replaces), forget and forget_pid ran that call, on the event thread with the window manager lock held, and an unresponsive app made it slow or made it invalidate the observer of a live app. Window::entry() now stores Application::inert(), a clone with is_observable false, whose drop does nothing, and Window::from_entry therefore builds windows with an inert application as well (it only affects Drop). Tests (run on a Mac; the flag is plain data and needs no Accessibility permission): a registered entry and a window made from an entry have is_observable false even when the window's own application is observable. Verified: macOS fake-CC type-check (tools/fast-check) PASS. Not verified: any run. Verified on the Mini: not yet. Hand check: H3 of docs/dev/macos-handcheck.md. --- komorebi-for-mac/komorebi/src/application.rs | 10 +++++ komorebi-for-mac/komorebi/src/ax_registry.rs | 41 ++++++++++++++++++++ komorebi-for-mac/komorebi/src/window.rs | 3 +- 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/komorebi-for-mac/komorebi/src/application.rs b/komorebi-for-mac/komorebi/src/application.rs index 8beace74..633c89e8 100644 --- a/komorebi-for-mac/komorebi/src/application.rs +++ b/komorebi-for-mac/komorebi/src/application.rs @@ -121,6 +121,16 @@ impl Application { }) } + /// A clone that does nothing when it is dropped: [`Application::drop`] asks the application + /// whether it is still there (a blocking Accessibility round trip) and may invalidate the + /// observer that every clone shares, which is for the clones a window carries and not for the + /// ones that a table keeps. + pub fn inert(&self) -> Self { + let mut inert = self.clone(); + inert.is_observable = false; + inert + } + /// The AX element of the application itself, as opposed to one of its windows pub fn element(&self) -> &AccessibilityUiElement { &self.element diff --git a/komorebi-for-mac/komorebi/src/ax_registry.rs b/komorebi-for-mac/komorebi/src/ax_registry.rs index a3159dac..0c9f1e4a 100644 --- a/komorebi-for-mac/komorebi/src/ax_registry.rs +++ b/komorebi-for-mac/komorebi/src/ax_registry.rs @@ -112,3 +112,44 @@ pub fn identify( ) -> std::result::Result { crate::accessibility::AccessibilityApi::window_id(element) } + +#[cfg(test)] +mod tests { + use super::*; + + fn window_of(id: u32, pid: i32) -> Window { + let mut window = Window::from(id); + window.application.process_id = pid; + window + } + + fn id(raw: u32) -> WindowId { + WindowId::from_u32(raw) + } + + /// `Application::drop` asks the application whether it is still there (a blocking + /// Accessibility round trip) when `is_observable`; the table must hold copies that skip it. + #[test] + fn a_registered_entry_does_not_ask_the_app_when_it_is_dropped() { + let mut window = window_of(0x5101, 6_100_001); + window.application.is_observable = true; + + assert!(!window.entry().application.is_observable); + + register(&window); + assert!(!get(id(0x5101)).unwrap().application.is_observable); + + forget(0x5101); + } + + #[test] + fn a_window_made_from_an_entry_is_inert_too() { + let mut window = window_of(0x5102, 6_100_002); + window.application.is_observable = true; + register(&window); + + assert!(!super::window(id(0x5102)).unwrap().application.is_observable); + + forget(0x5102); + } +} diff --git a/komorebi-for-mac/komorebi/src/window.rs b/komorebi-for-mac/komorebi/src/window.rs index ebc101dd..3699d077 100644 --- a/komorebi-for-mac/komorebi/src/window.rs +++ b/komorebi-for-mac/komorebi/src/window.rs @@ -578,7 +578,8 @@ impl Window { crate::ax_registry::AxEntry { element: self.element.clone(), observer: self.observer.clone(), - application: self.application.clone(), + // inert: dropping an entry must not ask the application anything (see `Application::inert`) + application: self.application.inert(), } }