Hammerspoon: Problems after coroutine & modernise hs.application/window/uielement merge

Created on 8 Apr 2020  路  44Comments  路  Source: Hammerspoon/hammerspoon

@cmsj & @asmagill - I have now merged the latest Hammerspoon master into CommandPost-App. The first issue I've come across is:

2020-04-08 17:21:54: *** ERROR: ...ost.app/Contents/Resources/extensions/hs/window/init.lua:947: attempt to index a nil value (global 'uielement')
stack traceback:
    ...ost.app/Contents/Resources/extensions/hs/window/init.lua:947: in metamethod '__index'
    ...rishocking/Github/CommandPost/src/extensions/cp/init.lua:40: in main chunk
    [C]: in function 'xpcall'
    ...Github/CommandPost/src/extensions/hs/_coresetup/init.lua:794: in function 'hs._coresetup.setup'
    (...tail calls...)

All 44 comments

We've also seemed to have lost hs.window.timeout().

Another one:

2020-04-08 17:33:18: ********
2020-04-08 17:33:18: 17:33:18 ERROR:   LuaSkin: hs.timer callback error: ...ost.app/Contents/Resources/extensions/hs/window/init.lua:515: ERROR: incorrect type 'no value' for argument 2 (expected boolean)
stack traceback:
    [C]: in method '_bringtofront'
    ...ost.app/Contents/Resources/extensions/hs/window/init.lua:515: in method 'focus'
    ...mandPost/src/plugins/finalcutpro/fullscreen/dockicon.lua:53: in function <...mandPost/src/plugins/finalcutpro/fullscreen/dockicon.lua:52>
2020-04-08 17:33:18: ********

@cmsj, the current branch includes changes to the underlying userdata for hs.window and hs.application, doesn't it?

Assuming this is so, since I'm seeing breakage with some of my own stuff that uses hs._asm.axuieleemnt, @latenitefilms I'm going to need to update axuielement, since it makes assumptions about the structures behind the userdata for these elements... it was easier than requiring hs._asm.axuielement to be recompiled everytime Hammerspoon was rebuilt.

I'll take a closer look at it later today.

@asmagill - Yes, it does - although I haven't spotted any hs._asm.axuielement errors yet.

Some other minor @stickler-ci things:

  • [ ] Can remove tinsert from here.
  • [ ] uielement doesn't exist here?

@asmagill - I'm wondering if now is a good time to just merge hs._asm.axuielement into Hammerspoon's master, either undocumented, or clearly documented that it's in beta and subject to change?

@asmagill it does, yeah. At the moment it doesn't do the equivalent of https://github.com/Hammerspoon/hammerspoon/blob/0.9.78/extensions/application/internal.m#L1257 and I'm not entirely sure if we want/need that. I thought we had that so that hs.application/window objects could be treated like hs.uielement objects, but if I understand the old code correctly, all it actually adds is these methods: https://github.com/Hammerspoon/hammerspoon/blob/0.9.78/extensions/uielement/internal.m#L388 and I think I'd rather re-implement those for each module than have them be weirdly intertwined like they were before.

Awesome, thanks @cmsj ! That's fixed a bunch of errors!

One error I'm now seeing a lot of is hs.application.frontmostApplication() returning nil, when it shouldn't ever really return nil - any ideas?

@latenitefilms could you merge b079110d into CP? Should help us narrow down where the nil is coming from, since there are multiple places it can happen.

Talking out loud... here's what the old code used to be:

/// hs.application.frontmostApplication() -> hs.application object
/// Function
/// Returns the application object for the frontmost (active) application.  This is the application which currently receives input events.
///
/// Parameters:
///  * None
///
/// Returns:
///  * An hs.application object
static int application_frontmostapplication(lua_State* L) {
    NSRunningApplication* runningApp = [[NSWorkspace sharedWorkspace] frontmostApplication];
    if (runningApp) {
        if (!new_application(L, [runningApp processIdentifier])) {
            lua_pushnil(L);
        }
    } else {
        lua_pushnil(L);
    }
return 1;
}

Here's what the new code is:

+(HSapplication *)frontmostApplication {
    HSapplication *frontmostApp = nil;

    NSRunningApplication *runningApp = [[NSWorkspace sharedWorkspace] frontmostApplication];
    if (runningApp) {
        frontmostApp = [HSapplication applicationForNSRunningApplication:runningApp];
    }
    return frontmostApp;
}

...so it seems like the original hs.application.frontmostApplication() can technically return nil, but I never saw it happen. With this new code, I'm seeing it happen a lot, because I never allowed for it to return nil in any of our functions.

...just saw your last post. Will compile b079110 into CommandPost-App now. Stay tuned.

Will keep playing, but just spotted:

2020-04-08 22:46:34: ********
2020-04-08 22:46:34: 22:46:34 ERROR:   LuaSkin: Unable to fetch NSRunningApplication for pid: -1
2020-04-08 22:46:34: ********
2020-04-08 22:46:36: ********
2020-04-08 22:46:36: 22:46:36 ERROR:   LuaSkin: Unable to fetch NSRunningApplication for pid: -1
2020-04-08 22:46:36: ********

