No description
Find a file
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
adb adb: fix adb client running out of sockets on Windows 2015-10-30 16:23:10 -07:00
adf Adf: Remove unused variable 2015-07-27 22:08:16 -07:00
base Fix lseek argument order. 2015-10-20 13:18:22 -07:00
cpio fs_config: replace getenv('OUT') by new fs_config parameter 2015-07-09 21:47:07 -07:00
crash_reporter Merge "core: Rename libchromeos into libbrillo" 2015-10-27 22:46:51 +00:00
debuggerd Remove __DATE__/__TIME__ from init and debuggerd 2015-10-22 13:12:18 -07:00
fastboot fastboot shouldn't erase non-existent cache partitions. 2015-10-30 14:15:22 -07:00
fingerprintd Fingerprint: Add HAL close() code 2015-07-10 16:26:32 -07:00
fs_mgr fs_mgr: set partition.*.verified property even without state 2015-10-30 13:14:38 +00:00
gatekeeperd update bundled rc files to their contents from rootdir/init.rc 2015-08-21 21:54:42 +00:00
healthd Don't use libstdc++. 2015-10-23 08:46:20 -07:00
include Merge "Add deflate compression to ZipWriter" 2015-10-30 16:55:55 +00:00
init Merge "init/adb: correct static lib dependencies for libselinux" 2015-10-22 23:03:51 +00:00
libbacktrace libbacktrace: don't let llvm cflags cover libbacktrace's cflags. 2015-09-24 17:46:27 -07:00
libbinderwrapper libbinderwrapper: Add GetCallingUid() and GetCallingPid(). 2015-10-16 09:04:33 -06:00
libcutils Make allocations use unique_ptr. 2015-10-14 14:03:29 -07:00
libdiskconfig am eb075e28: am c8179498: am 2f62edf7: Merge "libdiskconfig: Turn on -Werror" 2014-05-21 21:19:27 +00:00
libion am 8d2cd035: am 2ddb9cb1: am ed43be39: Merge "Remove LOCAL_ADDITIONAL_DEPENDENCIES in cases where it\'s not needed." 2015-04-02 22:07:16 +00:00
liblog liblog: add android_log_timestamp() private function 2015-10-29 14:23:42 -07:00
libmemtrack Remove a hack for getting libpagerank's headers. 2015-09-22 22:35:30 -07:00
libmincrypt Enable clang compilation. 2015-05-14 10:53:48 -07:00
libnativebridge Use __ANDROID__ instead of HAVE_ANDROID_OS. 2015-07-30 09:33:43 -07:00
libnetutils am fb1ecbc5: am e6e71608: Merge "Use __ANDROID__ instead of HAVE_ANDROID_OS." 2015-07-30 16:50:15 +00:00
libpixelflinger Fix some compiler warnings in libpixelflinger. 2015-10-21 18:52:17 -07:00
libprocessgroup processgroup: change the polling interval for killProcessGroup() 2015-06-16 13:59:32 -07:00
libsparse Fix parameter to lseek 2015-10-20 14:15:31 -07:00
libsuspend Adds a parameter to the wakeup_callback to report sucessful wakeup or suspend aborts. 2015-06-25 13:15:22 -07:00
libsync Remove LOCAL_ADDITIONAL_DEPENDENCIES in cases where it's not needed. 2015-04-02 14:31:07 -07:00
libsysutils Fix incorrectly sized buffer. 2015-07-28 17:56:06 +09:00
libusbhost libusbhost: Fix problem reading USB string descriptors on some quirky devices 2015-06-18 13:38:31 -07:00
libutils Disable sanitization for LinearTransform 2015-10-22 11:20:45 -07:00
libziparchive Merge "ZipWriter: archive disk numbers are 0 index based" 2015-10-30 16:56:11 +00:00
lmkd bundle init.rc contents with its service 2015-08-21 10:14:43 -07:00
logcat liblog: logcat: add epoch and monotonic format modifiers 2015-10-29 14:23:42 -07:00
logd logd: statistics report chatty effective percentage 2015-10-30 14:54:17 -07:00
logwrapper am eaf04aa0: am 3b94cef1: Merge "Add FORK_EXECVP_OPTION_CAPTURE_OUTPUT to logwrap.h" 2015-08-22 02:40:51 +00:00
metricsd Use GetBuildTime from libchrome instead of __DATE__ 2015-10-29 13:02:42 -07:00
mkbootimg mkbootimg: misc typo fixes 2015-06-04 09:59:01 -07:00
packagelistparser system/core: add libpackagelistparser 2015-10-22 19:51:51 -07:00
reboot reboot: Turn on -Werror 2014-05-21 12:58:38 -07:00
rootdir Merge "Disable container overflow detect on target." 2015-10-23 22:03:52 +00:00
run-as Extend run-as with optional --user argument. 2015-06-10 12:09:10 -07:00
sdcard sdcard: use libpackageparser 2015-10-22 22:52:35 +00:00
toolbox Fix ps -Z so it combines with other arguments. 2015-10-30 15:16:45 -07:00
trusty/libtrusty Merge "Revert "[core][trusty] add gatekeeper module"" 2015-09-16 22:54:14 +00:00
tzdatacheck Executable to run on boot that removes old tzdata if needed 2015-04-13 11:38:32 +01:00
.gitignore Ignore adb/*.pyc files 2015-08-11 12:59:58 -07:00
Android.mk Remove the simulator target from all makefiles. 2011-07-11 22:12:32 -07:00
CleanSpec.mk Add a CleanSpec for grep/toolbox. 2015-03-22 18:04:54 -07:00
MODULE_LICENSE_APACHE2 auto import from //depot/cupcake/@135843 2013-07-30 13:56:49 -07:00
NOTICE Fix omission in NOTICE file. 2013-07-30 13:56:55 -07:00