Skip to content

Bound AX messaging timeouts in SnappingManager to prevent multi-second main-thread hangs - #1988

Open
poife wants to merge 1 commit into
rxhanson:mainfrom
poife:fix/avoid-self-ax-deadlock
Open

poife wants to merge 1 commit into
rxhanson:mainfrom
poife:fix/avoid-self-ax-deadlock

Conversation

@poife

@poife poife commented Nov 28, 2026

Copy link
Copy Markdown

Summary

When an app is temporarily unresponsive on the Accessibility API (for example, Electron/Chromium apps during heavy DOM streaming) or when Rectangle's own Settings window is open/active during window interactions, SnappingManager can stall the main thread for several seconds:

  1. AccessibilityElement.getWindowElementUnderCursor performs unbounded synchronous AX calls (AXUIElementCopyAttributeValue(.windows) and AXUIElementCopyElementAtPosition). When an app times out, Stage 1 falls through to Stage 2 (systemWide) and Stage 3 (frontmostApplication), and SnappingManager.leftMouseDragged retries up to 20 times.
  2. SnappingManager.checkFullScreen() calls AccessibilityElement.getFrontWindowElement() on frontAppChanged, which also cascades through unbounded AX fallbacks (.focusedWindow -> .windows -> systemWide.focusedUIElement).
  3. AccessibilityElement(pid) previously queried .role over AX IPC inside applicationElement even though the element is already known to be an application element, and getWindowElementUnderCursor did not short-circuit on Rectangle's own windows.

Changes

  • Rectangle/Utilities/AXExtension.swift: Added AXLookupResult, AXUIElement.didMessagingTimeout, and performTimedLookup for optional bounded-timeout getValueResult / getElementAtPositionResult calls (restoring timeout to 0 via defer).
  • Rectangle/General/AccessibilityElement.swift:
    • Added knownApplication flag for AccessibilityElement(_ pid:) to skip redundant .role AX IPC.
    • Added WindowLookupResult (.found, .ownWindow, .notFound, .timedOut) and axLookupTimeout = 0.25 (matching OverlapOffsetGeometry, StackBadgeManager, StackCycleManager, and NewWindowActionManager).
    • Short-circuited resolveWindowElementUnderCursor on own windows and on .timedOut so fallbacks do not cascade.
    • Unified getFrontWindowElement(timeout: Float? = nil) with resolveFrontWindowElement to stop cascading on .timedOut when bounded.
  • Rectangle/Snapping/SnappingManager.swift:
    • Evaluated checkFullScreen() in-process via NSApp.windows when own app is frontmost, and bounded external-app lookups with axLookupTimeout.
    • Extracted abortNativeGesture() and aborted the drag gesture on .ownWindow or .timedOut in leftMouseDown / leftMouseDragged to prevent 20x retry loops.
  • RectangleTests/SnappingTests.swift: Added SelfWindowAccessibilityGuardTests covering own-window short-circuiting, timeout cascading prevention, and SnappingManager drag-retry abort.

Verification

  • Verified with sample against a local Debug build while interacting with a busy Electron app: multi-second getWindowElementUnderCursor / checkFullScreen stalls were eliminated.
  • RectangleTests: 796 passed, 0 failed, 11 skipped (run locally on Xcode 26.6 / macOS 26.7.3).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant