diff --git a/modules/p2ptrust/p2ptrust.py b/modules/p2ptrust/p2ptrust.py index 0f5befa03..44e6614c9 100644 --- a/modules/p2ptrust/p2ptrust.py +++ b/modules/p2ptrust/p2ptrust.py @@ -158,6 +158,8 @@ def get_local_IP(self): def get_available_port(self) -> int: for port in range(32768, 65535): + if port == self.redis_port: + continue sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: sock.bind(("0.0.0.0", port)) diff --git a/slips_files/core/database/redis_db/database.py b/slips_files/core/database/redis_db/database.py index 409e8d034..ac6f59a95 100644 --- a/slips_files/core/database/redis_db/database.py +++ b/slips_files/core/database/redis_db/database.py @@ -1,5 +1,7 @@ # SPDX-FileCopyrightText: 2021 Sebastian Garcia # SPDX-License-Identifier: GPL-2.0-only +import socket + from slips_files.common.printer import Printer from slips_files.common.slips_utils import utils from slips_files.common.parsers.config_parser import ConfigParser @@ -252,6 +254,9 @@ def init_redis_server(cls) -> Tuple[bool, str]: # starts the redis server using cli. # we don't need that when using -k cls._start_a_redis_server() + all_good, err = cls._confirm_redis_is_listening() + if not all_good: + return False, err connected, err = cls.connect_to_redis_server() if not connected: @@ -314,11 +319,35 @@ def _connect(port: int, db: int) -> redis.StrictRedis: health_check_interval=20, ) + @classmethod + def _confirm_redis_is_listening(cls, timeout: float = 5.0) -> (bool, str): + """ + Polls the redis port to confirm Redis is really listening + :param timeout: how long to keep polling before raising runtime error + """ + start = time.time() + while time.time() - start < timeout: + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: + sock.settimeout(0.2) + try: + sock.connect(("127.0.0.1", cls.redis_port)) + return True, "" # Redis is up + except (ConnectionRefusedError, OSError): + time.sleep(0.2) + + # If we reach here, port never opened + return False, ( + f"_confirm_redis_is_listening: Redis failed to start on " + f"{cls.redis_port}" + ) + @classmethod def _start_a_redis_server(cls) -> bool: cmd = ( - f"redis-server {cls._conf_file} --port {cls.redis_port} " - f" --daemonize yes" + f"redis-server {cls._conf_file} " + f"--port {cls.redis_port} " + f"--bind 127.0.0.1 " + f"--daemonize yes" ) process = subprocess.Popen( cmd,