diff --git a/comfy/cli_args.py b/comfy/cli_args.py index 0a0bf2f30..7b6078f73 100644 --- a/comfy/cli_args.py +++ b/comfy/cli_args.py @@ -37,6 +37,7 @@ 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("--unix-socket", type=str, default=None, help="Path to Unix socket file to listen on. When specified, --listen and --port are ignored.") 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 '*'.") diff --git a/main.py b/main.py index 8905fd09a..6ae361b41 100644 --- a/main.py +++ b/main.py @@ -320,13 +320,15 @@ def prompt_worker(q, server_instance): asset_seeder.resume() -async def run(server_instance, address='', port=8188, verbose=True, call_on_start=None): - addresses = [] - for addr in address.split(","): - addresses.append((addr, port)) - await asyncio.gather( - server_instance.start_multi_address(addresses, call_on_start, verbose), server_instance.publish_loop() - ) +async def run(server_instance, address='', port=8188, unix_socket=None, verbose=True, call_on_start=None): + if unix_socket: + start_coro = server_instance.start_unix_socket(unix_socket, verbose) + else: + addresses = [] + for addr in address.split(","): + addresses.append((addr, port)) + start_coro = server_instance.start_multi_address(addresses, call_on_start, verbose) + await asyncio.gather(start_coro, server_instance.publish_loop()) def hijack_progress(server_instance): def hook(value, total, preview_image, prompt_id=None, node_id=None): @@ -452,7 +454,7 @@ def start_comfyui(asyncio_loop=None): async def start_all(): await prompt_server.setup() - await run(prompt_server, address=args.listen, port=args.port, verbose=not args.dont_print_server, call_on_start=call_on_start) + await run(prompt_server, address=args.listen, port=args.port, unix_socket=args.unix_socket, verbose=not args.dont_print_server, call_on_start=call_on_start) # Returning these so that other code can integrate with the ComfyUI loop and server return asyncio_loop, prompt_server, start_all diff --git a/server.py b/server.py index 85a8964be..d8401ca41 100644 --- a/server.py +++ b/server.py @@ -1,5 +1,6 @@ import os import sys +import stat import asyncio import traceback import time @@ -1216,6 +1217,38 @@ class PromptServer(): if call_on_start is not None: call_on_start(scheme, self.address, self.port) + async def start_unix_socket(self, unix_socket, verbose=True): + """Start the server listening on a Unix domain socket instead of TCP.""" + if sys.platform == 'win32': + raise RuntimeError("Unix sockets are not supported on Windows. Please use --listen and --port instead.") + + if verbose: + logging.info("Starting server\n") + + if os.path.lexists(unix_socket): + st_mode = os.lstat(unix_socket).st_mode + if not stat.S_ISSOCK(st_mode): + raise RuntimeError(f"Refusing to remove non-socket path: {unix_socket}") + os.unlink(unix_socket) + + runner = web.AppRunner(self.app, access_log=None) + await runner.setup() + try: + site = web.UnixSite(runner, unix_socket) + await site.start() + os.chmod(unix_socket, 0o660) + except OSError as e: + await runner.cleanup() + raise RuntimeError(f"Failed to set socket permissions: {e}") from e + except Exception: + await runner.cleanup() + raise + self.address = unix_socket + self.port = None + self.unix_socket = unix_socket + if verbose: + logging.info("Listening on Unix socket: {}".format(unix_socket)) + def add_on_prompt_handler(self, handler): self.on_prompt_handlers.append(handler)