Skip to content

hotkey-helper: watchdog exits when its parent is gone (#443) - #460

Open
Nabzx wants to merge 1 commit into
mainfrom
hotkey-helper-watchdog-443
Open

Nabzx wants to merge 1 commit into
mainfrom
hotkey-helper-watchdog-443

Conversation

@Nabzx

@Nabzx Nabzx commented Sep 27, 2026

Copy link
Copy Markdown
Owner

Closes #443.

The bug

All four resident helpers are spawned with plain spawn(...) - no detached option. Graceful termination works fine: SIGTERM/SIGINT/SIGHUP route through app.quit() → will-quit, which stops every helper cleanly. SIGKILL (Force Quit, kill -9, a hard native crash) can't be caught, so that path never runs - and what happens next depends on an incidental protocol difference. accessibility-helper and transcription-helper both run while let line = readLine(...), so the parent dying closes the stdin pipe's write end and they exit naturally. hotkey-helper never reads stdin - it calls CFRunLoopRun(), which never returns on its own - so it used to survive indefinitely, still holding the Input Monitoring CGEventTap. A relaunch then spawns a second instance, racing two taps on every keystroke.

The fix

Design decision from the two the issue laid out: a lightweight watchdog, per the issue's own suggested approach, rather than restructuring how Electron spawns/groups the four helpers. hotkey-helper now captures its parent's pid at startup and polls getppid() every 2 seconds on the same run loop that already drives the tap; once the pid no longer matches (macOS reparents an orphan to launchd almost immediately), it logs and exits.

Tests

main.swift is OS-glue with no existing unit coverage (same as the rest of this file - HotkeyMatcherTests covers the pure decision logic in the HotkeyMatcher target, not this executable). swift build --package-path native/hotkey-helper is clean.

Went a step further than a bare compile check: this sandbox can't grant Input Monitoring, so the real CGEventTap path is untestable here - but the watchdog logic itself doesn't depend on the tap. Built a standalone binary with the identical getppid()/Timer/CFRunLoopAddTimer/CFRunLoopRun shape, spawned it as a child of a throwaway parent, kill -9'd the parent, and confirmed it detected the reparent (ppid → 1) and exited within one poll cycle. That's real evidence the mechanism works on this OS, not just that it compiles - what's left needs the actual tap and a real Electron parent, checklist in docs/testing/hotkey-helper-watchdog-443.md.

🤖 Generated with Claude Code

accessibility-helper and transcription-helper both sit in a readLine()
command loop, so a SIGKILL'd parent closes their stdin pipe and they
exit on their own. hotkey-helper never reads stdin at all -
CFRunLoopRun() never returns on its own - so a hard kill of the
Electron parent (Force Quit, kill -9, a native crash) used to leave it
running forever, still holding the Input Monitoring tap. A relaunch
would then race a second tap against it on every keystroke.

Now polls getppid() every 2 seconds and exits once it no longer
matches the pid captured at startup - macOS reparents an orphan to
launchd almost immediately, so this is a reliable signal without
needing a pipe of our own.

Verified the polling mechanism itself against a real kill -9 this
session, isolated from the CGEventTap (which needs Input Monitoring
permission this sandbox doesn't have): a standalone binary with the
identical getppid()/Timer/CFRunLoopRun shape, spawned as a child and
orphaned by killing its parent, detected the reparent and exited
within one poll cycle. swift build is clean. Everything that needs the
real tap and a real Electron parent is in
docs/testing/hotkey-helper-watchdog-443.md.

Claude-Session: https://claude.ai/code/session_0144sxo5CJiyk99vaR7sgjuj
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.

hotkey-helper can be orphaned on a hard kill, still holding the Input Monitoring tap

1 participant