Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2851)

Unified Diff: build/android/pylib/ports.py

Issue 634803002: Make host port availability detection more robust. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Allow reuseaddr before binding Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « build/android/pylib/chrome_test_server_spawner.py ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: build/android/pylib/ports.py
diff --git a/build/android/pylib/ports.py b/build/android/pylib/ports.py
index 34efb5253c425b807f3549eec12707f653c4e744..578152cb0f1e029f53a06294e06b3c2fe19ca0a5 100644
--- a/build/android/pylib/ports.py
+++ b/build/android/pylib/ports.py
@@ -9,11 +9,9 @@ import fcntl
import httplib
import logging
import os
-import re
import socket
import traceback
-from pylib import cmd_helper
from pylib import constants
@@ -57,7 +55,7 @@ def AllocateTestServerPort():
with open(constants.TEST_SERVER_PORT_FILE, 'r+') as fp:
port = int(fp.read())
ports_tried.append(port)
- while IsHostPortUsed(port):
+ while not IsHostPortAvailable(port):
port += 1
ports_tried.append(port)
if (port > constants.TEST_SERVER_PORT_LAST or
@@ -67,7 +65,7 @@ def AllocateTestServerPort():
fp.seek(0, os.SEEK_SET)
fp.write('%d' % (port + 1))
except Exception as e:
- logging.info(e)
+ logging.error(e)
finally:
if fp_lock:
fcntl.flock(fp_lock, fcntl.LOCK_UN)
@@ -80,25 +78,23 @@ def AllocateTestServerPort():
return port
-def IsHostPortUsed(host_port):
- """Checks whether the specified host port is used or not.
-
- Uses -n -P to inhibit the conversion of host/port numbers to host/port names.
+def IsHostPortAvailable(host_port):
+ """Checks whether the specified host port is available.
Args:
- host_port: Port on host we want to check.
+ host_port: Port on host to check.
Returns:
- True if the port on host is already used, otherwise returns False.
+ True if the port on host is available, otherwise returns False.
"""
- 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.search(cmd_helper.GetCmdOutput(['lsof', '-nPi:%d' % host_port])):
+ s = socket.socket()
+ try:
+ s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+ s.bind(('', host_port))
+ s.close()
return True
- return False
+ except socket.error:
+ return False
def IsDevicePortUsed(device, device_port, state=''):
« no previous file with comments | « build/android/pylib/chrome_test_server_spawner.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698