Commit b45f6428 authored by Raphael Kubo da Costa's avatar Raphael Kubo da Costa Committed by Commit Bot

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: 's avatarRobbie Iannucci <iannucci@chromium.org>
parent e17d1ed6
......@@ -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()
......
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