Repository navigation
[macOS] Fix CGEventTap timeout deadlocks, Chrome Remote Desktop freezes, and callback IPC stalls - #340
Open
mrtruongleo wants to merge 2 commits into
Open
[macOS] Fix CGEventTap timeout deadlocks, Chrome Remote Desktop freezes, and callback IPC stalls#340mrtruongleo wants to merge 2 commits into
mrtruongleo wants to merge 2 commits into
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
Chrome Remote Desktop ~2s freeze & Disconnect banner popup on every accented character
remoting_me2me_host) monitors local keyboard input onkCGSessionEventTapand only ignores events whosekCGEventSourceUnixProcessID == getpid(). When OpenKey tapskCGSessionEventTapand posts replacement Backspace/UnicodeCGEvents viaCGEventTapPostEvent, 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).kCGAnnotatedSessionEventTap(falling back to tail ofkCGSessionEventTapif needed) and preservekCGEventTargetUnixProcessID&kCGEventTargetProcessSerialNumberviaPostTapEvent(...). Replacement events are now posted afterkCGSessionEventTap, so Chrome Remote Desktop never intercepts them while target apps still receive them cleanly.OpenKey permanently stops typing accents after OOM / system stalls (even when icon stays
V)OpenKeyCallbackdid not handlekCGEventTapDisabledByTimeout(0xFFFFFFFE) orkCGEventTapDisabledByUserInput(0xFFFFFFFF), so once WindowServer disabled the tap during memory pressure/swap stalls, it stayed disabled indefinitely.OpenKeyCallbackon disable events, add a 5s watchdog timer (ensureEventTapActive) to re-enable or recreate invalidated taps, check tap status on wake, and holdNSActivityLatencyCriticalto prevent App Nap / low-QoS throttling under memory pressure.Synchronous IPC stalls & CF memory bug inside
CGEventTapcallbackCGWindowListCopyWindowInfo(...)(WindowServer IPC inside WindowServer's own synchronous event tap) and[[NSWorkspace sharedWorkspace] frontmostApplication]. In addition,vOtherLanguagecalledTISCopyCurrentKeyboardInputSource()on every keydown, leakedisourceon early return, and calledCFRelease(langRef)on a non-ownedCFArrayGetValueAtIndexpointer.kCGEventTargetUnixProcessID(O(1) PID check, automatically detectingcom.apple.SpotlightwithoutCGWindowListCopyWindowInfo) +NSWorkspaceDidActivateApplicationNotification, cache input source language viakTISNotifySelectedKeyboardInputSourceChangedwithout over-releasinglangRef, and avoid leakingCGEventSource/ reusable BackspaceCGEvents across re-init.Version bump
2.0.6(build50) and deployment target to12.0.Verification
arm64) cleanly with Xcode 26.4.1 (** BUILD SUCCEEDED **).CGEventTaptest harness on macOS 26.5.2 that events posted viaCGEventTapPostEventfromkCGAnnotatedSessionEventTapare not observed bykCGSessionEventTaplisteners (sessionSaw=false) while reaching downstream annotated taps with intactkCGEventTargetUnixProcessIDandkCGEventTargetProcessSerialNumber.