Skip to content

One hotkey failing to register at startup disables all hotkeys, Stop included #144

Closed

Description

@TMHSDigital

Summary

If RegisterHotKey fails for any binding during startup (for example F6 is taken by another program), the error callback raises on the hotkey thread before Tk's mainloop is running, the thread exits, and no global hotkey works for the whole session, including Stop and Emergency stop.

Where

  • autoclicker/app/hotkeys.py:243-250 (_loop calls _apply() outside the try)
  • autoclicker/app/hotkeys.py:231-232 (_apply calls on_error)
  • autoclicker/gui/main_window.py:141-143 (on_error goes through _ui, start() runs before mainloop)
  • autoclicker/gui/main_window.py:337-343 (_ui calls root.after directly)

Details

    def _loop(self) -> None:
        ...
        self._apply()          # not inside try/finally
        self._ready.set()
        try:
            while self._user32.GetMessageW(...) > 0:

_apply reports failed keys through on_error, which is lambda msg: self._ui(self._set_status_message, msg, "error"). _ui calls self.root.after(0, ...) from the hotkey thread. With threaded Tcl, a Tk call from another thread before the main thread enters mainloop waits about 1 s and then raises RuntimeError: main thread is not in main loop. The exception escapes _apply, so _ready is never set, the message loop never starts, and _thread_id stays set to a dead thread. Later set_running posts go nowhere.

Impact

The documented stop keys silently do not exist for that session, and the "Hotkey unavailable" message is lost. Startup also stalls about 2 s on _ready.wait. This removes one of the three ways to stop a run.

Steps to reproduce

  1. From another process, register F6 globally (for example with a small script calling RegisterHotKey(None, 1, 0, 0x75))
  2. Launch the app, press Start with the button
  3. Press F7 or Esc: nothing happens and no error is shown

Suggested fix

Catch exceptions from on_error inside _apply and move the initial _apply() inside the try. Make _ui safe from worker threads at any time, for example by putting callbacks on a queue.Queue that a main-thread after loop drains, so worker threads never call Tk. Add a test where on_error raises and assert the loop still runs.

Acceptance criteria

  • A failed registration at startup leaves the other hotkeys working
  • The "Hotkey unavailable" status is shown once the window is up
  • Unit test: on_error raising does not stop the hotkey loop

Getting started

  • Relevant files: autoclicker/app/hotkeys.py, autoclicker/gui/main_window.py, tests/test_hotkeys.py
  • How to run tests: make check (Git Bash) or tasks.bat lint, tasks.bat typecheck, tasks.bat test (cmd). Single test: python -m pytest tests/test_x.py::TestClass::test_name --no-cov
  • Related docs: CONTRIBUTING.md, docs/ARCHITECTURE.md

Filed by repo-audit/full-audit at 9968dc8. Severity: high. Effort: S. audit-key: bug|autoclicker/app/hotkeys.py|HotkeyManager._loop

Activity

  1. added
    bug
    priority: high
    safety
    on Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugpriority: highsafety

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions