node-hid / node-hid

Access USB & Bluetooth HID devices through Node.js
BSD 3-Clause "New" or "Revised" License
1.45k stars 280 forks source link

Electron App sometimes crash when reading async #540

Open theudebart opened 2 months ago

theudebart commented 2 months ago

In an Electron App i'm using the node-hid library to read hid data from a device. In the App there is a button to open / close the connection. I'm using the Async API. Most of the time the app crashes during opening or closing. Sometimes (but not very often) everything works and i receive data and can close the device gracefully.

What i was able to figure out ist that the error only occurs if an event listener to the data event is registered. If there is no device.on('data', (data) => {console.log(data)}); then the device can be opened and closed without crashes. As soon as the event listener is registered the app crashes at start on button click or it opens correctly, outputs data and crashes on the button click to close the device.

Error on open / after registering a event listener to the data event:

../../third_party/electron_node/src/node_api.cc:1319:napi_status napi_call_threadsafe_function(napi_threadsafe_function, void *, napi_threadsafe_function_call_mode): Assertion `(func) != nullptr' failed.
 1: 0x11180e7ac node::Buffer::New(v8::Isolate*, char*, unsigned long) [../node_modules/electron/dist/Electron.app/Contents/Frameworks/Electron Framework.framework/Versions/A/Electron Framework]
 2: 0x11180e434 node::Buffer::New(v8::Isolate*, char*, unsigned long) [../node_modules/electron/dist/Electron.app/Contents/Frameworks/Electron Framework.framework/Versions/A/Electron Framework]
 3: 0x1117d24b4 napi_call_threadsafe_function [../node_modules/electron/dist/Electron.app/Contents/Frameworks/Electron Framework.framework/Versions/A/Electron Framework]
 4: 0x128a811c0 ReadHelper::start(Napi::Env, Napi::Function) [../node_modules/node-hid/prebuilds/HID-darwin-arm64/node-napi-v3.node]
 5: 0x18c19ef94 _pthread_start [/usr/lib/system/libsystem_pthread.dylib]
 6: 0x18c199d34 thread_start [/usr/lib/system/libsystem_pthread.dylib]

Error on close:

libc++abi: terminating due to uncaught exception of type std::__1::system_error: thread::join failed: No such process

I'm not sure if this is related to #538 and https://github.com/electron/electron/issues/33868 as mentioned by @oalfroukh in https://github.com/node-hid/node-hid/issues/538#issuecomment-2028603268. I'm also not using a child window.

I'm using electron v26.2.1, node v18.18.0 on an Apple Silicon M1 with prebuilt node-hid in an development environment.

I'm hoping for some help, as i am stuck here.

Julusian commented 2 months ago

This sounds like the crash that https://github.com/node-hid/node-hid/pull/533 fixes, perhaps the build in my npm fork will resolve this for you? https://www.npmjs.com/package/@julusian/hid

theudebart commented 2 months ago

I will try this and keep you updated. As i understand I need to rebuild the node module because the changes are in the c code. As of now i don't get it to compile... Also i need to look into how to require a repository, so far i only used npm, but i will figure this out and update here.

Julusian commented 2 months ago

my fork on npm includes precompiled binaries for this library, its a drop in replacement. If you're using yarn, you can setup the dependency/resolution in your package.json with "node-hid": "npm:@julusian/hid@3.0.1-0". Other package managers are probably similar, but I havent checked

theudebart commented 2 months ago

Thank you for the help, this was easy to replace. Unfortunately this does not resolve my problem. I still get the same error. Only difference i notice is that it now always fails on opening.

theudebart commented 1 month ago

Hi, do you have any other recommendation that i could try? I'm a little bit helpless here, and need hid support for my application.

Julusian commented 1 month ago

I don't have any ideas currently. Could you make a small reproduction project that I can try and debug the issue with?

theudebart commented 1 month ago

I stripped my project down to isolate the problem. Here is a repo with the necessary code. I tested this project and get the same error if I open a hid device that can be opened.

https://github.com/theudebart/electron-hid-test-erb-issue-540

aronbraun commented 1 month ago

I had encountered the same issue with the async API, and using the fork from @Julusian's resolved the problem. Is there any way to expedite https://github.com/node-hid/node-hid/issues/538?