Hammerspoon / hammerspoon

Staggeringly powerful macOS desktop automation with Lua
http://www.hammerspoon.org
MIT License
12.11k stars 582 forks source link

hs.hotkey.bind() hotkey get endlessly delayed when there is high CPU usage #3187

Open MagicDuck opened 2 years ago

MagicDuck commented 2 years ago

First of all, thank you for this amazing tool that helps me tremendously every day! The issue that I am running into is as follows: I have a defined some keybindings in my .hammerspoon/init.lua to help with window management. For the sake of easy reproduction, here's the simplest one:

hs.hotkey.bind(
  superKey,
  "a",
  function()
    -- show name of application corresponding to current window
    local win = hs.window.frontmostWindow()
    hs.alert.show(win:application():name())
  end
)

When the system activity is normal, this keybinding is fairly responsive, but when I am running some more intensive operations in the background (jest unit tests that eat up a lot of CPU in my case), the keybinding will appear to not respond at all for a while, until the intensive operation is done, at which point you get all of the stacked up event handlers executing in a row. Other things I've noticed is that Karabiner does not have this problem, executing stuff immediately. Same for Contexts app or just doing CMD+TAB.

I am wondering if there is anything that I can fix on my end to remedy this situation or if this is a hammerspoon issue... I am on MacOS Monterey, but I had observed this problem on previous versions. Thank you and appreciate any help!

cmsj commented 2 years ago

Quite often we see that this is because there is a process running which is not responding to accessibility events properly (they are how all third party window management is achieved on macOS).

Next time you are in that high-load state, without doing one of your hotkeys, run this in the Hammerspoon Console:

hs.window._timed_allWindows()

It should print out any window objects which are taking an abnormally long amount of time to respond. Might help shed some light here.

latenitefilms commented 2 years ago

I'm not entirely sure but you MIGHT be able to reduce the timeout to help too:

hs.window.timeout(value)

@asmagill might be able to confirm?

MagicDuck commented 2 years ago

One thing that I am observing after a reboot is that things work fine even under higher CPU load 🙃 . Reboots fix everything, lol. Thank you both for the suggestions, will definitely try those when I get this problem again.