Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,19 @@ async def start_all():


if __name__ == "__main__":
# Early port check before loading custom nodes (which can be slow)
import socket
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))

Comment on lines +464 to +474
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.

# Running directly, just start ComfyUI.
logging.info("Python version: {}".format(sys.version))
logging.info("ComfyUI version: {}".format(comfyui_version.__version__))
Expand Down