This commit is contained in:
Jeff Wainwright 2026-01-07 10:16:15 -08:00 committed by GitHub
commit 2a8b9699d8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 40 additions and 1 deletions

View File

@ -1,6 +1,8 @@
import argparse
import enum
import os
import socket
import sys
import comfy.options
@ -36,7 +38,7 @@ class EnumAction(argparse.Action):
parser = argparse.ArgumentParser()
parser.add_argument("--listen", type=str, default="127.0.0.1", metavar="IP", nargs="?", const="0.0.0.0,::", help="Specify the IP address to listen on (default: 127.0.0.1). You can give a list of ip addresses by separating them with a comma like: 127.2.2.2,127.3.3.3 If --listen is provided without an argument, it defaults to 0.0.0.0,:: (listens on all ipv4 and ipv6)")
parser.add_argument("--port", type=int, default=8188, help="Set the listen port.")
parser.add_argument("--port", type=int, default=8188, help="Set the listen port. Use 0 for a random available port (useful for desktop apps).")
parser.add_argument("--tls-keyfile", type=str, help="Path to TLS (SSL) key file. Enables TLS, makes app accessible at https://... requires --tls-certfile to function")
parser.add_argument("--tls-certfile", type=str, help="Path to TLS (SSL) certificate file. Enables TLS, makes app accessible at https://... requires --tls-keyfile to function")
parser.add_argument("--enable-cors-header", type=str, default=None, metavar="ORIGIN", nargs="?", const="*", help="Enable CORS (Cross-Origin Resource Sharing) with optional origin or allow all with default '*'.")
@ -239,6 +241,13 @@ else:
if args.windows_standalone_build:
args.auto_launch = True
if args.port != 0:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.bind(('', args.port))
sock.close()
except OSError:
args.port = 0
if args.disable_auto_launch:
args.auto_launch = False

View File

@ -1182,6 +1182,11 @@ class PromptServer():
port = addr[1]
site = web.TCPSite(runner, address, port, ssl_context=ssl_ctx)
await site.start()
if port == 0 and site._server and site._server.sockets:
port = site._server.sockets[0].getsockname()[1]
if verbose:
logging.info("Random port assigned: {}".format(port))
if not hasattr(self, 'address'):
self.address = address #TODO: remove this

View File

@ -160,3 +160,28 @@ def test_base_path_change_clears_old(set_base_dir):
for name in ["controlnet", "diffusion_models", "text_encoders"]:
assert len(folder_paths.get_folder_paths(name)) == 2
def test_windows_standalone_random_port():
"""Test that --windows-standalone-build uses port 0 when port is in use"""
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('', 8188))
try:
with patch.object(sys, 'argv', ["main.py", "--windows-standalone-build"]):
reload(comfy.cli_args)
assert comfy.cli_args.args.port == 0
assert comfy.cli_args.args.windows_standalone_build
finally:
sock.close()
with patch.object(sys, 'argv', ["main.py", "--windows-standalone-build", "--port", "9999"]):
reload(comfy.cli_args)
assert comfy.cli_args.args.port == 9999 or comfy.cli_args.args.port == 0
assert comfy.cli_args.args.windows_standalone_build
with patch.object(sys, 'argv', ["main.py"]):
reload(comfy.cli_args)
assert comfy.cli_args.args.port == 8188
assert not comfy.cli_args.args.windows_standalone_build