From 5ce9051e2f2e65013485bc4c83897610df82f3c9 Mon Sep 17 00:00:00 2001 From: Matt Silva Date: Wed, 28 Sep 2022 10:08:27 -0400 Subject: [PATCH] libusb: stop `read_thread` from looping indefinitely (#457) - When `libusb_submit_transfer` in `read_thread` fails, `read_callback` never gets called and never sets `transfer_loop_finished` to true, causing `read_thread` to loop indefinitely; - Do not attempt to run a read loop if the initial `libusb_submit_transfer` fails fixes the issue; Fixes: #456 --- libusb/hid.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/libusb/hid.c b/libusb/hid.c index 13e8fdb8..b46b1c78 100644 --- a/libusb/hid.c +++ b/libusb/hid.c @@ -967,6 +967,7 @@ static void read_callback(struct libusb_transfer *transfer) static void *read_thread(void *param) { + int res; hid_device *dev = param; uint8_t *buf; const size_t length = dev->input_ep_max_packet_size; @@ -985,14 +986,18 @@ static void *read_thread(void *param) /* Make the first submission. Further submissions are made from inside read_callback() */ - libusb_submit_transfer(dev->transfer); + res = libusb_submit_transfer(dev->transfer); + if(res < 0) { + LOG("libusb_submit_transfer failed: %d %s. Stopping read_thread from running\n", res, libusb_error_name(res)); + dev->shutdown_thread = 1; + dev->transfer_loop_finished = 1; + } /* Notify the main thread that the read thread is up and running. */ pthread_barrier_wait(&dev->barrier); /* Handle all the events. */ while (!dev->shutdown_thread) { - int res; res = libusb_handle_events(usb_context); if (res < 0) { /* There was an error. */