From 83e93a0e0e240b822246140f71d8947777618793 Mon Sep 17 00:00:00 2001 From: "kyj@bowong.ai" Date: Fri, 25 Apr 2025 16:09:27 +0800 Subject: [PATCH 1/2] fix: Fix the problem of background thread pseudo-death caused by subprocess.PIPE blocking --- comfy_cli/command/launch.py | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/comfy_cli/command/launch.py b/comfy_cli/command/launch.py index 52885e66..90c7b9c6 100644 --- a/comfy_cli/command/launch.py +++ b/comfy_cli/command/launch.py @@ -4,6 +4,7 @@ import os import subprocess import sys +import tempfile import threading import uuid @@ -204,12 +205,16 @@ async def launch_and_monitor(cmd, listen, port): # NOTE: To prevent encoding error on Windows platform env = dict(os.environ, PYTHONIOENCODING="utf-8") env["COMFY_CLI_BACKGROUND"] = "true" + with tempfile.NamedTemporaryFile(mode="w+", delete=True) as tmp: + tmp_file_out = tmp.name + with tempfile.NamedTemporaryFile(mode="w+", delete=True) as tmp: + tmp_file_err = tmp.name if sys.platform == "win32": process = subprocess.Popen( cmd, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, + stdout=open(tmp_file_out, "w"), + stderr=open(tmp_file_err, "w"), text=True, env=env, encoding="utf-8", @@ -219,41 +224,45 @@ async def launch_and_monitor(cmd, listen, port): else: process = subprocess.Popen( cmd, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, + stdout=open(tmp_file_out, "w"), + stderr=open(tmp_file_err, "w"), text=True, env=env, encoding="utf-8", ) - def msg_hook(stream): + def msg_hook(temp_file_path): nonlocal log nonlocal logging_flag while True: - line = stream.readline() - if "Launching ComfyUI from:" in line: + with open(temp_file_path, 'r', encoding="utf-8") as file: + content = file.read() + # 检查内容中是否存在特定字符串 + if "Launching ComfyUI from:" in content: logging_flag = True - elif "To see the GUI go to:" in line: + + if "To see the GUI go to:" in content: print( f"[bold yellow]ComfyUI is successfully launched in the background.[/bold yellow]\nTo see the GUI go to: http://{listen}:{port}" ) ConfigManager().config["DEFAULT"][constants.CONFIG_KEY_BACKGROUND] = f"{(listen, port, process.pid)}" ConfigManager().write_config() - # NOTE: os.exit(0) doesn't work. + # 注意:os.exit(0) 不起作用。 os._exit(0) + # 如果需要,将内容添加到日志中 with logging_lock: if logging_flag: - log.append(line) - - stdout_thread = threading.Thread(target=msg_hook, args=(process.stdout,)) - stderr_thread = threading.Thread(target=msg_hook, args=(process.stderr,)) + log.append(content) + found_event = threading.Event() + stdout_thread = threading.Thread(target=msg_hook, args=(tmp_file_out,)) + stderr_thread = threading.Thread(target=msg_hook, args=(tmp_file_err,)) stdout_thread.start() stderr_thread.start() - + found_event.wait(timeout=300) process.wait() return log From b9d6cb91f209a53c67311397bc7e839d0244b5c3 Mon Sep 17 00:00:00 2001 From: "kyj@bowong.ai" Date: Mon, 28 Apr 2025 17:28:06 +0800 Subject: [PATCH 2/2] fix: Fix bind port fail case --- comfy_cli/command/launch.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/comfy_cli/command/launch.py b/comfy_cli/command/launch.py index 90c7b9c6..6b302ae6 100644 --- a/comfy_cli/command/launch.py +++ b/comfy_cli/command/launch.py @@ -6,6 +6,7 @@ import sys import tempfile import threading +import time import uuid import typer @@ -199,7 +200,7 @@ async def launch_and_monitor(cmd, listen, port): otherwise, return the log in case of failure. """ logging_flag = False - log = [] + log = None logging_lock = threading.Lock() # NOTE: To prevent encoding error on Windows platform @@ -252,17 +253,21 @@ def msg_hook(temp_file_path): # 注意:os.exit(0) 不起作用。 os._exit(0) + if "error while attempting to bind on address" in content: + print(f"Launch Error, [bold red]{listen}:{port}[/bold red] binding failed.") + os._exit(-1) + # 如果需要,将内容添加到日志中 with logging_lock: if logging_flag: - log.append(content) + log = content + + time.sleep(0.1) - found_event = threading.Event() stdout_thread = threading.Thread(target=msg_hook, args=(tmp_file_out,)) stderr_thread = threading.Thread(target=msg_hook, args=(tmp_file_err,)) stdout_thread.start() stderr_thread.start() - found_event.wait(timeout=300) process.wait() - return log + return log \ No newline at end of file