diff --git a/adb/adb_utils.h b/adb/adb_utils.h index 20c63b39d..11c0ec9cc 100644 --- a/adb/adb_utils.h +++ b/adb/adb_utils.h @@ -74,13 +74,18 @@ class BlockingQueue { template void PopAll(Fn fn) { - std::unique_lock lock(mutex); - cv.wait(lock, [this]() { return !queue.empty(); }); + std::vector popped; - for (const T& t : queue) { + { + std::unique_lock lock(mutex); + cv.wait(lock, [this]() { return !queue.empty(); }); + popped = std::move(queue); + queue.clear(); + } + + for (const T& t : popped) { fn(t); } - queue.clear(); } }; diff --git a/adb/client/usb_libusb.cpp b/adb/client/usb_libusb.cpp index 7e77b5ede..b2fdc07f0 100644 --- a/adb/client/usb_libusb.cpp +++ b/adb/client/usb_libusb.cpp @@ -422,8 +422,10 @@ static void device_disconnected(libusb_device* device) { if (!it->second->device_handle) { // If the handle is null, we were never able to open the device. unregister_usb_transport(it->second.get()); + usb_handles.erase(it); + } else { + // Closure of the transport will erase the usb_handle. } - usb_handles.erase(it); } }