@cmsj - Ok, so all the errors seem to be the same now:

2020-04-08 22:46:34: ********
2020-04-08 22:46:34: 22:46:34 ERROR:   LuaSkin: Unable to fetch NSRunningApplication for pid: -1
2020-04-08 22:46:34: ********
2020-04-08 22:46:36: ********
2020-04-08 22:46:36: 22:46:36 ERROR:   LuaSkin: Unable to fetch NSRunningApplication for pid: -1
2020-04-08 22:46:36: ********
2020-04-08 22:49:11: ********
2020-04-08 22:49:11: 22:49:11 ERROR:   LuaSkin: Unable to fetch NSRunningApplication for pid: -1
2020-04-08 22:49:11: ********
2020-04-08 22:49:11: ********
2020-04-08 22:49:11: 22:49:11 ERROR:   LuaSkin: Unable to fetch NSRunningApplication for pid: -1
2020-04-08 22:49:11: ********
2020-04-08 22:49:23: ********
2020-04-08 22:49:23: 22:49:23 ERROR:   LuaSkin: Unable to fetch NSRunningApplication for pid: -1
2020-04-08 22:49:23: ********
2020-04-08 22:49:23: ********
2020-04-08 22:49:23: 22:49:23 ERROR:   LuaSkin: Unable to fetch NSRunningApplication for pid: -1
2020-04-08 22:49:23: ********

@asmagill - Also, you're right, your fancy coroutine getMenuItems() function is no longer working using the latest Hammerspoon Master branch.

@latenitefilms It's strange - we're definitely bouncing between NSRunningApplication and a pid more than is needed, and it's certainly the case that not all NSRunningApplication's have a pid, but the old code shouldn't have worked for them either.

I just added 9c9ad75b which might at least point us in the direction of which application is considered frontmost, but I'm currently at a loss for why this is failing now when it wasn't before.

can you give me more info about the corouting getMenuItems() failure? I did make sure that all of the menu tests (including the "async" one) were passing, but perhaps I broke something more fundamental in the merge.

Will test out 9c9ad75 now.

Here's the code I'm using, however it might not mean much to you, so probably best to refer to @asmagill's original code here. Keep in mind though that it's using hs._asm.axuielement - so it's probably a bug in there, rather than in your changes.

@cmsj - I'm not seeing any new errors messages, same errors:

2020-04-08 23:07:35: ********
2020-04-08 23:07:35: 23:07:35 ERROR:   LuaSkin: Unable to fetch NSRunningApplication for pid: -1
2020-04-08 23:07:35: ********
2020-04-08 23:07:36: ********
2020-04-08 23:07:36: 23:07:36 ERROR:   LuaSkin: Unable to fetch NSRunningApplication for pid: -1
2020-04-08 23:07:36: ********
2020-04-08 23:07:46: ********
2020-04-08 23:07:46: 23:07:46 ERROR:   LuaSkin: Unable to fetch NSRunningApplication for pid: -1
2020-04-08 23:07:46: ********
2020-04-08 23:07:47: ********
2020-04-08 23:07:47: 23:07:47 ERROR:   LuaSkin: Unable to fetch NSRunningApplication for pid: -1
2020-04-08 23:07:47: ********

@asmagill since we seem to have gone into a little nexus of significant refactoring, how do you feel about us doing a push to integrate axuielement? :D

@cmsj - I'm going to try this code, to see if I can work out which app is failing:

+(HSapplication *)frontmostApplication {
    LuaSkin *skin = [LuaSkin sharedWithState:NULL];
    HSapplication *frontmostApp = nil;

    NSRunningApplication *runningApp = [[NSWorkspace sharedWorkspace] frontmostApplication];
    [skin logError:[NSString stringWithFormat:@"HSapplication::frontmostApplication failed for app: %@", runningApp.localizedName]];
    if (runningApp) {
        frontmostApp = [HSapplication applicationForNSRunningApplication:runningApp];
        if (!frontmostApp) {
            [skin logError:[NSString stringWithFormat:@"HSapplication::frontmostApplication failed for app: %@", runningApp.localizedName]];
        }
    } else {
        [skin logError:@"Unable to fetch frontmost application"];
    }
    return frontmostApp;
}

@cmsj - Ok, so it seems to be failing when switching from another app to CommandPost. Could CommandPost (or Hammerspoon) be blocking itself somehow?

