minutes icon indicating copy to clipboard operation
minutes copied to clipboard

CGEventTap leak: long-running app accumulates 512+ disabled taps, exhausting the system tap table (breaks screenshots system-wide)

Open jmh1313 opened this issue 4 months ago • 1 comments

Summary

A long-running Minutes.app instance leaked 512+ CGEventTaps (all listen-only and disabled), filling the system-wide event-tap table. With the table full, no other process can register a global tap: macOS screenshot selection (Cmd+Shift+4) shows the crosshair but ignores the drag, and any hotkey or accessibility utility needing a new tap fails. Quitting Minutes immediately restored the system.

Environment at time of observation

  • Minutes.app v0.19.0 (observed 2026-07-09; not yet re-verified on 0.21.0, but nothing in the 0.20.0 to 0.22.0 changelogs appears to touch tap lifecycle)
  • macOS 26 (Tahoe), Apple Silicon
  • App had been resident since roughly 11:47 AM; the failure was noticed later the same day

Evidence

Enumerating the tap table (no sudo needed) showed every slot owned by the minutes-app pid:

import CoreGraphics
var taps = [CGEventTapInformation](repeating: CGEventTapInformation(), count: 512)
var count: UInt32 = 0
CGGetEventTapList(512, &taps, &count)
var byPid: [pid_t: Int] = [:]
for t in taps.prefix(Int(count)) { byPid[t.tappingProcess, default: 0] += 1 }
for (pid, n) in byPid.sorted(by: { $0.value > $1.value }) { print(pid, n) }
  • Before quit: 512 taps, all owned by minutes-app (pid 658), all listen-only, all reported disabled
  • Killing screencaptureui did not help (it cannot register its selection tap while the table is full)
  • After quitting Minutes: table dropped to 8 healthy system taps and Cmd+Shift+4 worked again

Hypothesis

A keyboard/hotkey (or similar global input) listener re-registers a new tap on some recurring event without releasing the previous one, so taps accumulate over long uptime until the table is exhausted. The taps being disabled suggests the old ones are turned off but never CFMachPortInvalidated / released.

Why it matters

The blast radius is system-wide, not app-local: screenshots and other apps' global input break, and users will debug TCC/permissions long before suspecting a background meeting recorder. A per-registration release (or reuse of a single long-lived tap) should fix it. A quick self-check with more than about 20 taps owned by one pid is a reliable leak signal.

Happy to run diagnostics the next time it recurs, including on 0.21.0+.

jmh1313 avatar Jul 17 '26 02:07 jmh1313

Excellent, high-signal report @jmh1313 — root-caused, and it's a real system-wide-impact leak. The math even matches your "512+ disabled taps" exactly.

The dictation hotkey's own tap is fine (created once, re-enabled correctly on kCGEventTapDisabledByTimeout). The leak is elsewhere:

Trigger — a real tap created every 15s: spawn_permission_monitor (tauri/src-tauri/src/commands.rs:480) polls permission_rows() every PERMISSION_MONITOR_INTERVAL = 15s. For users who have granted Input Monitoring, input_monitoring_row() (crates/core/src/macos_permissions.rs:214) calls input_monitoring_runtime_issue() → probe_hotkey_monitor() → CGEventTapCreate (crates/core/src/hotkey_macos.rs:378). So a brand-new kCGHIDEventTap is created every 15 seconds purely to answer a passive status poll.

Teardown defect — the taps are never freed: run_event_tap cleanup (hotkey_macos.rs:437-440) does CGEventTapEnable(false) + CFRelease but never calls CFMachPortInvalidate(tap) (it isn't even declared in the FFI block). Disabling + releasing a CFMachPort does not remove a CGEventTapCreate port from the system tap table — the canonical teardown must invalidate the Mach port first. So each probe leaves a disabled orphan. 512 orphans × 15s ≈ 2.1h of uptime to exhaust the 512-slot table, after which any tap consumer — screenshots/Screen Recording included — fails system-wide. Matches your symptom precisely.

Fix (two parts):

  1. Don't create a real tap on the passive poll — read Input Monitoring state via the tap-free CGPreflightListenEventAccess(), and reserve the active "can I actually start a tap" probe for explicit user-initiated diagnostics, not the 15s loop. (Removes ~5,760 tap creations/day.)
  2. Add CFMachPortInvalidate(tap) to run_event_tap's teardown (and declare it in the FFI block) so no tap can leak even from the register/unregister churn.

Scope is the Input-Monitoring-granted (Caps Lock / fn dictation) cohort, which is consistent with your report. We'll ship this — thank you for the precise diagnosis, it made the trace fast.

silverstein avatar Jul 17 '26 05:07 silverstein