platform_system_core/adb
Spencer Low 351ecd15b2 adb: fix adb client running out of sockets on Windows
Background
==========

On Windows, if you run "adb shell exit" in a loop in two windows,
eventually the adb client will be unable to connect to the adb server. I
think connect() is returning WSAEADDRINUSE: "Only one usage of each
socket address (protocol/network address/port) is normally permitted.
(10048)". The Windows System Event Log may also show Event 4227, Tcpip.
Netstat output is filled with:

  # for the adb server
  TCP    127.0.0.1:5037         127.0.0.1:65523        TIME_WAIT
  # for the adb client
  TCP    127.0.0.1:65523        127.0.0.1:5037         TIME_WAIT

The error probably means that the client is running out of free
address:port pairs.

The first netstat line is unavoidable, but the second line exists
because the adb client is not waiting for orderly/graceful shutdown of
the socket, and that is apparently required on Windows to get rid of the
second line. For more info, see
https://github.com/CompareAndSwap/SocketCloseTest .

This is exacerbated by the fact that "adb shell exit" makes 4 socket
connections to the adb server: 1) host:version, 2) host:features, 3)
host:version (again), 4) shell:exit. Also exacerbating is the fact that
the adb protocol is length-prefixed so the client typically does not
have to 'read() until zero' which effectively waits for orderly/graceful
shutdown.

The Fix
=======

Introduce a function, ReadOrderlyShutdown(), that should be called in
the adb client to wait for the server to close its socket, before
closing the client socket.

I reviewed all code where the adb client makes a connection to the adb
server and added ReadOrderlyShutdown() when it made sense. I wasn't able
to add it to the following:

* interactive_shell: this doesn't matter because this is interactive and
  thus can't be run fast enough to use up ports.
* adb sideload: I couldn't get enough test coverage and I don't think
  this is being called frequently enough to be a problem.
* send_shell_command, backup, adb_connect_command, adb shell, adb
  exec-out, install_multiple_app, adb_send_emulator_command: These
  already wait for server socket shutdown since they already call
  recv() until zero.
* restore, adb exec-in: protocol design can't have the server close
  first.
* adb start-server: no fd is actually returned
* create_local_service_socket, local_connect_arbitrary_ports,
  connect_device: probably called rarely enough not to be a problem.

Also in this change
===================

* Clarify comments in when adb_shutdown() is called before exit().
* add some missing adb_close() in adb sideload.
* Fixup error handling and comments in adb_send_emulator_command().
* Make SyncConnection::SendQuit return a success boolean.
* Add unittest for adb emu kill command. This gets code coverage over
  this very careful piece of code.