2020-04-08 23:17:30: ********
2020-04-08 23:17:30: 23:17:30 ERROR:   LuaSkin: HSapplication::frontmostApplication failed for app: Safari
2020-04-08 23:17:30: ********
2020-04-08 23:17:30: ********
2020-04-08 23:17:30: 23:17:30 ERROR:   LuaSkin: HSapplication::frontmostApplication failed for app: Safari
2020-04-08 23:17:30: ********
2020-04-08 23:17:30: ********
2020-04-08 23:17:30: 23:17:30 ERROR:   LuaSkin: HSapplication::frontmostApplication failed for app: Safari
2020-04-08 23:17:30: ********
2020-04-08 23:17:35: -- Loading extension: hs.application
2020-04-08 23:17:35: ********
2020-04-08 23:17:35: 23:17:35 ERROR:   LuaSkin: Unable to fetch NSRunningApplication for pid: -1
2020-04-08 23:17:35: ********
2020-04-08 23:17:36: ********
2020-04-08 23:17:36: 23:17:36 ERROR:   LuaSkin: Unable to fetch NSRunningApplication for pid: -1
2020-04-08 23:17:36: ********
2020-04-08 23:17:36: ********
2020-04-08 23:17:36: 23:17:36 ERROR:   LuaSkin: HSapplication::frontmostApplication failed for app: CommandPost
2020-04-08 23:17:36: ********
2020-04-08 23:17:36: ********
2020-04-08 23:17:36: 23:17:36 ERROR:   LuaSkin: HSapplication::frontmostApplication failed for app: CommandPost
2020-04-08 23:17:36: ********
2020-04-08 23:17:36: ********
2020-04-08 23:17:36: 23:17:36 ERROR:   LuaSkin: HSapplication::frontmostApplication failed for app: CommandPost
2020-04-08 23:17:36: ********

Ummm, just got different errors this time:

2020-04-09 00:09:33: ********
2020-04-09 00:09:33: 00:09:33 ERROR:   LuaSkin: Unable to fetch NSRunningApplication for pid: 24457
2020-04-09 00:09:33: ********
2020-04-09 00:09:33: ********
2020-04-09 00:09:33: 00:09:33 ERROR:   LuaSkin: hs.application.watcher callback: ...ing/Github/CommandPost/src/extensions/cp/ui/notifier.lua:64: attempt to index a nil value (local 'app')
stack traceback:
    ...ing/Github/CommandPost/src/extensions/cp/ui/notifier.lua:64: in function <...ing/Github/CommandPost/src/extensions/cp/ui/notifier.lua:63>
2020-04-09 00:09:33: ********
2020-04-09 00:09:33: ********
2020-04-09 00:09:33: 00:09:33 ERROR:   LuaSkin: Unable to fetch NSRunningApplication for pid: 24457
2020-04-09 00:09:33: ********
2020-04-09 00:09:33: ********
2020-04-09 00:09:33: 00:09:33 ERROR:   LuaSkin: hs.application.watcher callback: ...hrishocking/Github/CommandPost/src/extensions/cp/app.lua:1241: attempt to index a nil value (local 'hsApp')
stack traceback:
    ...hrishocking/Github/CommandPost/src/extensions/cp/app.lua:1241: in function <...hrishocking/Github/CommandPost/src/extensions/cp/app.lua:1240>
2020-04-09 00:09:33: ********
2020-04-09 00:09:33: ********
2020-04-09 00:09:33: 00:09:33 ERROR:   LuaSkin: Unable to fetch NSRunningApplication for pid: 24457
2020-04-09 00:09:33: ********
2020-04-09 00:09:33: ********
2020-04-09 00:09:33: 00:09:33 ERROR:   LuaSkin: hs.application.watcher callback: .../Github/CommandPost/src/plugins/core/menu/menuaction.lua:156: attempt to index a nil value (local 'app')
stack traceback:
    .../Github/CommandPost/src/plugins/core/menu/menuaction.lua:156: in function <.../Github/CommandPost/src/plugins/core/menu/menuaction.lua:141>
2020-04-09 00:09:33: ********
2020-04-09 00:09:33: ********
2020-04-09 00:09:33: 00:09:33 ERROR:   LuaSkin: Unable to fetch NSRunningApplication for pid: 24457
2020-04-09 00:09:33: ********
2020-04-09 00:09:33: ********
2020-04-09 00:09:33: 00:09:33 ERROR:   LuaSkin: Unable to fetch NSRunningApplication for pid: 24457
2020-04-09 00:09:33: ********
2020-04-09 00:10:09: -- Loading extension: hs.application

> hs.application.applicationForPID(24457)
2020-04-09 00:10:15: ********
2020-04-09 00:10:15: 00:10:15 ERROR:   LuaSkin: Unable to fetch NSRunningApplication for pid: 24457
2020-04-09 00:10:15: ********
nil

Here's a screen recording so you can see when the errors are happening:

https://latenitefilms.digitalpigeon.com/msg/7T0pIHmjEeqrzAb4tvG4vQ/0Ha4_vj2gLJ5raW-NFCBng

Some more errors:

