diff --git a/rhodecode/lib/hooks_daemon.py b/rhodecode/lib/hooks_daemon.py --- a/rhodecode/lib/hooks_daemon.py +++ b/rhodecode/lib/hooks_daemon.py @@ -25,6 +25,7 @@ import tempfile import traceback import threading import socket +import random from BaseHTTPServer import BaseHTTPRequestHandler from SocketServer import TCPServer @@ -156,13 +157,19 @@ class HttpHooksCallbackDaemon(ThreadedHo def get_hostname(self): return socket.gethostname() or '127.0.0.1' - def get_available_port(self): - mysocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - mysocket.bind((self.get_hostname(), 0)) - port = mysocket.getsockname()[1] - mysocket.close() - del mysocket - return port + def get_available_port(self, min_port=20000, max_port=65535): + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + hostname = self.get_hostname() + + for _ in range(min_port, max_port): + pick_port = random.randint(min_port, max_port) + try: + sock.bind((hostname, pick_port)) + sock.close() + del sock + return pick_port + except OSError: + pass def _prepare(self, txn_id=None, host=None, port=None): if not host or host == "*":