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
This commit is contained in:
Henrik Skupin 2016-08-15 13:55:43 +02:00
Родитель 775bb69951
Коммит 32877c55c9
1 изменённых файлов: 7 добавлений и 4 удалений

Просмотреть файл

@ -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