hammerspoon icon indicating copy to clipboard operation
hammerspoon copied to clipboard

Sometimes hammerspoon hangs. Is there an easy way to profile our config?

Open unphased opened this issue 1 year ago • 5 comments

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?

unphased avatar Aug 23 '24 20:08 unphased

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.

unphased avatar Aug 28 '24 19:08 unphased

local socket = require "socket"

Any reason you're not using a built-in Hammerspoon extension?

latenitefilms avatar Aug 28 '24 20:08 latenitefilms

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?

latenitefilms avatar Aug 28 '24 20:08 latenitefilms

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?

unphased avatar Aug 29 '24 07:08 unphased

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

doanngochieuo avatar Feb 08 '25 00:02 doanngochieuo