From b45f6428f8844442a1c4e847cda68be4c5824452 Mon Sep 17 00:00:00 2001 From: Raphael Kubo da Costa Date: Tue, 26 Mar 2019 14:02:55 +0000 Subject: [PATCH] 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 Auto-Submit: Raphael Kubo da Costa Reviewed-by: Robbie Iannucci --- testing_support/fake_repos.py | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/testing_support/fake_repos.py b/testing_support/fake_repos.py index 493cf4174..43a72393a 100755 --- a/testing_support/fake_repos.py +++ b/testing_support/fake_repos.py @@ -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()