From b8b7fd61d235fbc97791dcf7f70372b192e1047d Mon Sep 17 00:00:00 2001 From: osy <50960678+osy@users.noreply.github.com> Date: Tue, 11 May 2021 20:36:31 -0700 Subject: [PATCH] darwin: add timeout for reset reenumerate USBDeviceReEnumerate() does not return an error code (bug?) so if it fails we could be stuck waiting forever. Set a sane timeout to 10s. Signed-off-by: Nathan Hjelm --- libusb/os/darwin_usb.c | 11 +++++++++++ libusb/version_nano.h | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/libusb/os/darwin_usb.c b/libusb/os/darwin_usb.c index 337dc92..699ca0e 100644 --- a/libusb/os/darwin_usb.c +++ b/libusb/os/darwin_usb.c @@ -41,6 +41,10 @@ * function. Its use is also conditionalized to only older deployment targets. */ #define OBJC_SILENCE_GC_DEPRECATIONS 1 +/* Default timeout to 10s for reenumerate. This is needed because USBDeviceReEnumerate + * does not return error status on macOS. */ +#define DARWIN_REENUMERATE_TIMEOUT_US 10000000 + #include #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060 && MAC_OS_X_VERSION_MIN_REQUIRED < 101200 #include @@ -1678,6 +1682,7 @@ static int darwin_reset_device(struct libusb_device_handle *dev_handle) { IOUSBConfigurationDescriptor *cached_configurations; IOReturn kresult; UInt8 i; + UInt32 time; if (dpriv->in_reenumerate) { /* ack, two (or more) threads are trying to reset the device! abort! */ @@ -1705,9 +1710,15 @@ static int darwin_reset_device(struct libusb_device_handle *dev_handle) { usbi_dbg ("darwin/reset_device: waiting for re-enumeration to complete..."); + time = 0; while (dpriv->in_reenumerate) { struct timespec delay = {.tv_sec = 0, .tv_nsec = 1000}; nanosleep (&delay, NULL); + if (time++ >= DARWIN_REENUMERATE_TIMEOUT_US) { + usbi_err (HANDLE_CTX (dev_handle), "darwin/reenumerate_device: timeout waiting for reenumerate"); + dpriv->in_reenumerate = false; + return LIBUSB_ERROR_TIMEOUT; + } } /* compare descriptors */ diff --git a/libusb/version_nano.h b/libusb/version_nano.h index 8d36b28..b4a6dae 100644 --- a/libusb/version_nano.h +++ b/libusb/version_nano.h @@ -1 +1 @@ -#define LIBUSB_NANO 11609 +#define LIBUSB_NANO 11610 -- 2.7.4