fix(hook): break the macOS run loop on a stop flag, not CFRunLoopStop alone#263
Merged
Conversation
… alone CFRunLoopStop is a no-op when it lands in the gap between the hook thread's 500ms run_in_mode slices: the shutdown signal is dropped, the loop re-enters a fresh slice, and stop()'s thread.join() blocks forever with the event tap still live at the HID location. Add an AtomicBool checked at the top of every slice. stop() now sets the flag before calling run_loop.stop(), so shutdown always terminates: promptly via the CF stop while a slice is running, otherwise within one 500ms slice via the flag. Mirrors the Linux backend's stop-flag pattern.
AprilNEA
force-pushed
the
fix/macos-hook-stop-race
branch
from
July 18, 2026 07:53
4ecfed4 to
e0fd699
Compare
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.
Problem
macos::stop()relies solely onCFRunLoopStopto terminate the hook thread. ButCFRunLoopStopis a no-op unless the run loop is currently running: it only sets the stop flag whenrl->_currentModeis non-null. The hook thread services the tap in 500 msrun_in_modeslices and, between slices, runshas_accessibility()(a TCC IPC) andtap.enable().If
stop()fires in that between-slices gap, the CF stop is dropped, nothing records the request, the thread re-enters a fresh 500 ms slice, andstop()'sthread.join()blocks forever — with theCGEventTapstill live at the HID location. The window is small (a few ms out of every 500 ms) but the failure mode is a permanent hang on hook teardown.Fix
Add an
AtomicBoolstop flag, checked at the top of every run-loop slice — mirroring the Linux backend's existing stop-flag pattern.stop()now sets the flag before callingrun_loop.stop(), so shutdown always terminates:run_in_mode):run_loop.stop()wakes it immediately → returnsStopped→ breaks, ~0 ms;Either way the loop exits and falls through to the existing
disable_tap(), so the tap is always detached on teardown.Relaxedordering suffices: the flag carries no other data, andthread.join()provides the final synchronisation.Testing
cargo clippy -p openlogi-hook --all-targets -- -D warnings— clean.Not unit-tested: the path is FFI/timing-bound (a real repro needs Accessibility + a live
CGEventTap), and the crate has no macOS unit tests today. A flag-only assertion would be tautological. The change is small and verifiable by inspection; happy to extract the slice-loop's termination decision behind a seam if a regression test is wanted.