Index: build/android/pylib/ports.py |
diff --git a/build/android/pylib/ports.py b/build/android/pylib/ports.py |
index 06b9f584d284193108bd8eaf0494455096b53dfe..74c84c1a0c57f2c8e2b9bac982f93cb3e2ac8b2b 100644 |
--- a/build/android/pylib/ports.py |
+++ b/build/android/pylib/ports.py |
@@ -2,7 +2,7 @@ |
# Use of this source code is governed by a BSD-style license that can be |
# found in the LICENSE file. |
-"""Functions that deals with local and device ports.""" |
+"""Functions that deal with local and device ports.""" |
import contextlib |
import fcntl |
@@ -17,12 +17,13 @@ import cmd_helper |
import constants |
-#The following two methods are used to allocate the port source for various |
-# types of test servers. Because some net relates tests can be run on shards |
-# at same time, it's important to have a mechanism to allocate the port process |
-# safe. In here, we implement the safe port allocation by leveraging flock. |
+# The following two methods are used to allocate the port source for various |
+# types of test servers. Because some net-related tests can be run on shards at |
+# same time, it's important to have a mechanism to allocate the port |
+# process-safe. In here, we implement the safe port allocation by leveraging |
+# flock. |
def ResetTestServerPortAllocation(): |
- """Reset the port allocation to start from TEST_SERVER_PORT_FIRST. |
+ """Resets the port allocation to start from TEST_SERVER_PORT_FIRST. |
Returns: |
Returns True if reset successes. Otherwise returns False. |
@@ -39,7 +40,7 @@ def ResetTestServerPortAllocation(): |
def AllocateTestServerPort(): |
- """Allocate a port incrementally. |
+ """Allocates a port incrementally. |
Returns: |
Returns a valid port which should be in between TEST_SERVER_PORT_FIRST and |
@@ -90,10 +91,12 @@ def IsHostPortUsed(host_port): |
Returns: |
True if the port on host is already used, otherwise returns False. |
""" |
- port_info = '(127\.0\.0\.1)|(localhost)\:%d' % host_port |
- # TODO(jnd): Find a better way to filter the port. |
+ port_info = '(\*)|(127\.0\.0\.1)|(localhost):%d' % host_port |
+ # TODO(jnd): Find a better way to filter the port. Note that connecting to the |
+ # socket and closing it would leave it in the TIME_WAIT state. Setting |
+ # SO_LINGER on it and then closing it makes the Python HTTP server crash. |
re_port = re.compile(port_info, re.MULTILINE) |
- if re_port.findall(cmd_helper.GetCmdOutput(['lsof', '-nPi:%d' % host_port])): |
+ if re_port.search(cmd_helper.GetCmdOutput(['lsof', '-nPi:%d' % host_port])): |
return True |
return False |
@@ -115,6 +118,11 @@ def IsDevicePortUsed(adb, device_port, state=''): |
for single_connect in netstat_results: |
# Column 3 is the local address which we want to check with. |
connect_results = single_connect.split() |
+ if connect_results[0] != 'tcp': |
+ continue |
+ if len(connect_results) < 6: |
+ raise Exception('Unexpected format while parsing netstat line: ' + |
+ single_connect) |
is_state_match = connect_results[5] == state if state else True |
if connect_results[3] == base_url and is_state_match: |
return True |