2020-04-09 00:53:54: ********
2020-04-09 00:53:54: 00:53:54 ERROR:   LuaSkin: Unable to fetch NSRunningApplication for pid: -1
2020-04-09 00:53:54: ********
2020-04-09 00:53:54: ********
2020-04-09 00:53:54: 00:53:54 ERROR:   LuaSkin: Unable to fetch NSRunningApplication for pid: -1
2020-04-09 00:53:54: ********
2020-04-09 00:54:41: ********
2020-04-09 00:54:41: 00:54:41 ERROR:   LuaSkin: Unable to fetch NSRunningApplication for pid: -1
2020-04-09 00:54:41: ********
2020-04-09 00:54:41: ********
2020-04-09 00:54:41: 00:54:41 ERROR:   LuaSkin: Unable to fetch NSRunningApplication for pid: -1
2020-04-09 00:54:41: ********
2020-04-09 00:55:06: ********
2020-04-09 00:55:06: 00:55:06 ERROR:   LuaSkin: Unable to fetch NSRunningApplication for pid: 40463
2020-04-09 00:55:06: ********
2020-04-09 00:55:06: ********
2020-04-09 00:55:06: 00:55:06 ERROR:   LuaSkin: Unable to fetch NSRunningApplication for pid: -1
2020-04-09 00:55:06: ********
2020-04-09 00:55:06: ********
2020-04-09 00:55:06: 00:55:06 ERROR:   LuaSkin: hs.application.watcher callback: ...ing/Github/CommandPost/src/extensions/cp/ui/notifier.lua:64: attempt to index a nil value (local 'app')
stack traceback:
    ...ing/Github/CommandPost/src/extensions/cp/ui/notifier.lua:64: in function <...ing/Github/CommandPost/src/extensions/cp/ui/notifier.lua:63>
2020-04-09 00:55:06: ********
2020-04-09 00:55:06: ********
2020-04-09 00:55:06: 00:55:06 ERROR:   LuaSkin: Unable to fetch NSRunningApplication for pid: -1
2020-04-09 00:55:06: ********
2020-04-09 00:55:06: ********
2020-04-09 00:55:06: 00:55:06 ERROR:   LuaSkin: hs.application.watcher callback: ...hrishocking/Github/CommandPost/src/extensions/cp/app.lua:1241: attempt to index a nil value (local 'hsApp')
stack traceback:
    ...hrishocking/Github/CommandPost/src/extensions/cp/app.lua:1241: in function <...hrishocking/Github/CommandPost/src/extensions/cp/app.lua:1240>
2020-04-09 00:55:06: ********
2020-04-09 00:55:06: ********
2020-04-09 00:55:06: 00:55:06 ERROR:   LuaSkin: Unable to fetch NSRunningApplication for pid: -1
2020-04-09 00:55:06: ********
2020-04-09 00:55:06: ********
2020-04-09 00:55:06: 00:55:06 ERROR:   LuaSkin: Unable to fetch NSRunningApplication for pid: -1
2020-04-09 00:55:06: ********
2020-04-09 00:55:06: ********
2020-04-09 00:55:06: 00:55:06 ERROR:   LuaSkin: Unable to fetch NSRunningApplication for pid: -1
2020-04-09 00:55:06: ********
2020-04-09 00:55:06: ********
2020-04-09 00:55:06: 00:55:06 ERROR:   LuaSkin: Unable to fetch NSRunningApplication for pid: 40496
2020-04-09 00:55:06: ********
2020-04-09 00:55:06: ********
2020-04-09 00:55:06: 00:55:06 ERROR:   LuaSkin: hs.application.watcher callback: ...ing/Github/CommandPost/src/extensions/cp/ui/notifier.lua:64: attempt to index a nil value (local 'app')
stack traceback:
    ...ing/Github/CommandPost/src/extensions/cp/ui/notifier.lua:64: in function <...ing/Github/CommandPost/src/extensions/cp/ui/notifier.lua:63>
2020-04-09 00:55:06: ********
2020-04-09 00:55:06: ********
2020-04-09 00:55:06: 00:55:06 ERROR:   LuaSkin: Unable to fetch NSRunningApplication for pid: 40496
2020-04-09 00:55:06: ********
2020-04-09 00:55:06: ********
2020-04-09 00:55:06: 00:55:06 ERROR:   LuaSkin: hs.application.watcher callback: ...hrishocking/Github/CommandPost/src/extensions/cp/app.lua:1241: attempt to index a nil value (local 'hsApp')
stack traceback:
    ...hrishocking/Github/CommandPost/src/extensions/cp/app.lua:1241: in function <...hrishocking/Github/CommandPost/src/extensions/cp/app.lua:1240>
2020-04-09 00:55:06: ********
2020-04-09 00:55:06: ********
2020-04-09 00:55:06: 00:55:06 ERROR:   LuaSkin: Unable to fetch NSRunningApplication for pid: 40496
2020-04-09 00:55:06: ********
2020-04-09 00:55:06: ********
2020-04-09 00:55:06: 00:55:06 ERROR:   LuaSkin: hs.application.watcher callback: .../Github/CommandPost/src/plugins/core/menu/menuaction.lua:156: attempt to index a nil value (local 'app')
stack traceback:
    .../Github/CommandPost/src/plugins/core/menu/menuaction.lua:156: in function <.../Github/CommandPost/src/plugins/core/menu/menuaction.lua:141>
2020-04-09 00:55:06: ********
2020-04-09 00:55:06: ********
2020-04-09 00:55:06: 00:55:06 ERROR:   LuaSkin: Unable to fetch NSRunningApplication for pid: 40496
2020-04-09 00:55:06: ********
2020-04-09 00:55:06: ********
2020-04-09 00:55:06: 00:55:06 ERROR:   LuaSkin: Unable to fetch NSRunningApplication for pid: 40496
2020-04-09 00:55:06: ********

