From e4604013a8c3acfe15e83bc63ca64246a7f512d5 Mon Sep 17 00:00:00 2001 From: calmini Date: Fri, 25 Apr 2025 15:28:30 +0800 Subject: [PATCH] fix: add port availability check and improve status message for MCPRouter --- src/mcpm/commands/router.py | 39 +++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/mcpm/commands/router.py b/src/mcpm/commands/router.py index cf2c16a9..aed5a6c9 100644 --- a/src/mcpm/commands/router.py +++ b/src/mcpm/commands/router.py @@ -6,6 +6,7 @@ import os import secrets import signal +import socket import subprocess import sys import uuid @@ -40,6 +41,35 @@ def is_process_running(pid): except Exception: return False +def is_port_listening(host, port) -> bool: + """ + Check if the specified (host, port) is being listened on. + + Args: + host: The host to check + port: The port to check + + Returns: + True if the (host, port) is being listened on + """ + sock = None + try: + # Try to connect to the port + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.settimeout(1) + connected_host = "127.0.0.1" if host == "0.0.0.0" else host + result = sock.connect_ex((connected_host, port)) + # result == 0 means connection successful, which means port is in use + # result != 0 means connection failed, which means port is not in use + # result == 61 means ECONNREFUSED + return result == 0 + except Exception as e: + logger.error(f"Error checking host {host} and port {port}: {e}") + return False + finally: + if sock: + sock.close() + def read_pid_file(): """read the pid file and return the process id, if the file does not exist or the process is not running, return None""" @@ -270,6 +300,15 @@ def router_status(): # check process status pid = read_pid_file() if pid: + if not is_port_listening(host, port): + console.print( + f"[bold yellow]Notice:[/] [bold cyan]{host}:{port}[/] is not yet accepting connections. The service may still be starting up. Please wait a few seconds and try again." + ) + console.print( + f"[yellow]If this message persists after waiting, please check the log for more details.[/] (Log file: {LOG_DIR / 'router_access.log'})" + ) + return + console.print(f"[bold green]MCPRouter is running[/] at http://{host}:{port} (PID: {pid})") share_config = ConfigManager().read_share_config() if share_config.get("pid"):