fake_repos: Create a new socket for every connection attempt while waiting for a port

connect(2) says stream sockets may only succeed to connect once. This means
if the call to sock.connect() fails it will continue failing for the
entirety of the wait loop, and we will fail to wait for a port to bind even
when it eventually becomes available.

Make sure we create a new socket every time we are about to try to connect
to it.

Change-Id: I16d7dbea3590c5bf7f7240bdefcc5ec0bcd1edb5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/1528291
Commit-Queue: Raphael Kubo da Costa <raphael.kubo.da.costa@intel.com>
Auto-Submit: Raphael Kubo da Costa <raphael.kubo.da.costa@intel.com>
Reviewed-by: Robbie Iannucci <iannucci@chromium.org>
changes/91/1528291/3
Raphael Kubo da Costa 6 years ago committed by Commit Bot
parent e17d1ed64f
commit b45f6428f8

@ -88,25 +88,18 @@ def find_free_port(host, base_port):
def wait_for_port_to_bind(host, port, process):
sock = socket.socket()
if sys.platform == 'darwin':
# On Mac SnowLeopard, if we attempt to connect to the socket
# immediately, it fails with EINVAL and never gets a chance to
# connect (putting us into a hard spin and then failing).
# Linux doesn't need this.
time.sleep(0.2)
try:
start = datetime.datetime.utcnow()
maxdelay = datetime.timedelta(seconds=30)
while (datetime.datetime.utcnow() - start) < maxdelay:
sock = socket.socket()
try:
sock.connect((host, port))
logging.debug('%d is now bound' % port)
return
except (socket.error, EnvironmentError):
pass
# Sleep a little bit to avoid spinning too much.
time.sleep(0.2)
logging.debug('%d is still not bound' % port)
finally:
sock.close()

Loading…
Cancel
Save