Skip to content

[macOS] Fix CGEventTap timeout deadlocks, Chrome Remote Desktop freezes, and callback IPC stalls - #340

Open
mrtruongleo wants to merge 2 commits into
tuyenvm:masterfrom
mrtruongleo:fix/macos-eventtap-timeout-crd-freeze
Open

mrtruongleo wants to merge 2 commits into
tuyenvm:masterfrom
mrtruongleo:fix/macos-eventtap-timeout-crd-freeze

Conversation

@mrtruongleo

Copy link
Copy Markdown

Summary

Fixes persistent macOS freezes, Chrome Remote Desktop input lockouts, and OpenKey permanently stopping Vietnamese composition after OOM/memory pressure or sleep/wake.

Root Causes & Fixes

  1. Chrome Remote Desktop ~2s freeze & Disconnect banner popup on every accented character

    • Cause: Chrome Remote Desktop (remoting_me2me_host) monitors local keyboard input on kCGSessionEventTap and only ignores events whose kCGEventSourceUnixProcessID == getpid(). When OpenKey taps kCGSessionEventTap and posts replacement Backspace/Unicode CGEvents via CGEventTapPostEvent, macOS stamps them with OpenKey's PID, causing Chrome Remote Desktop to treat every accented Vietnamese character as local physical keyboard input (pausing remote input for ~2s and flashing the "Your desktop is currently shared..." popup).
    • Fix: Register OpenKey's event tap at kCGAnnotatedSessionEventTap (falling back to tail of kCGSessionEventTap if needed) and preserve kCGEventTargetUnixProcessID & kCGEventTargetProcessSerialNumber via PostTapEvent(...). Replacement events are now posted after kCGSessionEventTap, so Chrome Remote Desktop never intercepts them while target apps still receive them cleanly.
  2. OpenKey permanently stops typing accents after OOM / system stalls (even when icon stays V)

    • Cause: OpenKeyCallback did not handle kCGEventTapDisabledByTimeout (0xFFFFFFFE) or kCGEventTapDisabledByUserInput (0xFFFFFFFF), so once WindowServer disabled the tap during memory pressure/swap stalls, it stayed disabled indefinitely.
    • Fix: Immediately re-enable the tap at the top of OpenKeyCallback on disable events, add a 5s watchdog timer (ensureEventTapActive) to re-enable or recreate invalidated taps, check tap status on wake, and hold NSActivityLatencyCritical to prevent App Nap / low-QoS throttling under memory pressure.
  3. Synchronous IPC stalls & CF memory bug inside CGEventTap callback

    • Cause: Every accented keystroke synchronously called CGWindowListCopyWindowInfo(...) (WindowServer IPC inside WindowServer's own synchronous event tap) and [[NSWorkspace sharedWorkspace] frontmostApplication]. In addition, vOtherLanguage called TISCopyCurrentKeyboardInputSource() on every keydown, leaked isource on early return, and called CFRelease(langRef) on a non-owned CFArrayGetValueAtIndex pointer.
    • Fix: Cache the active target bundle identifier from kCGEventTargetUnixProcessID (O(1) PID check, automatically detecting com.apple.Spotlight without CGWindowListCopyWindowInfo) + NSWorkspaceDidActivateApplicationNotification, cache input source language via kTISNotifySelectedKeyboardInputSourceChanged without over-releasing langRef, and avoid leaking CGEventSource / reusable Backspace CGEvents across re-init.
  4. Version bump

    • Bumped macOS target version to 2.0.6 (build 50) and deployment target to 12.0.

Verification

  • Built Release (arm64) cleanly with Xcode 26.4.1 (** BUILD SUCCEEDED **).
  • Verified with live CGEventTap test harness on macOS 26.5.2 that events posted via CGEventTapPostEvent from kCGAnnotatedSessionEventTap are not observed by kCGSessionEventTap listeners (sessionSaw=false) while reaching downstream annotated taps with intact kCGEventTargetUnixProcessID and kCGEventTargetProcessSerialNumber.

…es, and callback IPC stalls (v2.0.6)

Fixes three macOS input reliability issues:

1. Chrome Remote Desktop freezes & Disconnect popup on accented input:
   - Chrome Remote Desktop's LocalKeyboardInputMonitorMac listens on
     kCGSessionEventTap and ignores remote events where
     kCGEventSourceUnixProcessID == getpid().
   - Because OpenKey previously tapped kCGSessionEventTap and synthesized
     Backspace/Unicode replacement events with OpenKey's own PID, Chrome
     Remote Desktop misclassified accented Vietnamese characters as local
     physical input, triggering a ~2s remote-input lockout and showing the
     Disconnect banner on every accented keystroke.
   - Move OpenKey's tap to kCGAnnotatedSessionEventTap (with fallback to
     tail of kCGSessionEventTap) and propagate kCGEventTargetUnixProcessID
     and kCGEventTargetProcessSerialNumber onto posted replacement events
     so kCGSessionEventTap monitors no longer intercept OpenKey's synthetic
     output.

2. Permanent loss of Vietnamese input after OOM / system stalls:
   - Handle kCGEventTapDisabledByTimeout and kCGEventTapDisabledByUserInput
     at the start of OpenKeyCallback by immediately re-enabling the tap.
   - Add a 5-second watchdog timer and wake recovery to re-enable or
     recreate the tap if invalidated.
   - Hold NSActivityLatencyCritical to prevent App Nap / low-QoS throttling
     under memory pressure.

3. Synchronous WindowServer / LaunchServices / TIS IPC inside event tap:
   - Remove per-keystroke CGWindowListCopyWindowInfo() and
     NSWorkspace.frontmostApplication calls from OpenKeyCallback; cache the
     target app bundle ID by kCGEventTargetUnixProcessID and update
     _frontMostApp on NSWorkspaceDidActivateApplicationNotification.
   - Fix CFRelease over-release / leak in vOtherLanguage and cache current
     input source language via kTISNotifySelectedKeyboardInputSourceChanged.
   - Release previous CGEventSource / Backspace events when re-initializing.

Bump macOS version to 2.0.6 (build 50) and deployment target to 12.0.
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