mirror of
https://github.com/comfyanonymous/ComfyUI.git
synced 2026-06-17 21:39:45 +08:00
Adds port availability check before loading custom nodes to prevent port conflicts that only surface after lengthy custom node loading. Changes: - Added socket import for port checking - Added port availability check in main.py before creating PromptServer - Created port_check.py utility module with helper functions - Created test_port_check.py with comprehensive tests The check will: 1. Verify the server port is available before any heavy loading 2. Exit early with clear error message if port is in use 3. Provide helpful instructions for resolving the conflict Fixes #8935 [Bounty]
68 lines
1.9 KiB
Python
68 lines
1.9 KiB
Python
"""
|
|
Port check utility for ComfyUI
|
|
Checks if the server port is available before starting
|
|
to avoid port conflicts after custom nodes load
|
|
"""
|
|
|
|
import socket
|
|
import sys
|
|
import logging
|
|
|
|
|
|
def check_port_available(host, port, timeout=1):
|
|
"""
|
|
Check if a port is available for binding.
|
|
|
|
Args:
|
|
host: Host address to check
|
|
port: Port number to check
|
|
timeout: Connection timeout in seconds
|
|
|
|
Returns:
|
|
True if port is available, False otherwise
|
|
"""
|
|
try:
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
|
|
sock.settimeout(timeout)
|
|
result = sock.connect_ex((host, port))
|
|
if result == 0:
|
|
# Port is in use
|
|
return False
|
|
else:
|
|
# Port is available
|
|
return True
|
|
except socket.error as e:
|
|
logging.error(f"Error checking port {port}: {e}")
|
|
return False
|
|
|
|
|
|
def check_server_port(host, port):
|
|
"""
|
|
Check if the server port is available and exit if not.
|
|
This should be called early in the startup process.
|
|
|
|
Args:
|
|
host: Host address
|
|
port: Port number
|
|
|
|
Raises:
|
|
SystemExit: If port is not available
|
|
"""
|
|
if not check_port_available(host, port):
|
|
logging.error(f"\n{'='*60}")
|
|
logging.error(f"PORT CONFLICT DETECTED")
|
|
logging.error(f"{'='*60}")
|
|
logging.error(f"Port {port} on {host} is already in use.")
|
|
logging.error(f"Please either:")
|
|
logging.error(f" 1. Stop the other process using port {port}")
|
|
logging.error(f" 2. Start ComfyUI on a different port using: --port <PORT>")
|
|
logging.error(f"{'='*60}\n")
|
|
sys.exit(1)
|
|
else:
|
|
logging.info(f"Port {port} is available.")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
# Simple test
|
|
print(f"Checking port 8188: {check_port_available('127.0.0.1', 8188)}")
|