Commit 8a71316e authored by Matthias Gerstner's avatar Matthias Gerstner Committed by patelia
Browse files

choose_tls_port(): reuse socket and explicitly close it in all cases

This function only closes the socket on success, i.e. for each
unsuccessful bind attempt a socket "leaks". It does not actually leak,
because the Python interface implements reference counting. Still it is
unclean, because after successful bind the socket is explicitly closed.
So either the application is responsible for closing the socket, or not.
Since it is better not to rely on the implementation of the Python
interpreter and the socket module it should be prefered to always
explicitly close the socket.

Also this function opens a new socket for each port to try. This is
inefficient, since the same socket can be reused for testing. Therefore
only open and close a single socket.
parent 9a98bd6f
......@@ -180,8 +180,9 @@ def choose_tls_port(config):
ports_to_try = tls_ports[mid:] + tls_ports[:mid]
assert len(tls_ports) == len(ports_to_try)
sock = socket.socket()
for tls_port in ports_to_try:
sock = socket.socket()
try:
sock.bind(('localhost', tls_port))
sock.close()
......@@ -189,6 +190,8 @@ def choose_tls_port(config):
except socket.error:
continue
sock.close()
fatal_error('Failed to locate an available port in the range [%d, %d], '
'try specifying a different port range in %s'
% (lower_bound, upper_bound, CONFIG_FILE))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment