From 66013c8946eaf4f33f738f0e163c9ff4d9d749d4 Mon Sep 17 00:00:00 2001 From: Lingyi Wang Date: Wed, 26 Sep 2018 15:03:22 -0700 Subject: [PATCH 1/3] Change the host that is checked for port availability --- kolibri/utils/sanity_checks.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/kolibri/utils/sanity_checks.py b/kolibri/utils/sanity_checks.py index 9b45e146d55..541cf2f87dd 100644 --- a/kolibri/utils/sanity_checks.py +++ b/kolibri/utils/sanity_checks.py @@ -5,6 +5,7 @@ from .conf import OPTIONS from .server import get_status +from .server import LISTEN_ADDRESS from .server import NotRunning logger = logging.getLogger(__name__) @@ -24,7 +25,7 @@ def check_other_kolibri_running(port): except NotRunning: # In case that something other than Kolibri occupies the port, # check the port's availability. - check_port_availability('127.0.0.1', port) + check_port_availability(LISTEN_ADDRESS, port) def check_port_availability(host, port): From cc03798f29b0d95ae9efc6875f9bfd50f175f173 Mon Sep 17 00:00:00 2001 From: Lingyi Wang Date: Thu, 27 Sep 2018 10:39:54 -0700 Subject: [PATCH 2/3] use portend from cherrypy to check port availability --- kolibri/utils/sanity_checks.py | 14 ++++---------- kolibri/utils/tests/test_sanity_check.py | 9 ++++----- 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/kolibri/utils/sanity_checks.py b/kolibri/utils/sanity_checks.py index 541cf2f87dd..198d1017956 100644 --- a/kolibri/utils/sanity_checks.py +++ b/kolibri/utils/sanity_checks.py @@ -1,8 +1,9 @@ import logging import os -import socket import sys +import portend + from .conf import OPTIONS from .server import get_status from .server import LISTEN_ADDRESS @@ -29,22 +30,15 @@ def check_other_kolibri_running(port): def check_port_availability(host, port): - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - # This is to prevent the previous execution has left the socket - # in a TIME_WAIT start, and can't be immediately reused. - # From the bottom of https://docs.python.org/2/library/socket.html#example - s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) try: - s.bind((host, port)) - s.close() - except socket.error: + portend.free(host, port, timeout=2) + except portend.Timeout: # Port is occupied logger.error( "Port {} is occupied.\n" "Please check that you do not have other processes " "running on this port and try again.\n".format(port) ) - s.close() sys.exit(1) diff --git a/kolibri/utils/tests/test_sanity_check.py b/kolibri/utils/tests/test_sanity_check.py index 029a70c1ef0..44bd466bb6b 100644 --- a/kolibri/utils/tests/test_sanity_check.py +++ b/kolibri/utils/tests/test_sanity_check.py @@ -1,5 +1,4 @@ -import socket - +import portend from django.test import TestCase from mock import patch @@ -16,11 +15,11 @@ def test_other_kolibri_running(self, status_mock, logging_mock): logging_mock.assert_called() @patch('kolibri.utils.sanity_checks.logging.error') - @patch('kolibri.utils.sanity_checks.socket.socket') + @patch('kolibri.utils.sanity_checks.portend.free') @patch('kolibri.utils.sanity_checks.get_status') - def test_port_occupied(self, status_mock, socket_mock, logging_mock): + def test_port_occupied(self, status_mock, portend_mock, logging_mock): status_mock.side_effect = NotRunning('Kolibri not running') - socket_mock.return_value.bind.side_effect = socket.error + portend_mock.side_effect = portend.Timeout with self.assertRaises(SystemExit): cli.main({'start': True}) logging_mock.assert_called() From 5cdf2bcb14454d30701f4dfa3e801c96dd470830 Mon Sep 17 00:00:00 2001 From: indirectlylit Date: Wed, 3 Oct 2018 16:17:28 -0400 Subject: [PATCH 3/3] moving timeout to a constant --- kolibri/utils/sanity_checks.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/kolibri/utils/sanity_checks.py b/kolibri/utils/sanity_checks.py index 198d1017956..f271976fb91 100644 --- a/kolibri/utils/sanity_checks.py +++ b/kolibri/utils/sanity_checks.py @@ -11,6 +11,8 @@ logger = logging.getLogger(__name__) +PORT_AVAILABILITY_CHECK_TIMEOUT = 2 + def check_other_kolibri_running(port): try: @@ -31,7 +33,7 @@ def check_other_kolibri_running(port): def check_port_availability(host, port): try: - portend.free(host, port, timeout=2) + portend.free(host, port, timeout=PORT_AVAILABILITY_CHECK_TIMEOUT) except portend.Timeout: # Port is occupied logger.error(