> hs.application.applicationForPID(40496)
2020-04-09 00:55:53: ********
2020-04-09 00:55:53: 00:55:53 ERROR:   LuaSkin: Unable to fetch NSRunningApplication for pid: 40496
2020-04-09 00:55:53: ********
nil

> hs.application.applicationForPID(40463)
hs.application: Final Cut Pro (0x6000005542c8)

For those playing at home, 32cd3cb seems to have solved the errors listed above. Woohoo!

Now the only thing left to do I think is to get hs._asm.axuielement playing nicely with everything again.

Thanks heaps @cmsj & @asmagill !!

To be clear, I didn't fix anything, I just switched some things from initialising HSapplication via PID when they had an NSRunningApplication. Something is clearly still wrong with the PID initialisation.

(but I can't trigger the PID failure unless I feed it an obviously incorrect PID, so it would be super helpful to have a small reproducer, if anyone is able to do that)

@latenitefilms try the latest at https://github.com/asmagill/hs._asm.axuielement

@cmsj I'm not quite ready to integrate axuielement, but with a little luck will be this weekend.

Thanks @asmagill - just merged in hs._asm.axuielement 0.7.4.

My existing code still doesn't seem to be working - so will have to do some more digging to work out why.

Also, just got this crash which I haven't seen before:

Process:               CommandPost [48438]
Path:                  /Users/USER/*/CommandPost.app/Contents/MacOS/CommandPost
Identifier:            org.latenitefilms.CommandPost
Version:               1.0.7-beta.6 (6015)
Code Type:             X86-64 (Native)
Parent Process:        ??? [1]
Responsible:           CommandPost [48438]
User ID:               501

Date/Time:             2020-04-09 09:42:13.174 +1000
OS Version:            Mac OS X 10.14.6 (18G3020)
Report Version:        12
Bridge OS Version:     3.0 (14Y908)
Anonymous UUID:        15304943-7B8E-F62C-F19F-B9B548D6D5BC

Sleep/Wake UUID:       C959A4DF-DA7F-44B0-A6F4-7A02A127BDDF

Time Awake Since Boot: 720000 seconds
Time Since Wake:       2300 seconds

System Integrity Protection: enabled

Crashed Thread:        0  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_CRASH (SIGABRT)
Exception Codes:       0x0000000000000000, 0x0000000000000000
Exception Note:        EXC_CORPSE_NOTIFY

Application Specific Information:
Assertion failed: (((n) < (L->top - L->ci->func)) && "not enough elements in the stack"), function luaD_precall, file /Users/chrishocking/Github/CommandPost-App/LuaSkin/lua-5.3.5/src/ldo.c, line 453.


Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   libsystem_kernel.dylib          0x00007fff5c51e2c2 __pthread_kill + 10
1   libsystem_pthread.dylib         0x00007fff5c5d9bf1 pthread_kill + 284
2   libsystem_c.dylib               0x00007fff5c488745 __abort + 144
3   libsystem_c.dylib               0x00007fff5c4886b5 abort + 142
4   libsystem_c.dylib               0x00007fff5c45120d __assert_rtn + 324
5   org.hammerspoon.LuaSkin         0x0000000105d1de8b luaD_precall.cold.1 + 35
6   org.hammerspoon.LuaSkin         0x0000000105d129ba luaD_precall + 983 (ldo.c:512)
7   org.hammerspoon.LuaSkin         0x0000000105d0ea3c luaV_execute + 708 (lvm.c:1134)
8   org.hammerspoon.LuaSkin         0x0000000105d12e08 unroll + 72 (ldo.c:568)
9   org.hammerspoon.LuaSkin         0x0000000105d14221 luai_objcttry + 28 (lobjectivec_exceptions.m:173)
10  org.hammerspoon.LuaSkin         0x0000000105d12b31 lua_resume + 232 (ldo.c:681)
11  org.hammerspoon.LuaSkin         0x0000000105d0a9ca auxresume + 85 (lcorolib.c:40)
12  org.hammerspoon.LuaSkin         0x0000000105d0a79d luaB_coresume + 43 (lcorolib.c:61)
13  org.hammerspoon.LuaSkin         0x0000000105d1288c luaD_precall + 681 (ldo.c:453)
14  org.hammerspoon.LuaSkin         0x0000000105d0ea3c luaV_execute + 708 (lvm.c:1134)
15  org.hammerspoon.LuaSkin         0x0000000105d129fa luaD_call + 64 (ldo.c:517)
16  org.hammerspoon.LuaSkin         0x0000000105d12a3b luaD_callnoyield + 21 (ldo.c:527)
17  org.hammerspoon.LuaSkin         0x0000000105d14221 luai_objcttry + 28 (lobjectivec_exceptions.m:173)
18  org.hammerspoon.LuaSkin         0x0000000105d12f38 luaD_pcall + 110 (ldo.c:746)
19  org.hammerspoon.LuaSkin         0x0000000105d0c688 lua_pcallk + 236
20  org.hammerspoon.LuaSkin         0x0000000105cf54cb -[LuaSkin protectedCallAndTraceback:nresults:] + 218 (Skin.m:332)
21  internal.so                     0x000000010aefb458 -[HSTimer callback:] + 244 (internal.m:51)
22  com.apple.Foundation            0x00007fff327067cb __NSFireTimer + 80
23  com.apple.CoreFoundation        0x00007fff30477810 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
24  com.apple.CoreFoundation        0x00007fff304773bc __CFRunLoopDoTimer + 851
25  com.apple.CoreFoundation        0x00007fff30476f02 __CFRunLoopDoTimers + 330
26  com.apple.CoreFoundation        0x00007fff30458112 __CFRunLoopRun + 2130
27  com.apple.CoreFoundation        0x00007fff3045766e CFRunLoopRunSpecific + 455
28  com.apple.HIToolbox             0x00007fff2f6b61ab RunCurrentEventLoopInMode + 292
29  com.apple.HIToolbox             0x00007fff2f6b5ee5 ReceiveNextEventCommon + 603
30  com.apple.HIToolbox             0x00007fff2f6b5c76 _BlockUntilNextEventMatchingListInModeWithFilter + 64
31  com.apple.AppKit                0x00007fff2da4d77d _DPSNextEvent + 1135
32  com.apple.AppKit                0x00007fff2da4c46b -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1361
33  com.apple.AppKit                0x00007fff2da46588 -[NSApplication run] + 699
34  com.apple.AppKit                0x00007fff2da35ac8 NSApplicationMain + 777
35  libdyld.dylib                   0x00007fff5c3e33d5 start + 1

@asmagill - Unfortunately, with hs._asm.axuielement 0.7.4 I just get a crash pretty much straight away when I load CommandPost now, so hard to test.

Crash report is on CommandPost's Fabric too if helpful.

Yeah, just got one myself, but haven't had a chance to review the crash log yet... Ok, well, it's getting there, just in fits and spurts...

Ok, @latenitefilms check out my latest pull request to Hammerspoon and the most recent push to axuielement and let me know how it goes.

Basically, HSuicore creates three classes for hs.application, hs.window, and hs.uielement but then allows some of the methods to be invoked from lua functions/methods rather than just through callbacks. It's those which can be invoked by lua functions/methods that require state to be passed, so I've added that to the necessary methods. It should be reviewed more to see if others need to be adjusted as well, but it passes my current usage of the three modules as well as my usage of hs._asm.axuielement.

Awesome, thank you! Building now.

Woohoo! That fixed it!

However, now I'm seeing this crash:

Process:               CommandPost [53520]
Path:                  /Users/USER/*/CommandPost.app/Contents/MacOS/CommandPost
Identifier:            org.latenitefilms.CommandPost
Version:               1.0.7-beta.6 (6017)
Code Type:             X86-64 (Native)
Parent Process:        ??? [1]
Responsible:           CommandPost [53520]
User ID:               501

Date/Time:             2020-04-09 12:33:52.054 +1000
OS Version:            Mac OS X 10.14.6 (18G3020)
Report Version:        12
Bridge OS Version:     3.0 (14Y908)
Anonymous UUID:        15304943-7B8E-F62C-F19F-B9B548D6D5BC

Sleep/Wake UUID:       C959A4DF-DA7F-44B0-A6F4-7A02A127BDDF

Time Awake Since Boot: 730000 seconds
Time Since Wake:       12000 seconds

System Integrity Protection: enabled

Crashed Thread:        0  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_BAD_ACCESS (SIGSEGV)
Exception Codes:       KERN_INVALID_ADDRESS at 0x00002b0308f50b30
Exception Note:        EXC_CORPSE_NOTIFY

Termination Signal:    Segmentation fault: 11
Termination Reason:    Namespace SIGNAL, Code 0xb
Terminating Process:   exc handler [53520]

VM Regions Near 0x2b0308f50b30:
    CoreAnimation          000000011f054000-0000000120896000 [ 24.3M] rw-/rwx SM=PRV  
--> 
    MALLOC_NANO            0000600000000000-0000600018000000 [384.0M] rw-/rwx SM=PRV  

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   libobjc.A.dylib                 0x00007fff5ac0864c objc_release + 28
1   org.latenitefilms.CommandPost   0x0000000109575e21 -[HSwindow dealloc] + 33
2   internal.so                     0x000000010ecad86b userdata_gc + 169
3   org.hammerspoon.LuaSkin         0x00000001098d788c luaD_precall + 681 (ldo.c:453)
4   org.hammerspoon.LuaSkin         0x00000001098d79ee luaD_call + 52 (ldo.c:515)
5   org.hammerspoon.LuaSkin         0x00000001098d7a3b luaD_callnoyield + 21 (ldo.c:527)
6   org.hammerspoon.LuaSkin         0x00000001098d9221 luai_objcttry + 28 (lobjectivec_exceptions.m:173)
7   org.hammerspoon.LuaSkin         0x00000001098d7f38 luaD_pcall + 110 (ldo.c:746)
8   org.hammerspoon.LuaSkin         0x00000001098c926c GCTM + 252 (lgc.c:824)
9   org.hammerspoon.LuaSkin         0x00000001098c9078 runafewfinalizers + 53 (lgc.c:848)
10  org.hammerspoon.LuaSkin         0x00000001098d071e lua_pushlstring + 102 (lapi.c:487)
11  org.hammerspoon.LuaSkin         0x00000001098bea1a -[LuaSkin pushNSObject:withOptions:alreadySeenObjects:] + 1297
12  org.hammerspoon.LuaSkin         0x00000001098c0028 -[LuaSkin pushNSDictionary:withOptions:alreadySeenObjects:] + 534
13  org.hammerspoon.LuaSkin         0x00000001098beab1 -[LuaSkin pushNSObject:withOptions:alreadySeenObjects:] + 1448
14  org.hammerspoon.LuaSkin         0x00000001098c0028 -[LuaSkin pushNSDictionary:withOptions:alreadySeenObjects:] + 534
15  org.hammerspoon.LuaSkin         0x00000001098beab1 -[LuaSkin pushNSObject:withOptions:alreadySeenObjects:] + 1448
16  org.hammerspoon.LuaSkin         0x00000001098c0028 -[LuaSkin pushNSDictionary:withOptions:alreadySeenObjects:] + 534
17  org.hammerspoon.LuaSkin         0x00000001098beab1 -[LuaSkin pushNSObject:withOptions:alreadySeenObjects:] + 1448
18  org.hammerspoon.LuaSkin         0x00000001098c0028 -[LuaSkin pushNSDictionary:withOptions:alreadySeenObjects:] + 534
19  org.hammerspoon.LuaSkin         0x00000001098beab1 -[LuaSkin pushNSObject:withOptions:alreadySeenObjects:] + 1448
20  org.hammerspoon.LuaSkin         0x00000001098c0028 -[LuaSkin pushNSDictionary:withOptions:alreadySeenObjects:] + 534
21  org.hammerspoon.LuaSkin         0x00000001098beab1 -[LuaSkin pushNSObject:withOptions:alreadySeenObjects:] + 1448
22  org.hammerspoon.LuaSkin         0x00000001098c0028 -[LuaSkin pushNSDictionary:withOptions:alreadySeenObjects:] + 534
23  org.hammerspoon.LuaSkin         0x00000001098beab1 -[LuaSkin pushNSObject:withOptions:alreadySeenObjects:] + 1448
24  org.hammerspoon.LuaSkin         0x00000001098bc37c -[LuaSkin pushNSObject:withOptions:] + 119
25  internal.so                     0x000000010ed3e754 json_read + 509
26  org.hammerspoon.LuaSkin         0x00000001098d788c luaD_precall + 681 (ldo.c:453)
27  org.hammerspoon.LuaSkin         0x00000001098d39bf luaV_execute + 583 (lvm.c:1149)
28  org.hammerspoon.LuaSkin         0x00000001098d79fa luaD_call + 64 (ldo.c:517)
29  org.hammerspoon.LuaSkin         0x00000001098d7a3b luaD_callnoyield + 21 (ldo.c:527)
30  org.hammerspoon.LuaSkin         0x00000001098d9221 luai_objcttry + 28 (lobjectivec_exceptions.m:173)
31  org.hammerspoon.LuaSkin         0x00000001098d7f38 luaD_pcall + 110 (ldo.c:746)
32  org.hammerspoon.LuaSkin         0x00000001098d1688 lua_pcallk + 236
33  org.hammerspoon.LuaSkin         0x00000001098ba4cb -[LuaSkin protectedCallAndTraceback:nresults:] + 218 (Skin.m:332)
34  org.hammerspoon.LuaSkin         0x00000001098ba592 -[LuaSkin protectedCallAndError:nargs:nresults:] + 55 (Skin.m:346)
35  watcher.so                      0x000000010ec0d06f -[AppWatcher callback:withEvent:] + 452 (watcher.m:110)
36  watcher.so                      0x000000010ec0d2a4 -[AppWatcher applicationActivated:] + 65 (watcher.m:134)
37  com.apple.CoreFoundation        0x00007fff304bb346 __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12
38  com.apple.CoreFoundation        0x00007fff304bb2c0 ___CFXRegistrationPost_block_invoke + 63
39  com.apple.CoreFoundation        0x00007fff304bb22a _CFXRegistrationPost + 404
40  com.apple.CoreFoundation        0x00007fff304c36d8 ___CFXNotificationPost_block_invoke + 87
41  com.apple.CoreFoundation        0x00007fff3042c064 -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 1642
42  com.apple.CoreFoundation        0x00007fff3042b417 _CFXNotificationPost + 732
43  com.apple.Foundation            0x00007fff326b2a7b -[NSNotificationCenter postNotificationName:object:userInfo:] + 66
44  com.apple.AppKit                0x00007fff2dc9353e applicationStatusSubsystemCallback + 758
45  com.apple.LaunchServices        0x00007fff31a5618f ___LSScheduleNotificationFunction_block_invoke_2 + 47
46  com.apple.CoreFoundation        0x00007fff30494f14 __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
47  com.apple.CoreFoundation        0x00007fff30458637 __CFRunLoopDoBlocks + 394
48  com.apple.CoreFoundation        0x00007fff30458394 __CFRunLoopRun + 2772
49  com.apple.CoreFoundation        0x00007fff3045766e CFRunLoopRunSpecific + 455
50  com.apple.HIToolbox             0x00007fff2f6b61ab RunCurrentEventLoopInMode + 292
51  com.apple.HIToolbox             0x00007fff2f6b5ee5 ReceiveNextEventCommon + 603
52  com.apple.HIToolbox             0x00007fff2f6b5c76 _BlockUntilNextEventMatchingListInModeWithFilter + 64
53  com.apple.AppKit                0x00007fff2da4d77d _DPSNextEvent + 1135
54  com.apple.AppKit                0x00007fff2da4c46b -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1361
55  com.apple.AppKit                0x00007fff2da46588 -[NSApplication run] + 699
56  com.apple.AppKit                0x00007fff2da35ac8 NSApplicationMain + 777
57  libdyld.dylib                   0x00007fff5c3e33d5 start + 1

At the end of the crashlog on your computer, it should list the loaded libraries and their address ranges... which internal.so contains 0x000000010ecad86b? I'm going to guess hs.window, but could you confirm?

(I've occasionally wondered if it would be worth the hassle of going through and uniquely re-naming every internal.m and watcher.m for this very reason, but it would mean changing every modules init.lua, every luaopen_* function, the entire build setup in XCode to match the new names, and probably some tweaks to package.cpath as well...)

Full crash log here: https://gist.github.com/latenitefilms/eabb739a2b5dc5f1ba347753f419f7eb

(I've occasionally wondered if it would be worth the hassle of going through and uniquely re-naming every internal.m and watcher.m for this very reason, but it would mean changing every modules init.lua, every luaopen_* function, the entire build setup in XCode to match the new names, and probably some tweaks to package.cpath as well...)

That is actually a really good idea. Obviously a lot of changes, but I think could definitely be worth thinking about - @cmsj ?

See if 0.7.5.1 for axuielement fixes it... it looks like the axuielement created for HSapplication objects is retained in it's initializers; the initializer for HSwindow assumes it's retained outside (because it usually is with a create or copy method prior to invoking the initializer, rather than hs.axuielement's way of slipping one in it already has the CFobject).

Woohoo! Yep, that seems to have fixed the crashes! You're an absolute genius and a legend. Thank you!

@asmagill - Are you using hs.application.watcher at all? I'm currently using it to detect when I'm changing apps - and it seems a bit slower than previously. Is that expected?

That I couldn't say... I'll take a closer look at it tomorrow and see what examples or tests I can run.

Actually, I have no idea if what I said is accurate or not - it could have always been this speed, and I've never noticed before because I wasn't looking for it - or my system could just be behaving differently. It could also be due to the fact that I have several hs.application.watcher objects in the code, so something in my own code could be slowing things down.

I'm going to close this issue, as I believe, for now, we've solved all the problems that I've run into.

Thanks for all your amazing work @cmsj & @asmagill !

@asmagill yeah I've considered doing The Great Renaming several times in the past couple of years. I think we should, but I think we should be very deliberate about it. We've had more pain than we might have done by landing the coroutines and application/window/uielement changes closely to each other, because they're kinda intertwined a bit.

If we do The Great Renaming, I think we should decide on what the outcome looks like, clear out anything else first that will interfere, and then get it all done.

@cmsj I agree that we should wait a bit before embarking on the great renaming... if you're still serious about locking down v1.0 in the near(ish) future (on even days I think its a good idea, on odd ones I'm not as sure since we're all just contributing "as and when we can"), then maybe make it part of v2?

And @latenitefilms you'll probably also be interested in this: I've added some documentation to the Hammerspoon wiki; first I moved my hs.canvas examples to the main wiki (and added a section in the menu for modules because I think there are some others than deserve examples as well) because I recently discovered that the hs.canvas doc strings still reference the one hosted on my github site... I'll correct this in a future pull, but it's a minor detail so not worthy of its own pull request.

I also added a section in the side menu for Developers and added a main page for my discussions about coroutines. While the old info is still relevant, and probably even important to module developers going forward, the more interesting document is the one about Leveraging Coroutines in Hammerspoon that you can get to from the main Coroutine page. It takes a barebones generic lua function and shows how to rewrite it to leverage coroutine.applicationYield as well as discuss some variants and limitations. I consider it very draftish at the moment, as I haven't tested all of the examples (especially the ones using hs.watchable to pass data in and out) fully yet, so let me know if you find any issues before I do... I'll be thoroughly testing it myself over the next couple of days, but figured even in its early state it would be of interest to the two of you.

Ok, I think I've worked out most of the bugs in my examples... let me know if I've missed anything!

Ok, I'm done tweaking the Leveraging documentation... for some reason it completely slipped my mind when considering callback functions -- they can allow passing new data into the coroutine as well, so I rewrote the section about passing data in and out. I'm done for tonight (on this at least), I promise!

Amazing - thank you! I look forward to reading this all in more detail over the coming days!

Was this page helpful?
0 / 5 - 0 ratings