Revert "adb: fix deadlock between transport_unref and usb_close."
This reverts commit 7e197ef83307ffcf2f55d1b8a603d0d3bd2e1d4d.
The mutex lock in transport_unref hides a race that seems otherwise
hard to fix. Specifically, there's no synchronization between acquiring
a transport and attaching it to an asocket*, leading to badness if the
transport is closed in between the two operations.
Fix the original problem the reverted patch addressed by manually
unlocking before calling unregister_usb_transport.
Bug: http://b/65419665
Test: python test_device.py
Change-Id: I0ed0044129b1671b2c5dd1b9fa2e70a9b4475dc5
diff --git a/adb/transport.cpp b/adb/transport.cpp
index b2e03a0..1b597fd 100644
--- a/adb/transport.cpp
+++ b/adb/transport.cpp
@@ -615,15 +615,15 @@
static void transport_unref(atransport* t) {
CHECK(t != nullptr);
- size_t old_refcount = t->ref_count--;
- CHECK_GT(old_refcount, 0u);
-
- if (old_refcount == 1u) {
+ std::lock_guard<std::recursive_mutex> lock(transport_lock);
+ CHECK_GT(t->ref_count, 0u);
+ t->ref_count--;
+ if (t->ref_count == 0) {
D("transport: %s unref (kicking and closing)", t->serial);
t->close(t);
remove_transport(t);
} else {
- D("transport: %s unref (count=%zu)", t->serial, old_refcount - 1);
+ D("transport: %s unref (count=%zu)", t->serial, t->ref_count);
}
}