Bug 1240601 - Set timeout on socket in transport.wait_for_port; r?automatedtester draft
authorAndreas Tolfsen <ato@mozilla.com>
Mon, 18 Jan 2016 22:12:55 +0000
changeset 326157 1914c51b3b78da8a410d0296b4912db615fe911b
parent 326148 211a4c710fb6af2cad10102c4cabc7cb525998b8
child 513559 dc57da3410fe758e929c8664d35f814ed546f999
push id10102
push useratolfsen@mozilla.com
push dateWed, 27 Jan 2016 14:10:21 +0000
reviewersautomatedtester
bugs1240601
milestone47.0a1
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.
testing/marionette/transport/marionette_transport/transport.py
--- 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