Sometimes hammerspoon hangs. Is there an easy way to profile our config?
I've noticed some inconsistent hanging/slowness with hammerspoon where it does not respond and I will notice that the menubar tray icon is beachballing. There are times when I've killed it and times when it only crunches on something for about a second or two before responding. And then it mysteriously stops and starts working normally. Today it was doing that, i restarted it once it gave me control, upon restarting it was still being slow, but then it started not being slow by itself shortly after that.
One easy but not great way to "profile" it is to place prints in the config and try to see which areas we got to by watching the console, but when we have freezing like this, it's often not even possible to bring up the console as neither the hotkey I set up to do it will be working nor will the menu be responsive on account of it being beachballed. But I will try next time it happens maybe I can bring it up and put the console on always on top mode and see if it can be useful this way.
But are there other viable approaches?
I'm using
local socket = require "socket"
local function dump(o)
if type(o) == 'table' then
local s = '{ '
for k,v in pairs(o) do
if type(k) ~= 'number' then k = '"'..k..'"' end
s = s .. '['..k..'] = ' .. dump(v) .. ','
end
return s .. '} '
else
return tostring(o)
end
end
_G.l = function(...)
local args = {...}
local log_file_path = "/tmp/lua-hs.log"
local log_file = io.open(log_file_path, "a")
if log_file == nil then
print("Could not open log file: " .. log_file_path)
return
end
io.output(log_file)
io.write(socket.gettime() .. " >>> ")
for i, payload in ipairs(args) do
local ty = type(payload)
if ty == "table" then
io.write(string.format("%d -> %s\n", i, dump(payload)))
elseif ty == "function" then
io.write(string.format("%d -> [function]\n", i))
else
io.write(string.format("%d -> %s\n", i, payload))
end
end
io.close(log_file)
end
stuck in the front of my hammerspoon config.
I tried posix.clock_gettime, but that method somehow doesnt exist when i fetch luaposix with luarocks, so i gave up on that.
I found some hanging behavior around here
local function findNeovideInstancesAccessOrder()
print("Listing Neovides by window order...")
l('fNIAO')
local instances = {}
local orderedWindows = hs.window.orderedWindows()
l('fNIAO 1')
for _, window in ipairs(orderedWindows) do
l('fNIAO 2 ' .. window:title())
local app = window:application();
if app:bundleID() == 'com.neovide.neovide' then
table.insert(instances, app)
end
end
return instances
end
but the sluggishness went away after i added these logs so i will have to wait until it gets sluggish again to continue troubleshooting this.
local socket = require "socket"
Any reason you're not using a built-in Hammerspoon extension?
local orderedWindows = hs.window.orderedWindows()
My guess is SOMETHING on your system is blocking/breaking the Accessibility API. Can you try force quit everything except Hammerspoon?
the issue doesnt really happen frequently enough to be a problem. I suspect the largest hanging thing is hs.window.orderedWindows() but I havent been able to confirm it before it started being fast again.
Any reason you're not using a built-in Hammerspoon extension?
Could you elaborate on which built in extension would provide millisecond or better precision?
I have same issue, i only use hs and more like local appWatcher = hs.application.watcher.new(applicationWatcher)
mostly hs.eventtap.keyStroke and hs.hotkey.bind to hs.execute to run scripts.
hammerspon hangs on startup and sometime in middle of usage.
Updated: solved in #3748