Hammerspoon / hammerspoon

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

Window filter subscribe is broken on 0.9.79 #2474

Closed folke closed 4 years ago

folke commented 4 years ago

Running the test script below gives an error.

filter = hs.window.filter
filter.setLogLevel(10)
filter.default:subscribe(filter.windowFocused, function() print("foo") end)

Output:

2020-09-20 20:42:24: 20:42:24 wf-default: hs.window.filter.default (0x6000028e4680) instance deleted
2020-09-20 20:42:24:             wfilter: unregistered 0 apps
2020-09-20 20:42:24: -- Lazy extension loading enabled
2020-09-20 20:42:24: -- Loading ~/.hammerspoon/init.lua
2020-09-20 20:42:24: -- Loading extension: window
2020-09-20 20:42:24: -- Loading extensions: window.filter
2020-09-20 20:42:24: 20:42:24 wf-default: new hs.window.filter: wf-default (0x6000028f7840) - empty
2020-09-20 20:42:24:          wf-default: setting filter for Flux: reject
2020-09-20 20:42:24:          wf-default: setting filter for Music Manager: reject
2020-09-20 20:42:24:          wf-default: setting filter for CheatSheet: reject
2020-09-20 20:42:24:          wf-default: setting filter for loginwindow: reject
2020-09-20 20:42:24:          wf-default: setting filter for MenuMetersApp: reject
2020-09-20 20:42:24:          wf-default: setting filter for CursorSense Manager: reject
2020-09-20 20:42:24:          wf-default: setting filter for Google Drive: reject
2020-09-20 20:42:24:          wf-default: setting filter for Alfred 2: reject
2020-09-20 20:42:24:          wf-default: setting filter for CrashPlan menu bar: reject
2020-09-20 20:42:24:          wf-default: setting filter for PopClip: reject
2020-09-20 20:42:24:          wf-default: setting filter for Grandview: reject
2020-09-20 20:42:24:          wf-default: setting filter for Bartender: reject
2020-09-20 20:42:24:          wf-default: setting filter for Notification Center: reject
2020-09-20 20:42:24:          wf-default: setting filter for 1Password mini: reject
2020-09-20 20:42:24:          wf-default: setting filter for SystemPal: reject
2020-09-20 20:42:24:          wf-default: setting filter for Isolator: reject
2020-09-20 20:42:24:          wf-default: setting filter for BetterSnapTool: reject
2020-09-20 20:42:24:          wf-default: setting filter for Radium: reject
2020-09-20 20:42:24:          wf-default: setting filter for MacID: reject
2020-09-20 20:42:24:          wf-default: setting filter for Moom: reject
2020-09-20 20:42:24:          wf-default: setting filter for Dropbox: reject
2020-09-20 20:42:24:          wf-default: setting filter for Colors for Hue: reject
2020-09-20 20:42:24:          wf-default: setting filter for PressAndHold: reject
2020-09-20 20:42:24:          wf-default: setting filter for Spotlight: reject
2020-09-20 20:42:24:          wf-default: setting filter for CornerClickBG: reject
2020-09-20 20:42:24:          wf-default: setting filter for Jettison: reject
2020-09-20 20:42:24:          wf-default: setting filter for ScreenSaverEngine: reject
2020-09-20 20:42:24:          wf-default: setting filter for Hammerspoon: allowRoles={AXStandardWindow}, allowTitles={Preferences,...}, 
2020-09-20 20:42:24:          wf-default: setting default filter: visible=true, 
2020-09-20 20:42:24:          wf-default: default windowfilter instantiated
2020-09-20 20:42:24:                          added callback for event windowFocused
2020-09-20 20:42:24:             wfilter: registering 56 running apps
2020-09-20 20:42:24: *** ERROR: ...n.app/Contents/Resources/extensions/hs/window/filter.lua:1431: attempt to index a nil value (field '_element')
stack traceback:
    ...n.app/Contents/Resources/extensions/hs/window/filter.lua:1431: in upvalue 'startAppWatcher'
    ...n.app/Contents/Resources/extensions/hs/window/filter.lua:1559: in upvalue 'startGlobalWatcher'
    ...n.app/Contents/Resources/extensions/hs/window/filter.lua:1722: in upvalue 'start'
    ...n.app/Contents/Resources/extensions/hs/window/filter.lua:1913: in method 'subscribe'
    /Users/folke/.hammerspoon/test.lua:5: in main chunk
    [C]: in function 'rawrequire'
    ...app/Contents/Resources/extensions/hs/_coresetup/init.lua:651: in function 'require'
    /Users/folke/.hammerspoon/init.lua:4: in main chunk
    [C]: in function 'xpcall'
    ...app/Contents/Resources/extensions/hs/_coresetup/init.lua:702: in function 'hs._coresetup.setup'
    (...tail calls...)

Edit: this is on Big Sur

folke commented 4 years ago

If I replace the checks for watcher._element.pid to watcher:element() and watcher:pid() on line 1431 and line 1390 in extensions/hs/window/filter.lua, then everything starts working again.

However, after that, reloading the config segfaults the hammerpsoon process, but this might be unrelated:

