From f74dd42e4e20bc09748aa5157186b05bdf31f74a Mon Sep 17 00:00:00 2001 From: Josh Gao Date: Thu, 1 Jun 2017 15:34:21 -0700 Subject: [PATCH] adb: improve race between device_connected and device_disconnected. If a device is unplugged while it's being processed, we can be inside register_usb_transport still when we call unregister_usb_transport, leading to bad things happening. Move the call of register_usb_transport into the scope guarded by usb_handles_mutex. Bug: http://b/62200735 Test: manual Merged-In: I2517dcb9d30d9a08cebbb5f715ae3fd30c510109 Change-Id: I2517dcb9d30d9a08cebbb5f715ae3fd30c510109 (cherry picked from commit 7dd382ded948aa5adae3618f99d00c14c3703d05) --- adb/client/usb_libusb.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/adb/client/usb_libusb.cpp b/adb/client/usb_libusb.cpp index b2fdc07f0..dc1a6b476 100644 --- a/adb/client/usb_libusb.cpp +++ b/adb/client/usb_libusb.cpp @@ -377,9 +377,10 @@ static void process_device(libusb_device* device) { { std::unique_lock lock(usb_handles_mutex); usb_handles[device_address] = std::move(result); - } - register_usb_transport(usb_handle_raw, device_serial.c_str(), device_address.c_str(), writable); + register_usb_transport(usb_handle_raw, device_serial.c_str(), device_address.c_str(), + writable); + } LOG(INFO) << "registered new usb device '" << device_serial << "'"; }