Crash when used together with Electron
stefansundin opened this issue · 2 comments
Hello, thanks for the package.
It works great except for one condition when it is used together with Electron (v21.0.0 tested). On macOS, if the user has not granted the application the Accessibility permission then the application crashes when you call uIOhook.start()
.
I created an Electron Fiddle to make it easy for you to reproduce: https://gist.github.com/stefansundin/937b9b20b28d58bc4d3ff05157d412c5
I included the full crash log in the gist above (scroll to the bottom), but here's the important part:
Thread 0 Crashed:: CrBrowserMain Dispatch queue: com.apple.main-thread
0 libsystem_kernel.dylib 0x19cdced98 __pthread_kill + 8
1 libsystem_pthread.dylib 0x19ce03ee0 pthread_kill + 288
2 libsystem_c.dylib 0x19cd3e340 abort + 168
3 Electron Framework 0x10c74019c uv_mutex_destroy + 28
4 uiohook_napi.node 0x104f51d4c uiohook_worker_start + 148
5 uiohook_napi.node 0x104f51438 AddonStart + 264
6 Electron Framework 0x11247604c napi_is_detached_arraybuffer + 296
7 Electron Framework 0x10d586af8 v8::internal::Accessors::MakeAccessor(v8::internal::Isolate*, v8::internal::Handle<v8::internal::Name>, void (*)(v8::Local<v8::Name>, v8::PropertyCallbackInfo<v8::Value> const&), void (*)(v8::Local<v8::Name>, v8::Local<v8::Value>, v8::PropertyCallbackInfo<v8::Boolean> const&)) + 15136
8 Electron Framework 0x10d586688 v8::internal::Accessors::MakeAccessor(v8::internal::Isolate*, v8::internal::Handle<v8::internal::Name>, void (*)(v8::Local<v8::Name>, v8::PropertyCallbackInfo<v8::Value> const&), void (*)(v8::Local<v8::Name>, v8::Local<v8::Value>, v8::PropertyCallbackInfo<v8::Boolean> const&)) + 14000
9 Electron Framework 0x10d585b40 v8::internal::Accessors::MakeAccessor(v8::internal::Isolate*, v8::internal::Handle<v8::internal::Name>, void (*)(v8::Local<v8::Name>, v8::PropertyCallbackInfo<v8::Value> const&), void (*)(v8::Local<v8::Name>, v8::Local<v8::Value>, v8::PropertyCallbackInfo<v8::Boolean> const&)) + 11112
If you run the application from the terminal then it is the terminal app's permission that takes effect. So if you've already given it accessibility permissions then you may have to revoke that in System Preferences -> Security & Privacy to trigger the crash.
Before the crash this is printed:
hook_run [1407]: Accessibility API is disabled!
The prompt to grant accessibility permissions is shown as expected (see screenshot below), followed immediately by the crash. When run through Electron Fiddle the macOS crash reporter isn't shown.
The crash is happening on this line:
uiohook-napi/src/lib/uiohook_worker.c
Line 179 in 63745a5
I tried commenting that out and it prevents the crash, but of course there may be a memory leak created in that case.
Would love to hear if you have any ideas on how to safely address this problem. Thank you!
Versions: uiohook-napi v1.5.0, Electron v21.0.0, node.js v16.17.0
Not a fix but a workaround would be to use Electron's systemPreferences.isTrustedAccessibilityClient()
to have more control over showing the dialog, and then only do require("uiohook-napi")
once you know the permissions are already granted. This approach has other advantages too since it lets you warn the user with a custom message before seeing the scary ambiguously-worded dialog, and also avoids the occasional issue of needing to restart the app before the perms take effect.
PRs welcome, I don't own Mac.