From 32877c55c91846a4040e17cf60ad790874d71fc4 Mon Sep 17 00:00:00 2001 From: Henrik Skupin Date: Mon, 15 Aug 2016 13:55:43 +0200 Subject: [PATCH] Bug 1293982 - TcpTransport.close() has to force a shutdown of the socket. r=ato When the Marionette client closes the socket connection it currently only calls close() on it. This will actually decrease the reference counter, and keeps the OS socket around until it gets garbage collected by the system. This can cause port in use failures for socket connections created shortly after eg. restarts of the appication. By using shutdown() the client indicates that the socket has to be closed immediately. MozReview-Commit-ID: 3jUgaWnujLc --HG-- extra : rebase_source : 3111d041585a8e50258d2be9bfef55708f80d4b4 --- .../marionette/client/marionette_driver/transport.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/testing/marionette/client/marionette_driver/transport.py b/testing/marionette/client/marionette_driver/transport.py index 8c725820d414..ad1d31031b29 100644 --- a/testing/marionette/client/marionette_driver/transport.py +++ b/testing/marionette/client/marionette_driver/transport.py @@ -132,9 +132,7 @@ class TcpTransport(object): self.application_type = None self.last_id = 0 self.expected_response = None - - self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - self.sock.settimeout(self.socket_timeout) + self.sock = None def _unmarshal(self, packet): msg = None @@ -210,6 +208,9 @@ class TcpTransport(object): Returns a tuple of the protocol level and the application type. """ try: + self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + self.sock.settimeout(self.socket_timeout) + self.sock.connect((self.addr, self.port)) except: # Unset self.sock so that the next attempt to send will cause @@ -276,11 +277,12 @@ class TcpTransport(object): def close(self): """Close the socket.""" if self.sock: + self.sock.shutdown(socket.SHUT_RDWR) self.sock.close() + self.sock = None def __del__(self): self.close() - self.sock = None def wait_for_port(host, port, timeout=60): @@ -294,6 +296,7 @@ def wait_for_port(host, port, timeout=60): sock.settimeout(0.5) sock.connect((host, port)) data = sock.recv(16) + sock.shutdown(socket.SHUT_RDWR) sock.close() if ":" in data: return True