Skip to content

feat: Add port availability check at startup#12459

Open
andygoodluck wants to merge 1 commit intoComfy-Org:masterfrom
andygoodluck:feat-port-check
Open

feat: Add port availability check at startup#12459
andygoodluck wants to merge 1 commit intoComfy-Org:masterfrom
andygoodluck:feat-port-check

Conversation

@andygoodluck
Copy link

Summary

Adds port availability check at the beginning of main.py to prevent port conflicts that only surface after lengthy custom node loading.

Problem

Users would start ComfyUI, wait for custom nodes to load (which can take a long time), and only then discover that the port was already in use:

# Custom nodes loading... (can take minutes)
...
# Finally, after all loading:
OSError: [Errno 48] Address already in use

This wastes time and provides a poor user experience.

Solution

Check port availability before loading custom nodes. If the port is in use, exit immediately with a clear error message.

Changes

1. main.py

  • Added socket import
  • Added port availability check before creating PromptServer
  • Exits early with helpful error message if port is in use

2. port_check.py (new)

  • Utility module with port checking functions
  • check_port_available(): Check if a port is available
  • check_server_port(): Check and exit if port is in use
  • Can be reused for other port-related functionality

3. test_port_check.py (new)

  • Comprehensive tests for port checking functionality
  • Tests available ports, in-use ports, and timeout handling

Example Output

When port is available:

Port 8188 is available.
# ... continues loading custom nodes

When port is in use:

============================================================
PORT CONFLICT DETECTED
============================================================
Port 8188 on 0.0.0.0 is already in use.
Please either:
  1. Stop the other process using port 8188
  2. Start ComfyUI on a different port using: --port <PORT>
============================================================

Testing

python test_port_check.py

All tests pass:

  • ✅ Available port detection
  • ✅ In-use port detection
  • ✅ Timeout handling

Related Issue

Fixes #8935

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 Comfy-Org#8935
[Bounty]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Run port check at the beginning of the main.py

1 participant