hammerspoon: `application.watcher` stops watching after several times
With release 0.9.43 I am testing the application.watcher. Depending on complexity/load of the init script, the watcher stops after several executions, hammerspoon still keeps on listening to e.g. keyboard bindings.
Down, an applescript and an init.lua is appended to reproduce the issue.
With my personal configuration the watcher stops is after 10 times switching applications. The problem occurs with the appended scripts after 400 times switching applications (yes 40 seconds to reproduce the bug).
-- applescript
repeat 100 times
tell application "System Events"
delay 0.1
key code 48 using command down -- send cmd + tab
end tell
end repeat
-- init.lua
hs.application.watcher.new(function(name,event, app) appwatch(name,event,app) end):start()
local i = 1
function appwatch(name, event, app)
local win = hs.window.focusedWindow()
if not win then
print("no window focused")
return
end
local lookuptable = {
[hs.application.watcher.activated] = "act",
[hs.application.watcher.deactivated] = "deact",
[hs.application.watcher.hidden] = "hid",
[hs.application.watcher.launched] = "lnchd",
[hs.application.watcher.launching] = "lnchng",
[hs.application.watcher.terminated] = "term",
[hs.application.watcher.unhidden] = "unhid"
}
print(i .. " event: ".. lookuptable[event] .. "; app: " .. hs.window.focusedWindow():application():title())
if event == hs.application.watcher.activated then
i = i+1
end
end
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 17 (13 by maintainers)
Commits related to this issue
- hammerspoon: Make `appWatcher` global After a certain time, the watcher stops working after it gets garbage collected (as per https://github.com/Hammerspoon/hammerspoon/issues/681). Making the varia... — committed to cinaglia/dotfiles by cinaglia 7 years ago
- prevent Hammerspoon watchers to be garbage collected and randomly stops watching Ref: https://github.com/Hammerspoon/hammerspoon/issues/681#issuecomment-212286907 — committed to zetavg/dotfiles by zetavg 6 years ago
- [hammerspoon] use `caffeinateWatcher' as a global variable to avoid the garbage collection. link: https://github.com/Hammerspoon/hammerspoon/issues/681 — committed to gujiaxi/dotfiles by gujiaxi 5 years ago
- Fixes local app bindings\menus not working randomly agzam/spacehammer#31 - Additional logging showed the hs.application.watcher was not responding at random times. - Further research hinted at Hammer... — committed to jaidetree/spacehammer by jaidetree 4 years ago
- Fix the power source watcher not persisting I forgot to assign it to a variable to stop it from getting garbage collected. See: https://github.com/Hammerspoon/hammerspoon/issues/681#issuecomment-2122... — committed to dguo/dotfiles by dguo 3 years ago
That’s because
appWatcheris local so it goes out of scope and gets garbage collected. You can either removelocalor, as you’ve done, reference it somewhere else like in your globalreloadConfig.In the initial example the application watcher was not stored in a variable. Then as mentioned in #774 the watcher will be garbage collected at some point.
@arolle can you verify if the bug still exists if the application watcher is stored in a variable?