Skip to content
Snippets Groups Projects
Commit ec495242 authored by Craig Tiller's avatar Craig Tiller
Browse files

Add a mutex around allocate_port

parent ea525ebd
No related branches found
No related tags found
No related merge requests found
...@@ -46,7 +46,7 @@ import threading ...@@ -46,7 +46,7 @@ import threading
# increment this number whenever making a change to ensure that # increment this number whenever making a change to ensure that
# the changes are picked up by running CI servers # the changes are picked up by running CI servers
# note that all changes must be backwards compatible # note that all changes must be backwards compatible
_MY_VERSION = 10 _MY_VERSION = 11
if len(sys.argv) == 2 and sys.argv[1] == 'dump_version': if len(sys.argv) == 2 and sys.argv[1] == 'dump_version':
...@@ -70,6 +70,7 @@ print('port server running on port %d' % args.port) ...@@ -70,6 +70,7 @@ print('port server running on port %d' % args.port)
pool = [] pool = []
in_use = {} in_use = {}
mu = threading.Lock()
def refill_pool(max_timeout, req): def refill_pool(max_timeout, req):
...@@ -97,16 +98,21 @@ def refill_pool(max_timeout, req): ...@@ -97,16 +98,21 @@ def refill_pool(max_timeout, req):
def allocate_port(req): def allocate_port(req):
global pool global pool
global in_use global in_use
global mu
mu.acquire()
max_timeout = 600 max_timeout = 600
while not pool: while not pool:
refill_pool(max_timeout, req) refill_pool(max_timeout, req)
if not pool: if not pool:
req.log_message("failed to find ports: retrying soon") req.log_message("failed to find ports: retrying soon")
mu.release()
time.sleep(1) time.sleep(1)
mu.acquire()
max_timeout /= 2 max_timeout /= 2
port = pool[0] port = pool[0]
pool = pool[1:] pool = pool[1:]
in_use[port] = time.time() in_use[port] = time.time()
mu.release()
return port return port
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment