call hid_close(libusb/hid.c) stuck on android
as the title show, I use hidapi (libusb/hid.c) to communicate with my usb hid device on android, sometimes, I call hid_close will stuck, It stuck at
hidapi_thread_join(&dev->thread_state);
and when stucking, The "read_thread" thread is running the below loop:
while (!dev->transfer_loop_finished)
libusb_handle_events_completed(usb_context, &dev->transfer_loop_finished);
and not jump out。 sometimes, It works fine . In this situation, call hid_close , the read_callback fuction with transfer->status = LIBUSB_TRANSFER_CANCELLE is been called. But when cause this stuck problem, the read_callback fuction with transfer->status = LIBUSB_TRANSFER_CANCELLE is not been called!
anyone cause the same problem? thanks a lot.
@pengxianheng-nreal
How do you use hidapi under Android? What is the Android device you use for testing?
Take note there are limitations on how to use hidapi under un-rooted Android devices.
- https://github.com/libusb/hidapi/issues/178#issuecomment-931252311
@mcuee The way I use hidapi on android is the same with https://github.com/libusb/hidapi/issues/178#issuecomment-931252311. use java to get fd, and pass to native hidapi,and call hid_libusb_wrap_sys_device;
Can you verify that you're not trying to call hid_close concurently with any other HIDAPI function?
Can I assume you're using latest master/latest release of HIDAPI?
@pengxianheng-nreal
Any updates? Thanks.
@mcuee I use https://github.com/libusb/hidapi/pull/457/commits to fix my problem. thanks. my code is older than master/latest
but now,I cause the new cause . even if I use https://github.com/libusb/hidapi/pull/457/commits. also encouter stuck when call hid_close on android. and before stuck , I use hid_write always return -1. and the errno is 9(Bad file descriptor). and after a few seconds usb device has been plugged out. and then I call hid_close stuck . I found it stuck at hidapi_thread_join(&dev->thread_state); in libusb/hid.c . I don't know why. @mcuee
Can you verify that you're not trying to call
hid_closeconcurently with any other HIDAPI function? Can I assume you're using latestmaster/latest release of HIDAPI?
maybe will call hid_write when call hid_close. is this not right calling logic?
HIDAPI is not thread-safe. You definitely shouldn't call both hid_write and hid_close concurently. That is undefined behavior.