Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash when used together with Electron #24

Open
stefansundin opened this issue Sep 28, 2022 · 2 comments · May be fixed by #51
Open

Crash when used together with Electron #24

stefansundin opened this issue Sep 28, 2022 · 2 comments · May be fixed by #51
Labels

Comments

@stefansundin
Copy link

stefansundin commented Sep 28, 2022

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.

Screen Shot 2022-09-28 at 15 01 52

The crash is happening on this line:

uv_mutex_destroy(&hook_running_mutex);

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

@marcelblum
Copy link

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.

https://www.electronjs.org/docs/latest/api/system-preferences#systempreferencesistrustedaccessibilityclientprompt-macos

@SnosMe
Copy link
Owner

SnosMe commented Oct 7, 2022

PRs welcome, I don't own Mac.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants