From 34f45c566365c966c78264e8a4dc0cf69957b6fa Mon Sep 17 00:00:00 2001 From: Yabin Cui Date: Wed, 26 Aug 2015 11:18:42 -0700 Subject: [PATCH] adb: keep all asocket operations in the main thread. As far as I can see, all asockets operations happen in fdevent_loop() in the main thread, excepting close_all_sockets(). Instead of adding lock and ref_count for each asocket, a simpler way would be moving close_all_sockets() from input_thread to the main thread. In input_thread(), there are two path to break the loop and call close_all_sockets(). One path is when receiving offline A_SYNC, which is sent by the main thread. The other path is when read_packet fails, which I believe is almost not possible and doesn't matter (Because t->fd is closed just before t is freed.). So I move close_all_sockets() to handle_offline() in the main thread. the socket_list_lock in sockets.cpp could be removed. But I prefer to leave it for the following changes. Bug: 6558362 Change-Id: I5da23f60a67a331262c62693b9b127fe2689c799 --- adb/adb.cpp | 5 +++++ adb/transport.cpp | 4 ---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/adb/adb.cpp b/adb/adb.cpp index 29c948126..a0501a677 100644 --- a/adb/adb.cpp +++ b/adb/adb.cpp @@ -243,6 +243,11 @@ void handle_offline(atransport *t) D("adb: offline\n"); //Close the associated usb t->online = 0; + + // This is necessary to avoid a race condition that occured when a transport closes + // while a client socket is still active. + close_all_sockets(t); + run_transport_disconnects(t); } diff --git a/adb/transport.cpp b/adb/transport.cpp index afdab86fa..5e4ffbb95 100644 --- a/adb/transport.cpp +++ b/adb/transport.cpp @@ -330,10 +330,6 @@ static void *input_thread(void *_t) put_apacket(p); } - // this is necessary to avoid a race condition that occured when a transport closes - // while a client socket is still active. - close_all_sockets(t); - D("%s: transport input thread is exiting, fd %d\n", t->serial, t->fd); kick_transport(t); transport_unref(t);