2020-09-20 21:24:42.002 Hammerspoon[70193:1203413] destroyLuaState
ERROR: SentryCrashMachineContext.c (230): void sentrycrashmc_resumeEnvironment(): thread_resume (00008f07): (os/kern) invalid argument
fish: '/Applications/Hammerspoon.app/C…' terminated by signal SIGSEGV (Address boundary error)

Another output:

2020-09-20 21:31:37.529 Hammerspoon[70492:1207876] destroyLuaState
ERROR: SentryCrashCPU.c (58): _Bool sentrycrashcpu_i_fillState(const thread_t, const thread_state_t, const thread_state_flavor_t, const mach_msg_type_number_t): thread_get_state: (ipc/send) invalid destination port
ERROR: SentryCrashCPU.c (58): _Bool sentrycrashcpu_i_fillState(const thread_t, const thread_state_t, const thread_state_flavor_t, const mach_msg_type_number_t): thread_get_state: (ipc/send) invalid destination port
ERROR: SentryCrashMachineContext.c (230): void sentrycrashmc_resumeEnvironment(): thread_resume (ffffffff): (ipc/send) invalid destination port
fish: '/Applications/Hammerspoon.app/C…' terminated by signal SIGSEGV (Address boundary error)
wych42 commented 4 years ago

Same issue on catalina.

asmagill commented 4 years ago

If I'm reading the source correctly, both 1431 and 1390 should be just watcher:pid() but I can confirm that even that results in a crash on reload. I'll dig deeper.

asmagill commented 4 years ago

Any of you who are comfortable building your own Hammerspoon build, please take a look at #2483.

I was able to successfully run the example given above and restart without a crash, but I don't regularly use hs.window.filter or anything that does, so I really don't know what to do to test further.

If you can test this out, please let me know your results. I'll see if I can come up with some other tests on my own as well.

ahaljh commented 4 years ago

Hi. The same thing happens to me after updating hammerspoon.

Hammerspoon Console

2020-09-21 13:27:32: -- Loading extension: window
2020-09-21 13:27:32: -- Loading extensions: window.filter
2020-09-21 13:27:32: *** ERROR: ...n.app/Contents/Resources/extensions/hs/window/filter.lua:1431: attempt to index a nil value (field '_element')
stack traceback:
    ...n.app/Contents/Resources/extensions/hs/window/filter.lua:1431: in upvalue 'startAppWatcher'
    ...n.app/Contents/Resources/extensions/hs/window/filter.lua:1559: in upvalue 'startGlobalWatcher'
    ...n.app/Contents/Resources/extensions/hs/window/filter.lua:1722: in upvalue 'start'
    ...n.app/Contents/Resources/extensions/hs/window/filter.lua:1913: in method 'subscribe'
    /Users/ahaljh/.hammerspoon/init.lua:111: in main chunk
    [C]: in function 'xpcall'
    ...app/Contents/Resources/extensions/hs/_coresetup/init.lua:702: in function 'hs._coresetup.setup'
    (...tail calls...)

part of init.lua that called window.filter

finderHotkey = hs.hotkey.bind({'ctrl', 'cmd'}, 'e', function()
    local curAppName = hs.application.frontmostApplication():name()
    if curAppName ~= "Citrix Viewer" then
        hs.execute('open ~')
    else
        finderHotkey:disable() -- does not work without this, even though it should
        hs.eventtap.keyStroke({'ctrl', 'cmd'}, 'e')
        --hs.timer.doAfter(10, finderHotkey:enable)
    end
end)

--finderHotkey:enable()

hs.window.filter.new('Citrix Viewer')
  :subscribe(hs.window.filter.windowFocused,function() finderHotkey:disable() end)
  :subscribe(hs.window.filter.windowUnfocused,function() finderHotkey:enable() end)
wych42 commented 4 years ago

Any of you who are comfortable building your own Hammerspoon build, please take a look at #2483.

I was able to successfully run the example given above and restart without a crash, but I don't regularly use hs.window.filter or anything that does, so I really don't know what to do to test further.

If you can test this out, please let me know your results. I'll see if I can come up with some other tests on my own as well.

Fix my issue.

I used hs.window.filter to set input method on switch to application:

-- set input methods [[[2

local function Chinese()
    -- hs.keycodes.setMethod("Pinyin - Simplified")
    -- hs.keycodes.currentSourceID("com.googlecode.rimeime.inputmethod.Squirrel.Rime")
    hs.keycodes.currentSourceID("com.baidu.inputmethod.BaiduIM.pinyin")
end

local function English()
    -- hs.keycodes.setLayout("U.S.")
    --   hs.keycodes.currentSourceID("com.apple.keylayout.US")
    hs.keycodes.currentSourceID("com.apple.keylayout.USExtended")
end

local function set_app_input_method(app_name, set_input_method_function, event)
    event = event or hs.window.filter.windowFocused
    wf = hs.window.filter.new(app_name)

    wf:subscribe(
        event,
        function()
            set_input_method_function()
        end
    )
end

set_app_input_method("Hammerspoon", English, hs.window.filter.windowCreated)
set_app_input_method("Alfred", English, hs.window.filter.windowCreated)
folke commented 4 years ago

If you can test this out, please let me know your results.

works!

cmsj commented 4 years ago

This should be fixed in 0.9.80 which has just been released.