Skip to content

Commit

Permalink
libusb: stop read_thread from looping indefinitely (signal11#457)
Browse files Browse the repository at this point in the history
- 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: signal11#456
  • Loading branch information
thesilvanator authored Sep 28, 2022
1 parent 4e63d6d commit 5ce9051
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions libusb/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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. */
Expand Down

0 comments on commit 5ce9051

Please sign in to comment.