ruabmbua / hidapi-rs

Rust bindings for the hidapi C library
MIT License
167 stars 80 forks source link

Crash rescanning for devices on macOS M1 #85

Closed timfish closed 2 years ago

timfish commented 2 years ago

I'm currently unsure if this is a Mac M1 issue with hidapi (related issue here) or just as likely it's caused by something unsupported I'm doing with threads. I'm currently calling hidapi-rs via napi-rs from node.js in Electron 😬. The first open always succeeds and subsequent calls crash the process.

Is there anything in particular that I should be carful about regarding which thread I should be calling hidapi-rs from?

image

image
ruabmbua commented 2 years ago

Are you maybe opening the same device multiple times? There is an issue in hidapi where opening the same device multiple times can result in a crash.

Regarding threads:

I can`t really give any guarantees about this, I do not know how the different backends in the C library act when called from different threads. Its better to run hidapi in one thread, and use e.g. message passing with channels to serve other threads.

Also, I just checked, and it seems HidApi is at least Send, I think I am going to fix that to make it not send-able to a different thread.

timfish commented 2 years ago

Are you maybe opening the same device multiple times

For testing, the devices I'm using have no programmed serial number so I can only open with vid/pid.

However, it crashes on the 2nd attempt to open a device, regardless of whether it found a device on the first search so I'm guessing it's not related to that issue.

Its better to run hidapi in one thread, and use e.g. message passing with channels to serve other threads

Thanks, that is what I'll try next and report back!

timfish commented 2 years ago

Confirmed that everything works without crashes if all calls to hidapi come from a single thread!