Change-Id: Iad0b1336f5b74186af2cd35f7ea827d0fa77a17c
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
2015-10-30 16:23:10 -07:00
..
client Adb: use VLOG() to replace D() for verbose logging. 2015-09-23 12:53:38 -07:00
daemon Adb: use VLOG() to replace D() for verbose logging. 2015-09-23 12:53:38 -07:00
.clang-format Fix clang-format file for correct indent settings. 2015-03-19 13:27:20 -07:00
Android.mk Show transfer progress in adb sync/pull/push. 2015-10-30 14:19:23 -07:00
CPPLINT.cfg Ignore whitespace/indent issues from cpplint. 2015-05-18 16:49:07 -07:00
MODULE_LICENSE_APACHE2 eclair snapshot 2009-11-12 18:46:23 -08:00
NOTICE eclair snapshot 2009-11-12 18:46:23 -08:00
OVERVIEW.TXT Fixing spelling errors in adb docs 2010-04-28 11:38:39 -04:00
SERVICES.TXT adb sync cleanup. 2015-08-24 11:15:01 -07:00
SYNC.TXT adb sync cleanup. 2015-08-24 11:15:01 -07:00
adb.cpp adb: fix adb client running out of sockets on Windows 2015-10-30 16:23:10 -07:00
adb.h adb: put legacy shell: service back in. 2015-09-30 15:40:09 -07:00
adb_auth.cpp Adb: use VLOG() to replace D() for verbose logging. 2015-09-23 12:53:38 -07:00
adb_auth.h Ignore ro.adb.secure in user builds. 2015-06-19 13:26:02 -07:00
adb_auth_client.cpp Adb: use VLOG() to replace D() for verbose logging. 2015-09-23 12:53:38 -07:00
adb_auth_host.cpp Use const auto&/auto&& in adb. 2015-10-07 16:11:17 -07:00
adb_client.cpp adb: fix adb client running out of sockets on Windows 2015-10-30 16:23:10 -07:00
adb_client.h Fix error handling/reporting for "adb forward" and "adb reverse". 2015-05-29 18:03:57 -07:00
adb_io.cpp adb: fix adb client running out of sockets on Windows 2015-10-30 16:23:10 -07:00
adb_io.h adb: fix adb client running out of sockets on Windows 2015-10-30 16:23:10 -07:00
adb_io_test.cpp Fix lseek argument order. 2015-10-20 13:18:22 -07:00
adb_listeners.cpp adb: clean up transport disconnect operations. 2015-08-28 16:36:00 -07:00
adb_listeners.h adb: win32: initial IPv6 support and improved Winsock error reporting 2015-07-30 23:07:55 -07:00
adb_trace.cpp Adb: use VLOG() to replace D() for verbose logging. 2015-09-23 12:53:38 -07:00
adb_trace.h Adb: use VLOG() to replace D() for verbose logging. 2015-09-23 12:53:38 -07:00
adb_utils.cpp adb: keep file flags in fdevent_install. 2015-10-06 16:15:30 -07:00
adb_utils.h adb: keep file flags in fdevent_install. 2015-10-06 16:15:30 -07:00
adb_utils_test.cpp adb: keep file flags in fdevent_install. 2015-10-06 16:15:30 -07:00
commandline.cpp adb: fix adb client running out of sockets on Windows 2015-10-30 16:23:10 -07:00
console.cpp adb: fix adb client running out of sockets on Windows 2015-10-30 16:23:10 -07:00
fdevent.cpp Use const auto&/auto&& in adb. 2015-10-07 16:11:17 -07:00
fdevent.h Add unit tests for local socket. 2015-09-16 15:00:59 -07:00
fdevent_test.cpp adb: keep file flags in fdevent_install. 2015-10-06 16:15:30 -07:00
file_sync_client.cpp adb: fix adb client running out of sockets on Windows 2015-10-30 16:23:10 -07:00
file_sync_service.cpp Use const auto&/auto&& in adb. 2015-10-07 16:11:17 -07:00
file_sync_service.h Show transfer progress in adb sync/pull/push. 2015-10-30 14:19:23 -07:00
framebuffer_service.cpp Move adb to C++. 2015-03-09 14:06:11 -07:00
get_my_path_darwin.cpp Move get_my_path_darwin to C++. 2015-04-16 13:24:58 -07:00
get_my_path_linux.cpp Move adb to C++. 2015-03-09 14:06:11 -07:00
jdwp_service.cpp adb: keep file flags in fdevent_install. 2015-10-06 16:15:30 -07:00
line_printer.cpp Show transfer progress in adb sync/pull/push. 2015-10-30 14:19:23 -07:00
line_printer.h Show transfer progress in adb sync/pull/push. 2015-10-30 14:19:23 -07:00
mutex_list.h adb: clean up debug tracing a little. 2015-09-02 20:21:00 -07:00
protocol.txt Increase size of the the adb packets sent over the wire 2015-07-22 13:06:06 -07:00
remount_service.cpp Adb: use VLOG() to replace D() for verbose logging. 2015-09-23 12:53:38 -07:00
remount_service.h Failure to find an oem partition should not be a remount failure. 2015-05-11 13:36:13 -07:00
services.cpp Fix adb -d/-e error reporting. 2015-10-07 15:35:18 -07:00
services.h adb: put legacy shell: service back in. 2015-09-30 15:40:09 -07:00
set_verity_enable_state_service.cpp adb: use libfec in disable-verity 2015-10-20 13:55:24 +01:00
shell_service.cpp adb: non-interactive shell stdin. 2015-10-15 09:44:54 -07:00
shell_service.h adb: non-interactive shell stdin. 2015-10-15 09:44:54 -07:00
shell_service_protocol.cpp adb: create shell protocol class (take 2). 2015-09-08 10:04:10 -07:00
shell_service_protocol_test.cpp adb: fix shell_service_protocol_test for Windows. 2015-09-11 18:28:27 -07:00
shell_service_test.cpp adb: non-interactive shell stdin. 2015-10-15 09:44:54 -07:00
socket.h Add unit tests for local socket. 2015-09-16 15:00:59 -07:00
socket_test.cpp adb: detect sockets in CLOSE_WAIT state to prevent socket leak on linux. 2015-09-30 15:03:26 -07:00
sockets.cpp Fix adb -d/-e error reporting. 2015-10-07 15:35:18 -07:00
sockets.dia auto import from //depot/cupcake/@135843 2009-03-03 19:32:55 -08:00
sysdeps.h adb: win32: Improve Winsock error code mappings and strings 2015-10-18 16:45:09 -07:00
sysdeps_win32.cpp Don't use VLAs in adb. 2015-10-27 15:46:41 -07:00
sysdeps_win32_test.cpp adb: win32: Improve Winsock error code mappings and strings 2015-10-18 16:45:09 -07:00
test_adb.py adb: fix adb client running out of sockets on Windows 2015-10-30 16:23:10 -07:00
test_track_devices.cpp Add missing includes. 2015-08-11 16:39:57 -07:00
transport.cpp Use const auto&/auto&& in adb. 2015-10-07 16:11:17 -07:00
transport.h Fix adb -d/-e error reporting. 2015-10-07 15:35:18 -07:00
transport_local.cpp Adb: use VLOG() to replace D() for verbose logging. 2015-09-23 12:53:38 -07:00
transport_test.cpp adb: fix adbd feature parsing for no features. 2015-09-25 13:10:17 -07:00
transport_usb.cpp Adb: use VLOG() to replace D() for verbose logging. 2015-09-23 12:53:38 -07:00
usb_linux.cpp Adb: use VLOG() to replace D() for verbose logging. 2015-09-23 12:53:38 -07:00
usb_linux_client.cpp adbd: Add os descriptor support for adb. 2015-10-07 06:04:00 +00:00
usb_osx.cpp Adb: use VLOG() to replace D() for verbose logging. 2015-09-23 12:53:38 -07:00
usb_windows.cpp Adb: use VLOG() to replace D() for verbose logging. 2015-09-23 12:53:38 -07:00