Linux: Hanging inside hid_close() #142

Closed
opened 2020-02-01 05:59:34 -06:00 by vozhyk-com · 3 comments
vozhyk-com commented 2020-02-01 05:59:34 -06:00 (Migrated from github.com)

For catching this hanging need just open device by hid_open() or hid_open_path() and close it by hid_close(), see simple example:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "hidapi.h"

int main(int argc, char* argv[])
{
hid_device *handle;
struct hid_device_info *devs, *cur_dev;

 if (hid_init())
      return -1; 
 int i;
 for (i =0 ; i < 10000; i++) { 
      handle = hid_open(0x22c9, 0x0901, NULL);
      if (!handle) {
           printf("unable to open device\n");
           return 1;
      }
      printf("iteration: %d, going to call hid_close() \n", i); 
      hid_close(handle);
      printf("iteration: %d, hid_close() done \n", i); 
 }   
 /* Free static HIDAPI objects. */
 hid_exit();
 return 0;

}

For catching this hanging need just open device by hid_open() or hid_open_path() and close it by hid_close(), see simple example: #include <stdio.h> #include <string.h> #include <stdlib.h> #include "hidapi.h" int main(int argc, char* argv[]) { hid_device *handle; struct hid_device_info *devs, *cur_dev; if (hid_init()) return -1; int i; for (i =0 ; i < 10000; i++) { handle = hid_open(0x22c9, 0x0901, NULL); if (!handle) { printf("unable to open device\n"); return 1; } printf("iteration: %d, going to call hid_close() \n", i); hid_close(handle); printf("iteration: %d, hid_close() done \n", i); } /* Free static HIDAPI objects. */ hid_exit(); return 0; }
Youw commented 2020-09-20 03:11:42 -05:00 (Migrated from github.com)

I wasn't able to reproduce the original issue, presumably because I don't have a device that would allow me to do so.
I believe there is a race condition, that when hit, may lead to deadlock, when trying to close the device, in case if device submits transfers/interruptions frequently, and device is being closed exactly during successful handling of such transfer.

The fix suggested in #143 kind-of fixes the issue. Kind-of, because it constantly tries not to hit a deadlock, but a race condition is still there, it is a root cause, and we better fix a root cause, not symptoms.

@vozhyk-com would you be able to check a patch I prepared, with your device? https://github.com/libusb/hidapi/tree/libusb-hang

I wasn't able to reproduce the original issue, presumably because I don't have a device that would allow me to do so. I believe there is a race condition, that when hit, may lead to deadlock, when trying to close the device, in case if device submits transfers/interruptions frequently, and device is being closed exactly during successful handling of such transfer. The fix suggested in #143 kind-of fixes the issue. Kind-of, because it constantly tries not to hit a deadlock, but a race condition is still there, it is a root cause, and we better fix a root cause, not symptoms. @vozhyk-com would you be able to check a patch I prepared, with your device? https://github.com/libusb/hidapi/tree/libusb-hang
vozhyk-com commented 2020-09-26 14:35:11 -05:00 (Migrated from github.com)

I confirm that the patch from branch libusb-hang solves #142 issue. I can not reproduce it with the patch, but easy can reproduce without.

I confirm that the patch from branch libusb-hang solves #142 issue. I can not reproduce it with the patch, but easy can reproduce without.
Youw commented 2020-09-26 16:18:11 -05:00 (Migrated from github.com)

That's awesome news. As I hoped, the root issue found and fixed.

That's awesome news. As I hoped, the root issue found and fixed.
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-libusb/hidapi#142
No description provided.