From af83341fdd1f18ba20bb58069893583c64532099 Mon Sep 17 00:00:00 2001 From: none Date: Fri, 18 Aug 2017 09:16:43 +0200 Subject: [PATCH 01/13] Added timeout to TcpHandle class --- adb/common.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/adb/common.py b/adb/common.py index e63fa00..f136ef2 100644 --- a/adb/common.py +++ b/adb/common.py @@ -278,9 +278,12 @@ def FindDevices(cls, setting_matcher, device_matcher=None, class TcpHandle(object): """TCP connection object. - Provides same interface as UsbHandle but ignores timeout.""" + Provides same interface as UsbHandle, sets a timout + which is handled by running the initial connection + adb_protocol.AdbMessage.Connect(tcp) as a separate + process with a timout (See adb_commands.AdbCommands.Connect()).""" - def __init__(self, serial): + def __init__(self, serial, timeout=None): """Initialize the TCP Handle. Arguments: serial: Android device serial of the form host or host:port. @@ -292,6 +295,10 @@ def __init__(self, serial): else: host = serial port = 5555 + if timeout: + self._timeout = float (timeout/1000) + else: + self._timeout = 60 self._serial_number = '%s:%s' % (host, port) self._connection = socket.create_connection((host, port)) From a6d8b4e14057f293fa0cab77f3026e2b15dfdfea Mon Sep 17 00:00:00 2001 From: none Date: Fri, 18 Aug 2017 09:19:15 +0200 Subject: [PATCH 02/13] Determining device_state will now timeout (Would previously hang indefinitely) --- adb/adb_commands.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/adb/adb_commands.py b/adb/adb_commands.py index 8a745ba..704d57d 100644 --- a/adb/adb_commands.py +++ b/adb/adb_commands.py @@ -25,6 +25,7 @@ import io import os import socket +import multiprocessing from adb import adb_protocol from adb import common @@ -45,6 +46,8 @@ # Ignore this error when M2Crypto is not installed, there are other options. pass +class TCPTimeout(Exception): + pass class AdbCommands(object): """Exposes adb-like methods for use. @@ -68,7 +71,7 @@ def ConnectDevice( used instead of a USB connection. """ if serial and b':' in serial: - handle = common.TcpHandle(serial) + handle = common.TcpHandle(serial,timeout=default_timeout_ms) else: handle = common.UsbHandle.FindAndOpen( DeviceIsAvailable, port_path=port_path, serial=serial, @@ -96,7 +99,22 @@ def Connect(cls, usb, banner=None, **kwargs): """ if not banner: banner = socket.gethostname().encode() - device_state = cls.protocol_handler.Connect(usb, banner=banner, **kwargs) + if isinstance(usb, common.TcpHandle): + def f(q): + device_state = cls.protocol_handler.Connect(usb, banner=banner, **kwargs) + q.put([device_state]) + q = multiprocessing.Queue() + p = multiprocessing.Process(target=f, args=(q,)) + p.start() + p.join(usb._timeout) + if p.is_alive(): + msg = 'TCP connection to %s timed out (%ss)' % ( + usb._serial_number,usb._timeout) + p.terminate() + raise TCPTimeout(msg) + device_state = q.get()[0] + else: + device_state = cls.protocol_handler.Connect(usb, banner=banner, **kwargs) # Remove banner and colons after device state (state::banner) device_state = device_state.split(b':')[0] return cls(usb, device_state) From de67f60fc665843e3ed012f77b54352c98ccf689 Mon Sep 17 00:00:00 2001 From: "Jonas Riisnaes -T (jriisnae - VEKSTHUSET REKRUTTERING AS at Cisco)" Date: Wed, 23 Aug 2017 12:47:28 +0200 Subject: [PATCH 03/13] Removed multiprocessing and TCPTimeout(Exception), timout handled in TcpHandler --- adb/adb_commands.py | 22 ++-------------------- 1 file changed, 2 insertions(+), 20 deletions(-) diff --git a/adb/adb_commands.py b/adb/adb_commands.py index 704d57d..91b9aac 100644 --- a/adb/adb_commands.py +++ b/adb/adb_commands.py @@ -25,7 +25,6 @@ import io import os import socket -import multiprocessing from adb import adb_protocol from adb import common @@ -46,8 +45,6 @@ # Ignore this error when M2Crypto is not installed, there are other options. pass -class TCPTimeout(Exception): - pass class AdbCommands(object): """Exposes adb-like methods for use. @@ -71,7 +68,7 @@ def ConnectDevice( used instead of a USB connection. """ if serial and b':' in serial: - handle = common.TcpHandle(serial,timeout=default_timeout_ms) + handle = common.TcpHandle(serial,timeout_ms=default_timeout_ms) else: handle = common.UsbHandle.FindAndOpen( DeviceIsAvailable, port_path=port_path, serial=serial, @@ -99,22 +96,7 @@ def Connect(cls, usb, banner=None, **kwargs): """ if not banner: banner = socket.gethostname().encode() - if isinstance(usb, common.TcpHandle): - def f(q): - device_state = cls.protocol_handler.Connect(usb, banner=banner, **kwargs) - q.put([device_state]) - q = multiprocessing.Queue() - p = multiprocessing.Process(target=f, args=(q,)) - p.start() - p.join(usb._timeout) - if p.is_alive(): - msg = 'TCP connection to %s timed out (%ss)' % ( - usb._serial_number,usb._timeout) - p.terminate() - raise TCPTimeout(msg) - device_state = q.get()[0] - else: - device_state = cls.protocol_handler.Connect(usb, banner=banner, **kwargs) + device_state = cls.protocol_handler.Connect(usb, banner=banner, **kwargs) # Remove banner and colons after device state (state::banner) device_state = device_state.split(b':')[0] return cls(usb, device_state) From 122a9ac12d4080dd518ba36ad13bf32f464b124c Mon Sep 17 00:00:00 2001 From: "Jonas Riisnaes -T (jriisnae - VEKSTHUSET REKRUTTERING AS at Cisco)" Date: Wed, 23 Aug 2017 12:49:37 +0200 Subject: [PATCH 04/13] Added timout to BulkRead by means of select library --- adb/common.py | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/adb/common.py b/adb/common.py index f136ef2..f90c1c0 100644 --- a/adb/common.py +++ b/adb/common.py @@ -19,6 +19,7 @@ import socket import threading import weakref +import select import libusb1 import usb1 @@ -45,6 +46,10 @@ def Matcher(device): return Matcher +class TCPTimeout(Exception): + pass + + class UsbHandle(object): """USB communication object. Not thread-safe. @@ -278,12 +283,9 @@ def FindDevices(cls, setting_matcher, device_matcher=None, class TcpHandle(object): """TCP connection object. - Provides same interface as UsbHandle, sets a timout - which is handled by running the initial connection - adb_protocol.AdbMessage.Connect(tcp) as a separate - process with a timout (See adb_commands.AdbCommands.Connect()).""" + Provides same interface as UsbHandle. """ - def __init__(self, serial, timeout=None): + def __init__(self, serial, timeout_ms=None): """Initialize the TCP Handle. Arguments: serial: Android device serial of the form host or host:port. @@ -295,13 +297,11 @@ def __init__(self, serial, timeout=None): else: host = serial port = 5555 - if timeout: - self._timeout = float (timeout/1000) - else: - self._timeout = 60 + self._timeout_ms = timeout_ms or DEFAULT_TIMEOUT_MS self._serial_number = '%s:%s' % (host, port) self._connection = socket.create_connection((host, port)) + self._connection.setblocking(0) @property def serial_number(self): @@ -310,11 +310,16 @@ def serial_number(self): def BulkWrite(self, data, timeout=None): return self._connection.sendall(data) - def BulkRead(self, numbytes, timeout=None): - return self._connection.recv(numbytes) + def BulkRead(self, numbytes, timeout_ms=None): + tsec = float(self.Timeout(timeout_ms) / 1000) + ready = select.select([self._connection], [], [], tsec) + if ready[0]: + return self._connection.recv(numbytes) + msg = 'Reading from {} timed out (Timeout {}s)'.format(self._serial_number,tsec) + raise TCPTimeout(msg) def Timeout(self, timeout_ms): - return timeout_ms + return timeout_ms if timeout_ms is not None else self._timeout_ms def Close(self): return self._connection.close() From 7fc30651ab77922194b1d8419b8e5b43d2c879d5 Mon Sep 17 00:00:00 2001 From: "Jonas Riisnaes -T (jriisnae - VEKSTHUSET REKRUTTERING AS at Cisco)" Date: Tue, 29 Aug 2017 13:57:07 +0200 Subject: [PATCH 05/13] socket.create_connection timeout can now be set by 'default_timout_ms' --- adb/common.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/adb/common.py b/adb/common.py index f90c1c0..de71977 100644 --- a/adb/common.py +++ b/adb/common.py @@ -300,7 +300,15 @@ def __init__(self, serial, timeout_ms=None): self._timeout_ms = timeout_ms or DEFAULT_TIMEOUT_MS self._serial_number = '%s:%s' % (host, port) - self._connection = socket.create_connection((host, port)) + try: + tout = float(self._timeout_ms / 1000) + self._connection = socket.create_connection( + (host, port), + timeout = tout ) + except socket.timeout as e: + msg = 'Creating connection to {} timed out (Timeout {}s)'.format(self._serial_number,tout) + raise TCPTimeout(msg) + self._connection.setblocking(0) @property From 93533584f2d84855fddb8a8a0d360373aeec0fe8 Mon Sep 17 00:00:00 2001 From: "Jonas Riisnaes -T (jriisnae - VEKSTHUSET REKRUTTERING AS at Cisco)" Date: Fri, 1 Sep 2017 15:45:13 +0200 Subject: [PATCH 06/13] Cast timeout_ms to int --- adb/common.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/adb/common.py b/adb/common.py index de71977..6c0c770 100644 --- a/adb/common.py +++ b/adb/common.py @@ -301,7 +301,7 @@ def __init__(self, serial, timeout_ms=None): self._serial_number = '%s:%s' % (host, port) try: - tout = float(self._timeout_ms / 1000) + tout = float(int(self._timeout_ms) / 1000) self._connection = socket.create_connection( (host, port), timeout = tout ) @@ -319,7 +319,8 @@ def BulkWrite(self, data, timeout=None): return self._connection.sendall(data) def BulkRead(self, numbytes, timeout_ms=None): - tsec = float(self.Timeout(timeout_ms) / 1000) + #tsec = float(self.Timeout(timeout_ms) / 1000) + tsec = int(self._timeout_ms) / 1000 ready = select.select([self._connection], [], [], tsec) if ready[0]: return self._connection.recv(numbytes) From f51bfbc470719e5b3688d7c4ec62e726dae6476b Mon Sep 17 00:00:00 2001 From: "Jonas Riisnaes -T (jriisnae - VEKSTHUSET REKRUTTERING AS at Cisco)" Date: Wed, 6 Sep 2017 15:25:35 +0200 Subject: [PATCH 07/13] KJR: Cleaned up --- adb/common.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/adb/common.py b/adb/common.py index 6c0c770..237c195 100644 --- a/adb/common.py +++ b/adb/common.py @@ -297,14 +297,12 @@ def __init__(self, serial, timeout_ms=None): else: host = serial port = 5555 - self._timeout_ms = timeout_ms or DEFAULT_TIMEOUT_MS + self._timeout_ms = timeout_ms if timeout_ms else DEFAULT_TIMEOUT_MS self._serial_number = '%s:%s' % (host, port) try: tout = float(int(self._timeout_ms) / 1000) - self._connection = socket.create_connection( - (host, port), - timeout = tout ) + self._connection = socket.create_connection((host,port),timeout=tout) except socket.timeout as e: msg = 'Creating connection to {} timed out (Timeout {}s)'.format(self._serial_number,tout) raise TCPTimeout(msg) @@ -319,8 +317,8 @@ def BulkWrite(self, data, timeout=None): return self._connection.sendall(data) def BulkRead(self, numbytes, timeout_ms=None): - #tsec = float(self.Timeout(timeout_ms) / 1000) - tsec = int(self._timeout_ms) / 1000 + tmsec = self.Timeout(timeout_ms) + tsec = float(tmsec / 1000) ready = select.select([self._connection], [], [], tsec) if ready[0]: return self._connection.recv(numbytes) From f4e6baa08de9b14e0f1afb5a179973f8d54a2a0d Mon Sep 17 00:00:00 2001 From: "Jonas Riisnaes -T (jriisnae - VEKSTHUSET REKRUTTERING AS at Cisco)" Date: Thu, 7 Sep 2017 14:49:21 +0200 Subject: [PATCH 08/13] KJR: Increased default timeout --- adb/common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adb/common.py b/adb/common.py index 237c195..abcaf1c 100644 --- a/adb/common.py +++ b/adb/common.py @@ -26,7 +26,7 @@ from adb import usb_exceptions -DEFAULT_TIMEOUT_MS = 1000 +DEFAULT_TIMEOUT_MS = 30000 _LOG = logging.getLogger('android_usb') From 4fa129bd71ae1634cf3d4424f24bc87fd8f294c2 Mon Sep 17 00:00:00 2001 From: "Jonas Riisnaes -T (jriisnae - VEKSTHUSET REKRUTTERING AS at Cisco)" Date: Fri, 8 Sep 2017 10:30:28 +0200 Subject: [PATCH 09/13] Set back to original --- adb/common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adb/common.py b/adb/common.py index abcaf1c..237c195 100644 --- a/adb/common.py +++ b/adb/common.py @@ -26,7 +26,7 @@ from adb import usb_exceptions -DEFAULT_TIMEOUT_MS = 30000 +DEFAULT_TIMEOUT_MS = 1000 _LOG = logging.getLogger('android_usb') From a1a74d9f80bb7ab2415ee4e8702a8f48d60607f4 Mon Sep 17 00:00:00 2001 From: "Jonas Riisnaes -T (jriisnae - VEKSTHUSET REKRUTTERING AS at Cisco)" Date: Fri, 8 Sep 2017 14:03:45 +0200 Subject: [PATCH 10/13] Moved TCPTimoutException into usb_exceptions, timeout_ms can now be None --- adb/usb_exceptions.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/adb/usb_exceptions.py b/adb/usb_exceptions.py index 4c50f53..aa02bac 100644 --- a/adb/usb_exceptions.py +++ b/adb/usb_exceptions.py @@ -70,3 +70,7 @@ class AdbCommandFailureException(Exception): class AdbOperationException(Exception): """Failed to communicate over adb with device after multiple retries.""" + + +class TCPTimeoutException(FormatMessageWithArgumentsException): + """TCP connection timed out in the time out given.""" From 1198669fcf918b3f223ea1ced642d503b0a37f58 Mon Sep 17 00:00:00 2001 From: "Jonas Riisnaes -T (jriisnae - VEKSTHUSET REKRUTTERING AS at Cisco)" Date: Fri, 8 Sep 2017 14:04:14 +0200 Subject: [PATCH 11/13] Revert "Moved TCPTimoutException into usb_exceptions, timeout_ms can now be None" This reverts commit a1a74d9f80bb7ab2415ee4e8702a8f48d60607f4. --- adb/usb_exceptions.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/adb/usb_exceptions.py b/adb/usb_exceptions.py index aa02bac..4c50f53 100644 --- a/adb/usb_exceptions.py +++ b/adb/usb_exceptions.py @@ -70,7 +70,3 @@ class AdbCommandFailureException(Exception): class AdbOperationException(Exception): """Failed to communicate over adb with device after multiple retries.""" - - -class TCPTimeoutException(FormatMessageWithArgumentsException): - """TCP connection timed out in the time out given.""" From 6a1a20ec847d7aa65ffbfd8a4d89f9b149b75b15 Mon Sep 17 00:00:00 2001 From: "Jonas Riisnaes -T (jriisnae - VEKSTHUSET REKRUTTERING AS at Cisco)" Date: Fri, 8 Sep 2017 14:06:49 +0200 Subject: [PATCH 12/13] Added exception TCPTimoutException for exception raising in tcphandle --- adb/usb_exceptions.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/adb/usb_exceptions.py b/adb/usb_exceptions.py index 4c50f53..aa02bac 100644 --- a/adb/usb_exceptions.py +++ b/adb/usb_exceptions.py @@ -70,3 +70,7 @@ class AdbCommandFailureException(Exception): class AdbOperationException(Exception): """Failed to communicate over adb with device after multiple retries.""" + + +class TCPTimeoutException(FormatMessageWithArgumentsException): + """TCP connection timed out in the time out given.""" From 7e2c640fdeec7c31b5d6342d0fb3330e9fffc490 Mon Sep 17 00:00:00 2001 From: "Jonas Riisnaes -T (jriisnae - VEKSTHUSET REKRUTTERING AS at Cisco)" Date: Fri, 8 Sep 2017 14:07:38 +0200 Subject: [PATCH 13/13] Moved TCPTimoutException into usb_exceptions, timeout_ms can now be None --- adb/common.py | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/adb/common.py b/adb/common.py index 237c195..e1acd7e 100644 --- a/adb/common.py +++ b/adb/common.py @@ -46,10 +46,6 @@ def Matcher(device): return Matcher -class TCPTimeout(Exception): - pass - - class UsbHandle(object): """USB communication object. Not thread-safe. @@ -297,15 +293,10 @@ def __init__(self, serial, timeout_ms=None): else: host = serial port = 5555 - self._timeout_ms = timeout_ms if timeout_ms else DEFAULT_TIMEOUT_MS + self._timeout_ms = timeout_ms self._serial_number = '%s:%s' % (host, port) - - try: - tout = float(int(self._timeout_ms) / 1000) - self._connection = socket.create_connection((host,port),timeout=tout) - except socket.timeout as e: - msg = 'Creating connection to {} timed out (Timeout {}s)'.format(self._serial_number,tout) - raise TCPTimeout(msg) + timeout = self._timeout_ms/1000.0 if self._timeout_ms else None + self._connection = socket.create_connection((host,port),timeout=timeout) self._connection.setblocking(0) @@ -317,13 +308,12 @@ def BulkWrite(self, data, timeout=None): return self._connection.sendall(data) def BulkRead(self, numbytes, timeout_ms=None): - tmsec = self.Timeout(timeout_ms) - tsec = float(tmsec / 1000) - ready = select.select([self._connection], [], [], tsec) + t = self.Timeout(timeout_ms)/1000.0 if self.Timeout(timeout_ms) else None + ready = select.select([self._connection], [], [], t) if ready[0]: return self._connection.recv(numbytes) - msg = 'Reading from {} timed out (Timeout {}s)'.format(self._serial_number,tsec) - raise TCPTimeout(msg) + msg = 'Reading from {} timed out (Timeout {}s)'.format(self._serial_number,t) + raise usb_exceptions.TCPTimeoutException(msg) def Timeout(self, timeout_ms): return timeout_ms if timeout_ms is not None else self._timeout_ms