Skip to content

[BOUNTY #8935] Run port check at the beginning of the main.py#13096

Open
zhaog100 wants to merge 1 commit intoComfy-Org:masterfrom
zhaog100:bounty-8935
Open

[BOUNTY #8935] Run port check at the beginning of the main.py#13096
zhaog100 wants to merge 1 commit intoComfy-Org:masterfrom
zhaog100:bounty-8935

Conversation

@zhaog100
Copy link
Copy Markdown

Closes #8935

Summary

Moves port availability check to the very start of main.py (before custom node loading), so users get an immediate error if the port is already in use instead of waiting through slow custom node initialization.

Changes

  • Added early port check using socket.connect_ex() on 127.0.0.1:{args.port}
  • Check runs before start_comfyui() which loads all custom nodes
  • Prints error message and exits with code 1 if port is taken
  • +13 lines, no new dependencies

Move port availability check to the beginning of main.py, before
custom node loading starts. This prevents users from waiting through
slow custom node initialization only to discover the port is taken.

Closes Comfy-Org#8935
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 21, 2026

📝 Walkthrough

Walkthrough

The change adds an early TCP port availability check during program initialization. When the program starts, it attempts to connect to 127.0.0.1 on the specified port with a short timeout. If the connection succeeds, indicating the port is already in use, the program logs an error and exits immediately via sys.exit(1). If an OSError occurs during the check, a warning is logged and startup continues normally. This validation executes before custom node initialization. No other startup logic or public APIs were modified.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: moving the port availability check to the beginning of main.py, directly addressing the bounty requirement.
Description check ✅ Passed The description is directly related to the changeset, explaining the purpose, implementation approach, and scope of the port check changes.
Linked Issues check ✅ Passed The PR successfully implements the core requirement from issue #8935: running port availability checks at startup before custom node loading to provide immediate feedback when a port is already in use.
Out of Scope Changes check ✅ Passed All changes are directly scoped to the stated objective of adding an early port check; no extraneous modifications were introduced.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Tip

CodeRabbit can use Trivy to scan for security misconfigurations and secrets in Infrastructure as Code files.

Add a .trivyignore file to your project to customize which findings Trivy reports.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
main.py (1)

467-467: Consider using args.listen instead of hardcoded 127.0.0.1.

The port check connects to 127.0.0.1, but the server binds to args.listen (line 455). For most cases this works fine since a process bound to 0.0.0.0 accepts connections on 127.0.0.1. However, if someone binds to a specific non-localhost interface (e.g., --listen 192.168.1.100), a conflict on that interface might not be detected.

This is a minor edge case, but for consistency you could check against the first address in args.listen (or keep 127.0.0.1 as a reasonable default for the common case).

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@main.py` at line 467, The port-check currently uses a hardcoded loopback
address when calling test_socket.connect_ex(('127.0.0.1', args.port)), which can
miss conflicts when the server actually binds to a specific interface; change
the connect_ex target to use the first address from args.listen (or args.listen
itself if it's a single address), falling back to '127.0.0.1' if args.listen is
empty or set to '0.0.0.0' — update the code that calls test_socket.connect_ex to
use that resolved listen address and keep args.port as the port to test.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@main.py`:
- Around line 464-474: The socket created as test_socket can leak if an OSError
occurs before test_socket.close() — modify the block that creates test_socket
(the code that calls socket.socket, test_socket.settimeout, and
test_socket.connect_ex) to ensure the socket is always closed by using a
try/finally or a with/context manager around socket.socket; keep the existing
OSError except branch but move the close into the finally (or rely on the
context manager) so the socket is closed regardless of exceptions and logging
behaviour (logging.error / logging.warning and sys.exit) remains unchanged.

---

Nitpick comments:
In `@main.py`:
- Line 467: The port-check currently uses a hardcoded loopback address when
calling test_socket.connect_ex(('127.0.0.1', args.port)), which can miss
conflicts when the server actually binds to a specific interface; change the
connect_ex target to use the first address from args.listen (or args.listen
itself if it's a single address), falling back to '127.0.0.1' if args.listen is
empty or set to '0.0.0.0' — update the code that calls test_socket.connect_ex to
use that resolved listen address and keep args.port as the port to test.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 9fac9b75-d9ee-42b3-8101-c6a40c075a0d

📥 Commits

Reviewing files that changed from the base of the PR and between a11f68d and b356974.

📒 Files selected for processing (1)
  • main.py

Comment on lines +464 to +474
try:
test_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
test_socket.settimeout(1)
result = test_socket.connect_ex(('127.0.0.1', args.port))
test_socket.close()
if result == 0:
logging.error("Port {} is already in use. Please specify a different port with --port or stop the process using this port.".format(args.port))
sys.exit(1)
except OSError as e:
logging.warning("Could not check port {}: {}".format(args.port, e))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Socket may leak if OSError occurs after creation.

If an OSError is raised after the socket is created (line 465) but before close() (line 468), the socket resource leaks. Use a try/finally or context manager to ensure cleanup.

🛠️ Proposed fix using try/finally
     try:
         test_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-        test_socket.settimeout(1)
-        result = test_socket.connect_ex(('127.0.0.1', args.port))
-        test_socket.close()
-        if result == 0:
-            logging.error("Port {} is already in use. Please specify a different port with --port or stop the process using this port.".format(args.port))
-            sys.exit(1)
+        try:
+            test_socket.settimeout(1)
+            result = test_socket.connect_ex(('127.0.0.1', args.port))
+            if result == 0:
+                logging.error("Port {} is already in use. Please specify a different port with --port or stop the process using this port.".format(args.port))
+                sys.exit(1)
+        finally:
+            test_socket.close()
     except OSError as e:
         logging.warning("Could not check port {}: {}".format(args.port, e))
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@main.py` around lines 464 - 474, The socket created as test_socket can leak
if an OSError occurs before test_socket.close() — modify the block that creates
test_socket (the code that calls socket.socket, test_socket.settimeout, and
test_socket.connect_ex) to ensure the socket is always closed by using a
try/finally or a with/context manager around socket.socket; keep the existing
OSError except branch but move the close into the finally (or rely on the
context manager) so the socket is closed regardless of exceptions and logging
behaviour (logging.error / logging.warning and sys.exit) remains unchanged.

@zhaog100
Copy link
Copy Markdown
Author

👋 Hi! Just a friendly ping on this PR.

This is a bounty task submission. Would appreciate your review when you have a moment! 🙏

If there are any changes needed, please let me know. Thanks! 🌶️

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