Bug 1240601 - Set timeout on socket in transport.wait_for_port; r=automatedtester

If the client is able to connect but the server never sends any data,
the socket should time out in order for the function not to hang forever.

That said, polling for ports like this is inherently racy and we should
instead specify and bind what port we wish Marionette to start on in
the future.
This commit is contained in:
Andreas Tolfsen 2016-01-18 22:12:55 +00:00
parent ab64de1459
commit 4c7adabd69

View File

@ -289,22 +289,23 @@ class TcpTransport(object):
def wait_for_port(host, port, timeout=60):
""" Wait for the specified host/port to be available."""
"""Wait for the specified host/port to become available."""
starttime = datetime.datetime.now()
poll_interval = 0.1
while datetime.datetime.now() - starttime < datetime.timedelta(seconds=timeout):
sock = None
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(0.5)
sock.connect((host, port))
data = sock.recv(16)
sock.close()
if ':' in data:
if ":" in data:
return True
except socket.error:
pass
finally:
if sock:
if sock is not None:
sock.close()
time.sleep(poll_interval)
return False