From c9f765813ce85628f91f32a0c044aefa17200102 Mon Sep 17 00:00:00 2001 From: Safihre Date: Fri, 21 Apr 2017 15:35:10 +0200 Subject: [PATCH] Exit more cleanly when ports are occupied --- SABnzbd.py | 91 +++++++++++++++++++++++++++++++------------------------------- 1 file changed, 46 insertions(+), 45 deletions(-) diff --git a/SABnzbd.py b/SABnzbd.py index 9cf13e3..7c71a01 100755 --- a/SABnzbd.py +++ b/SABnzbd.py @@ -669,7 +669,11 @@ def is_sabnzbd_running(url): def find_free_port(host, currentport): - """ Return a free port, 0 when nothing is free """ + """ Return a free port if allowed, 0 when nothing is free """ + if sabnzbd.cfg.fixed_ports(): + # Port found before, so we bail out + raise IOError + n = 0 while n < 10 and currentport <= 49151: try: @@ -1042,51 +1046,48 @@ def main(): if url and check_for_sabnzbd(url, upload_nzbs, autobrowser): exit_sab(0) - # We only performe the free-port check on first start-up - # because we want to be sure we can bind to something! - if not sabnzbd.cfg.fixed_ports(): - # SSL - if enable_https: - port = https_port or cherryport - try: - cherrypy.process.servers.check_port(browserhost, port, timeout=0.05) - except IOError, error: - if str(error) == 'Port not bound.': - pass - else: - if not url: - url = 'https://%s:%s/sabnzbd/api?' % (browserhost, port) - if new_instance or not check_for_sabnzbd(url, upload_nzbs, autobrowser): - newport = find_free_port(browserhost, port) - if newport > 0: - # Save the new port - if https_port: - https_port = newport - sabnzbd.cfg.https_port.set(newport) - else: - # In case HTTPS == HTTP port - http_port = newport - sabnzbd.cfg.port.set(newport) - except: - Bail_Out(browserhost, cherryport, '49') + # SSL + if enable_https: + port = https_port or cherryport + try: + cherrypy.process.servers.check_port(browserhost, port, timeout=0.05) + except IOError, error: + if str(error) == 'Port not bound.': + pass + else: + if not url: + url = 'https://%s:%s/sabnzbd/api?' % (browserhost, port) + if new_instance or not check_for_sabnzbd(url, upload_nzbs, autobrowser): + newport = find_free_port(browserhost, port) + if newport > 0: + # Save the new port + if https_port: + https_port = newport + sabnzbd.cfg.https_port.set(newport) + else: + # In case HTTPS == HTTP port + http_port = newport + sabnzbd.cfg.port.set(newport) + except: + Bail_Out(browserhost, cherryport, '49') - # NonSSL check if there's no HTTPS or we only use 1 port - if not (enable_https and not https_port): - try: - cherrypy.process.servers.check_port(browserhost, cherryport, timeout=0.05) - except IOError, error: - if str(error) == 'Port not bound.': - pass - else: - if not url: - url = 'http://%s:%s/sabnzbd/api?' % (browserhost, cherryport) - if new_instance or not check_for_sabnzbd(url, upload_nzbs, autobrowser): - port = find_free_port(browserhost, cherryport) - if port > 0: - sabnzbd.cfg.cherryport.set(port) - cherryport = port - except: - Bail_Out(browserhost, cherryport, '49') + # NonSSL check if there's no HTTPS or we only use 1 port + if not (enable_https and not https_port): + try: + cherrypy.process.servers.check_port(browserhost, cherryport, timeout=0.05) + except IOError, error: + if str(error) == 'Port not bound.': + pass + else: + if not url: + url = 'http://%s:%s/sabnzbd/api?' % (browserhost, cherryport) + if new_instance or not check_for_sabnzbd(url, upload_nzbs, autobrowser): + port = find_free_port(browserhost, cherryport) + if port > 0: + sabnzbd.cfg.cherryport.set(port) + cherryport = port + except: + Bail_Out(browserhost, cherryport, '49') # We found a port, now we never check again sabnzbd.cfg.fixed_ports.set(True)