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

Most upvoted comments

That’s because appWatcher is local so it goes out of scope and gets garbage collected. You can either remove local or, as you’ve done, reference it somewhere else like in your global reloadConfig.

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?