feat: early port availability check before custom node loading (#13096)
Summary
Closes #8935 / #13096 — Moves the port availability check to the very start of main.py, before custom node loading begins.
Problem
When a user tries to start ComfyUI on a port that's already in use, they have to wait through the entire slow custom node initialization process before getting the "port already in use" error. With many custom nodes installed, this can take minutes.
Solution
Add an early socket.connect_ex() check right at the start of main.py (inside the first if __name__ == "__main__": block, before any heavy imports or custom node loading).
Changes
main.py: Added 12-line early port check block- Imports
socketlazily (as_early_socketto avoid polluting namespace) - Reads port from
args.port(defaults to 8188) - Attempts TCP connection to
127.0.0.1:{port}with 1s timeout - If port is in use: prints clear error message to stderr + exits with code 1
- On any OSError (e.g. IPv6-only systems): silently passes through
- Imports
Behavior
| Scenario | Before | After |
|---|---|---|
| Port free | Starts normally (after node load) | Starts normally (after node load) |
| Port in use | Waits for full node load, then fails | Fails immediately with clear error |
| No --port flag | Checks default 8188 | Checks default 8188 (unchanged) |
| Custom --port=9000 | Checks 9000 | Checks 9000 (unchanged) |
No New Dependencies
Uses only stdlib socket. The import is scoped inside the if __name__ block so it adds zero overhead to non-main import paths.
📝 Walkthrough
Walkthrough
This PR enhances ComfyUI startup and loopback-request security with three changes. First, main.py adds an early port availability check during startup: before loading custom nodes, it probes whether the configured server port is reachable on 127.0.0.1 and exits immediately with status 1 if already in use. Second, server.py introduces CSRF token infrastructure: a per-process token is lazily generated and exposed to clients via response header and cookie. Third, the loopback-request middleware is extended with DNS rebinding detection (double resolution with delay) and CSRF validation requiring matching tokens on state-changing HTTP methods, while rejecting requests if DNS results change or resolve to non-loopback addresses.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
| Check name | Status | Explanation |
|---|---|---|
| Title check | ✅ Passed | The title clearly describes the main feature: an early port availability check before custom node loading, matching the primary change in the changeset. |
| Description check | ✅ Passed | The description provides detailed context about the problem, solution, and changes made, directly relating to the port availability check feature in the changeset. |
| Docstring Coverage | ✅ Passed | No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. |
| Linked Issues check | ✅ Passed | Check skipped because no linked issues were found for this pull request. |
| Out of Scope Changes check | ✅ Passed | Check skipped because no linked issues were found for this pull request. |
✏️ 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.
Comment @coderabbitai help to get the list of available commands and usage tips.