Bug 1410366 - Improve socket handling in wait_for_port draft
authorHenrik Skupin <mail@hskupin.info>
Fri, 20 Oct 2017 14:54:03 +0200
changeset 685477 1c6c13c3aa111e5a239efd209ccfb8de43d9fe31
parent 684879 cf180cd1542145fd30ef2f87b8694b759e75f50d
child 737153 4976c32eda9d01c0e97e19042909527bdf2be093
push id85934
push userbmo:hskupin@gmail.com
push dateTue, 24 Oct 2017 16:21:23 +0000
bugs1410366
milestone58.0a1
Bug 1410366 - Improve socket handling in wait_for_port The temporarily created sockets have to be removed immediately by the operating system. Otherwise those enter the TIME_WAIT state, and will be removed about 90s later. This can cause a pile of orphaned sockets. MozReview-Commit-ID: BGy9ZOjhjT0
testing/marionette/client/marionette_driver/marionette.py
--- a/testing/marionette/client/marionette_driver/marionette.py
+++ b/testing/marionette/client/marionette_driver/marionette.py
@@ -693,22 +693,30 @@ class Marionette(object):
                 return False
 
             sock = None
             try:
                 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                 sock.settimeout(0.5)
                 sock.connect((self.host, self.port))
                 data = sock.recv(16)
+
+                # If the application starts up very slowly (eg. Fennec on Android
+                # emulator) a response package has to be received first. Otherwise
+                # start_session will fail (see bug 1410366 comment 32 ff.)
                 if ":" in data:
                     return True
             except socket.error:
                 pass
             finally:
                 if sock is not None:
+                    try:
+                        sock.shutdown(socket.SHUT_RDWR)
+                    except:
+                        pass
                     sock.close()
 
             time.sleep(poll_interval)
 
         return False
 
     def raise_for_port(self, timeout=None):
         """Raise socket.timeout if no connection can be established.