hidapi icon indicating copy to clipboard operation
hidapi copied to clipboard

call hid_close(libusb/hid.c) stuck on android

Open pengxianheng-nreal opened this issue 2 years ago • 8 comments

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 avatar Jun 29 '23 08:06 pengxianheng-nreal

@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 avatar Jun 30 '23 01:06 mcuee

@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;

pengxianheng-nreal avatar Jun 30 '23 03:06 pengxianheng-nreal

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?

Youw avatar Jul 03 '23 09:07 Youw

@pengxianheng-nreal

Any updates? Thanks.

mcuee avatar Aug 14 '23 11:08 mcuee

@mcuee I use https://github.com/libusb/hidapi/pull/457/commits to fix my problem. thanks. my code is older than master/latest

pengxianheng-nreal avatar May 14 '24 05:05 pengxianheng-nreal

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

pengxianheng-nreal avatar May 14 '24 07:05 pengxianheng-nreal

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?

maybe will call hid_write when call hid_close. is this not right calling logic?

pengxianheng-nreal avatar May 14 '24 07:05 pengxianheng-nreal

HIDAPI is not thread-safe. You definitely shouldn't call both hid_write and hid_close concurently. That is undefined behavior.

Youw avatar May 14 '24 08:05 Youw