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.
--- a/testing/marionette/transport/marionette_transport/transport.py
+++ b/testing/marionette/transport/marionette_transport/transport.py
@@ -284,27 +284,28 @@ class TcpTransport(object):
self.sock.close()
def __del__(self):
self.close()
self.sock = None
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