SnosMe / uiohook-napi

MIT License
162 stars 37 forks source link

Freezes after one key, mouse Input #23

Open ghost opened 1 year ago

ghost commented 1 year ago

In the simplest form of `const { uIOhook, UiohookKey } = require("uiohook-napi");

uIOhook.on("keydown", (e) => { if (e.keycode === UiohookKey.Q) { console.log("Hello!"); }

if (e.keycode === UiohookKey.Escape) { process.exit(0); } });

uIOhook.start(); `

The application lib is still running but after getting one key and outputting "hello" it stops working without an error message.

Mac OS ARM 12.5.1 strangely two months ago it worked without any problems but tested now back to version 1.3 and it still doesn't work. Not sure how to debug this since no output or sigterm but will try to look if I find something and close the ticket if it is only a problem occurring on my device

poernahi commented 1 year ago

Having the same issue with version 1.5.0, intel Mac. OSX version 11.6.8. The on keydown callback is only called once.

upuxaa commented 1 year ago

After I click somewhere on my mouse, the mousemove event stops

kyle-wendling commented 1 year ago

Same issue on intel Mac

upscripter commented 1 year ago

Same issue with M1 Mac (macOS 13.3.1)

0x11-dev commented 1 year ago

Same issue with intel mac 13.4.1 (22F82), node v16.20.0

zaneschepke commented 11 months ago

I know this is in go, but I found that this project works on mac if anyone is interested.

https://github.com/robotn/gohook

marcelblum commented 4 months ago

Using uiohook-napi on Mac in an Electron renderer process for a while now and I never ran into this issue until now after updating my Electron version. Using Electron >=25 brings the issue. Using Electron <=24 avoids it. M1 Mac running macOS 14. Bug does not occur if using uiohook-napi in the main process only in renderer. Edit: Also observed on an older Intel Mac running macOS 12 and again, only if using Electron 25+.

nevoxx commented 4 months ago

I have the same issue on macOS 14, even without Electron.

So i tried to debug for a bit, and it seems the error occurs on this line in libuiohook: https://github.com/kwhat/libuiohook/blob/1.2/src/darwin/input_hook.c#L280 (for keyboard key presses)

If i comment out this line it works on macOS 14.

The interesting thing is, that when i run the libuiohook C demo it works on macos without any changes.

So maybe the difference is that in node it is not running in the main loop? But i'm not really sure how the C lib is working internally.

Maybe this can be of help to further analyze the issue.

0x11-dev commented 3 months ago

The freezing issue occurs due to a deadlock caused by the dispatch_sync_f(dispatch_get_main_queue(), XX, XX) call. To resolve this, we need to make a copy of the CGEventRef using CGEventRef eventCopy = CGEventCreateCopy(event); instead of using a getter from the main thread.

The call graph is as follows:

    2562 Thread_21998699   DispatchQueue_1: com.apple.main-thread  (serial)
    + 2562 start  (in dyld) + 1942  [0x7ff8001b93a6]
    +   2562 node::Start(int, char**)  (in node) + 239  [0x10a9abfcf]
    +     2562 node::NodeMainInstance::Run()  (in node) + 115  [0x10aa206d3]
    +       2562 node::NodeMainInstance::Run(int*, node::Environment*)  (in node) + 97  [0x10aa20a51]
    +         2562 node::SpinEventLoop(node::Environment*)  (in node) + 291  [0x10a918743]
    +           2562 uv_run  (in node) + 417  [0x10b385ba1]
    +             2562 uv__io_poll  (in node) + 932  [0x10b398994]
    +               2562 kevent  (in libsystem_kernel.dylib) + 10  [0x7ff80050772e]
    2562 Thread_21998739
    + 2562 thread_start  (in libsystem_pthread.dylib) + 15  [0x7ff80053dbab]
    +   2562 _pthread_start  (in libsystem_pthread.dylib) + 99  [0x7ff800542202]
    +     2562 node::WorkerThreadsTaskRunner::DelayedTaskScheduler::Run()  (in node) + 361  [0x10aa49529]
    +       2562 uv_run  (in node) + 417  [0x10b385ba1]
    +         2562 uv__io_poll  (in node) + 932  [0x10b398994]
    +           2562 kevent  (in libsystem_kernel.dylib) + 10  [0x7ff80050772e]
    2562 Thread_21998740
    + 2562 thread_start  (in libsystem_pthread.dylib) + 15  [0x7ff80053dbab]
    +   2562 _pthread_start  (in libsystem_pthread.dylib) + 99  [0x7ff800542202]
    +     2562 node::(anonymous namespace)::PlatformWorkerThread(void*)  (in node) + 379  [0x10aa4668b]
    +       2562 node::TaskQueue<v8::Task>::BlockingPop()  (in node) + 72  [0x10aa49748]
    +         2562 uv_cond_wait  (in node) + 9  [0x10b3933e9]
    +           2562 _pthread_cond_wait  (in libsystem_pthread.dylib) + 1211  [0x7ff80054276b]
    +             2562 __psynch_cvwait  (in libsystem_kernel.dylib) + 10  [0x7ff80050560e]
    2562 Thread_21998741
    + 2562 thread_start  (in libsystem_pthread.dylib) + 15  [0x7ff80053dbab]
    +   2562 _pthread_start  (in libsystem_pthread.dylib) + 99  [0x7ff800542202]
    +     2562 node::(anonymous namespace)::PlatformWorkerThread(void*)  (in node) + 379  [0x10aa4668b]
    +       2562 node::TaskQueue<v8::Task>::BlockingPop()  (in node) + 72  [0x10aa49748]
    +         2562 uv_cond_wait  (in node) + 9  [0x10b3933e9]
    +           2562 _pthread_cond_wait  (in libsystem_pthread.dylib) + 1211  [0x7ff80054276b]
    +             2562 __psynch_cvwait  (in libsystem_kernel.dylib) + 10  [0x7ff80050560e]
    2562 Thread_21998742
    + 2562 thread_start  (in libsystem_pthread.dylib) + 15  [0x7ff80053dbab]
    +   2562 _pthread_start  (in libsystem_pthread.dylib) + 99  [0x7ff800542202]
    +     2562 node::(anonymous namespace)::PlatformWorkerThread(void*)  (in node) + 379  [0x10aa4668b]
    +       2562 node::TaskQueue<v8::Task>::BlockingPop()  (in node) + 72  [0x10aa49748]
    +         2562 uv_cond_wait  (in node) + 9  [0x10b3933e9]
    +           2562 _pthread_cond_wait  (in libsystem_pthread.dylib) + 1211  [0x7ff80054276b]
    +             2562 __psynch_cvwait  (in libsystem_kernel.dylib) + 10  [0x7ff80050560e]
    2562 Thread_21998743
    + 2562 thread_start  (in libsystem_pthread.dylib) + 15  [0x7ff80053dbab]
    +   2562 _pthread_start  (in libsystem_pthread.dylib) + 99  [0x7ff800542202]
    +     2562 node::(anonymous namespace)::PlatformWorkerThread(void*)  (in node) + 379  [0x10aa4668b]
    +       2562 node::TaskQueue<v8::Task>::BlockingPop()  (in node) + 72  [0x10aa49748]
    +         2562 uv_cond_wait  (in node) + 9  [0x10b3933e9]
    +           2562 _pthread_cond_wait  (in libsystem_pthread.dylib) + 1211  [0x7ff80054276b]
    +             2562 __psynch_cvwait  (in libsystem_kernel.dylib) + 10  [0x7ff80050560e]
    2562 Thread_21998756
    + 2562 thread_start  (in libsystem_pthread.dylib) + 15  [0x7ff80053dbab]
    +   2562 _pthread_start  (in libsystem_pthread.dylib) + 99  [0x7ff800542202]
    +     2562 node::inspector::(anonymous namespace)::StartIoThreadMain(void*)  (in node) + 19  [0x10aab25a3]
    +       2562 uv_sem_wait  (in node) + 16  [0x10b3939e0]
    +         2562 semaphore_wait_trap  (in libsystem_kernel.dylib) + 10  [0x7ff8005029ea]
    2562 Thread_21998757
    + 2562 thread_start  (in libsystem_pthread.dylib) + 15  [0x7ff80053dbab]
    +   2562 _pthread_start  (in libsystem_pthread.dylib) + 99  [0x7ff800542202]
    +     2562 worker  (in node) + 89  [0x10b381549]
    +       2562 uv_cond_wait  (in node) + 9  [0x10b3933e9]
    +         2562 _pthread_cond_wait  (in libsystem_pthread.dylib) + 1211  [0x7ff80054276b]
    +           2562 __psynch_cvwait  (in libsystem_kernel.dylib) + 10  [0x7ff80050560e]
    2562 Thread_21998758
    + 2562 thread_start  (in libsystem_pthread.dylib) + 15  [0x7ff80053dbab]
    +   2562 _pthread_start  (in libsystem_pthread.dylib) + 99  [0x7ff800542202]
    +     2562 worker  (in node) + 89  [0x10b381549]
    +       2562 uv_cond_wait  (in node) + 9  [0x10b3933e9]
    +         2562 _pthread_cond_wait  (in libsystem_pthread.dylib) + 1211  [0x7ff80054276b]
    +           2562 __psynch_cvwait  (in libsystem_kernel.dylib) + 10  [0x7ff80050560e]
    2562 Thread_21998759
    + 2562 thread_start  (in libsystem_pthread.dylib) + 15  [0x7ff80053dbab]
    +   2562 _pthread_start  (in libsystem_pthread.dylib) + 99  [0x7ff800542202]
    +     2562 worker  (in node) + 89  [0x10b381549]
    +       2562 uv_cond_wait  (in node) + 9  [0x10b3933e9]
    +         2562 _pthread_cond_wait  (in libsystem_pthread.dylib) + 1211  [0x7ff80054276b]
    +           2562 __psynch_cvwait  (in libsystem_kernel.dylib) + 10  [0x7ff80050560e]
    2562 Thread_21998760
    + 2562 thread_start  (in libsystem_pthread.dylib) + 15  [0x7ff80053dbab]
    +   2562 _pthread_start  (in libsystem_pthread.dylib) + 99  [0x7ff800542202]
    +     2562 worker  (in node) + 89  [0x10b381549]
    +       2562 uv_cond_wait  (in node) + 9  [0x10b3933e9]
    +         2562 _pthread_cond_wait  (in libsystem_pthread.dylib) + 1211  [0x7ff80054276b]
    +           2562 __psynch_cvwait  (in libsystem_kernel.dylib) + 10  [0x7ff80050560e]
    2562 Thread_21998821
    + 2562 thread_start  (in libsystem_pthread.dylib) + 15  [0x7ff80053dbab]
    +   2562 _pthread_start  (in libsystem_pthread.dylib) + 99  [0x7ff800542202]
    +     2562 hook_thread_proc  (in node.napi.node) + 109  [0x10f81194d]  uiohook_worker.c:108
    +       2562 hook_run  (in node.napi.node) + 1048  [0x10f813a68]  input_hook.c:1389
    +         2562 CFRunLoopRun  (in CoreFoundation) + 40  [0x7ff800696d9e]
    +           2562 CFRunLoopRunSpecific  (in CoreFoundation) + 557  [0x7ff80061b352]
    +             2562 __CFRunLoopRun  (in CoreFoundation) + 2700  [0x7ff80061c3a6]
    +               2562 __CFRunLoopDoSource1  (in CoreFoundation) + 534  [0x7ff80061d72e]
    +                 2562 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__  (in CoreFoundation) + 41  [0x7ff80061d7f7]
    +                   2562 __CFMachPortPerform  (in CoreFoundation) + 238  [0x7ff80064bb1c]
    +                     2562 eventTapMessageHandler(__CFMachPort*, void*, long, void*)  (in SkyLight) + 151  [0x7ff805cd3795]
    +                       2562 _XPostEventTapData  (in SkyLight) + 290  [0x7ff805f0daf9]
    +                         2562 processEventTapData(void*, unsigned int, unsigned int, unsigned int, unsigned char*, unsigned int)  (in SkyLight) + 598  [0x7ff805cd3a49]
    +                           2562 hook_event_proc  (in node.napi.node) + 2469  [0x10f812a05]  input_hook.c:0
    +                             2562 process_key_pressed  (in node.napi.node) + 418  [0x10f812eb2]  input_hook.c:286
    +                               2562 _dispatch_sync_f_slow  (in libdispatch.dylib) + 175  [0x7ff8003a72ef]
    +                                 2562 __DISPATCH_WAIT_FOR_QUEUE__  (in libdispatch.dylib) + 307  [0x7ff8003a76c3]
    +                                   2562 _dispatch_thread_event_wait_slow  (in libdispatch.dylib) + 40  [0x7ff80039ac3a]
    +                                     2562 _dlock_wait  (in libdispatch.dylib) + 46  [0x7ff80039adb2]
    +                                       2562 __ulock_wait  (in libsystem_kernel.dylib) + 10  [0x7ff800504222]
    2562 Thread_21998851
      2562 start_wqthread  (in libsystem_pthread.dylib) + 15  [0x7ff80053db97]
        2562 _pthread_wqthread  (in libsystem_pthread.dylib) + 416  [0x7ff80053eca0]
          2562 __workq_kernreturn  (in libsystem_kernel.dylib) + 10  [0x7ff800504192]

Total number in stack (recursive counted multiple, when >=5):
        11       _pthread_start  (in libsystem_pthread.dylib) + 99  [0x7ff800542202]
        11       thread_start  (in libsystem_pthread.dylib) + 15  [0x7ff80053dbab]
        8       __psynch_cvwait  (in libsystem_kernel.dylib) + 0  [0x7ff800505604]
        8       _pthread_cond_wait  (in libsystem_pthread.dylib) + 1211  [0x7ff80054276b]
        8       uv_cond_wait  (in node) + 9  [0x10b3933e9]

Sort by top of stack, same collapsed (when >= 5):
        __psynch_cvwait  (in libsystem_kernel.dylib)        20496
        kevent  (in libsystem_kernel.dylib)        5124
        __ulock_wait  (in libsystem_kernel.dylib)        2562
        __workq_kernreturn  (in libsystem_kernel.dylib)        2562
        semaphore_wait_trap  (in libsystem_kernel.dylib)        2562