From 304201c96dc4473c62466957c1131ad690108e31 Mon Sep 17 00:00:00 2001 From: "Dr.Lt.Data" Date: Tue, 21 May 2024 21:29:54 +0900 Subject: [PATCH 1/4] fix: pipe error from tqdm in launch --background --- comfy_cli/cmdline.py | 65 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 55 insertions(+), 10 deletions(-) diff --git a/comfy_cli/cmdline.py b/comfy_cli/cmdline.py index 4244d108..6897e1c1 100644 --- a/comfy_cli/cmdline.py +++ b/comfy_cli/cmdline.py @@ -371,6 +371,7 @@ 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" if sys.platform == "win32": process = subprocess.Popen( @@ -492,25 +493,69 @@ def launch_comfyui(extra): ConfigManager().get_config_path(), "tmp", str(uuid.uuid4()) ) new_env["__COMFY_CLI_SESSION__"] = session_path + new_env["PYTHONENCODING"] = "utf-8" # To minimize the possibility of leaving residue in the tmp directory, use files instead of directories. reboot_path = os.path.join(session_path + ".reboot") extra = extra if extra is not None else [] - while True: - res = subprocess.run( - [sys.executable, "main.py"] + extra, env=new_env, check=False - ) + process = None + + if "COMFY_CLI_BACKGROUND" not in os.environ: + # If not running in background mode, there's no need to use popen. This can prevent the issue of linefeeds occurring with tqdm. + while True: + res = subprocess.run( + [sys.executable, "main.py"] + extra, env=new_env, check=False + ) + + if reboot_path is None: + print("[bold red]ComfyUI is not installed.[/bold red]\n") + os.exit(res) + + if not os.path.exists(reboot_path): + os.exit(res) + + os.remove(reboot_path) + else: + # If running in background mode without using a popen, broken pipe errors may occur when flushing stdout/stderr. + def redirector_stderr(): + while True: + if process is not None: + print(process.stderr.readline(), end="") + + def redirector_stdout(): + while True: + if process is not None: + print(process.stderr.readline(), end="") + + t1 = threading.Thread(target=redirector_stderr).start() + t2 = threading.Thread(target=redirector_stdout).start() + + try: + while True: + process = subprocess.Popen( + [sys.executable, "main.py"] + extra, + text=True, + env=new_env, + encoding="utf-8", + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + ) + + process.wait() - if reboot_path is None: - print("[bold red]ComfyUI is not installed.[/bold red]\n") - exit(res) + if reboot_path is None: + print("[bold red]ComfyUI is not installed.[/bold red]\n") + os._exit(process.pid) - if not os.path.exists(reboot_path): - exit(res) + if not os.path.exists(reboot_path): + os._exit(process.pid) - os.remove(reboot_path) + os.remove(reboot_path) + except KeyboardInterrupt: + if process is not None: + os._exit(1) @app.command(help="Stop background ComfyUI") From d88f790f481ec7b667d06cd344e7c8186bdc1b49 Mon Sep 17 00:00:00 2001 From: ltdrdata Date: Tue, 21 May 2024 22:12:27 +0900 Subject: [PATCH 2/4] win fix --- comfy_cli/cmdline.py | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/comfy_cli/cmdline.py b/comfy_cli/cmdline.py index 6897e1c1..0a08a4ca 100644 --- a/comfy_cli/cmdline.py +++ b/comfy_cli/cmdline.py @@ -400,7 +400,6 @@ def msg_hook(stream): while True: line = stream.readline() - if "Launching ComfyUI from:" in line: logging_flag = True elif "To see the GUI go to:" in line: @@ -527,21 +526,33 @@ def redirector_stderr(): def redirector_stdout(): while True: if process is not None: - print(process.stderr.readline(), end="") + print(process.stdout.readline(), end="") t1 = threading.Thread(target=redirector_stderr).start() t2 = threading.Thread(target=redirector_stdout).start() try: while True: - process = subprocess.Popen( - [sys.executable, "main.py"] + extra, - text=True, - env=new_env, - encoding="utf-8", - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - ) + if sys.platform == "win32": + process = subprocess.Popen( + [sys.executable, "main.py"] + extra, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, + env=new_env, + encoding="utf-8", + shell=True, # win32 only + creationflags=subprocess.CREATE_NEW_PROCESS_GROUP, # win32 only + ) + else: + process = subprocess.Popen( + [sys.executable, "main.py"] + extra, + text=True, + env=new_env, + encoding="utf-8", + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + ) process.wait() From 23901ef4b544b1ffdf8fa2f3d6d294bd5acedced Mon Sep 17 00:00:00 2001 From: "Dr.Lt.Data" Date: Tue, 21 May 2024 22:56:30 +0900 Subject: [PATCH 3/4] pylint directive to bypass os._exit --- comfy_cli/cmdline.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/comfy_cli/cmdline.py b/comfy_cli/cmdline.py index 0a08a4ca..9443b6c3 100644 --- a/comfy_cli/cmdline.py +++ b/comfy_cli/cmdline.py @@ -412,6 +412,7 @@ def msg_hook(stream): ConfigManager().write_config() # NOTE: os.exit(0) doesn't work. + # pylint: disable=protected-access os._exit(0) with logging_lock: @@ -478,6 +479,7 @@ def background_launch(extra): console.print(f"\n[bold red]Execution error: failed to launch ComfyUI[/bold red]\n") # NOTE: os.exit(0) doesn't work + # pylint: disable=protected-access os._exit(1) @@ -528,8 +530,8 @@ def redirector_stdout(): if process is not None: print(process.stdout.readline(), end="") - t1 = threading.Thread(target=redirector_stderr).start() - t2 = threading.Thread(target=redirector_stdout).start() + threading.Thread(target=redirector_stderr).start() + threading.Thread(target=redirector_stdout).start() try: while True: @@ -558,14 +560,17 @@ def redirector_stdout(): if reboot_path is None: print("[bold red]ComfyUI is not installed.[/bold red]\n") + # pylint: disable=protected-access os._exit(process.pid) if not os.path.exists(reboot_path): + # pylint: disable=protected-access os._exit(process.pid) os.remove(reboot_path) except KeyboardInterrupt: if process is not None: + # pylint: disable=protected-access os._exit(1) From 998e0aab3b68f6536449363466b7cb7aba9c3ab5 Mon Sep 17 00:00:00 2001 From: "Dr.Lt.Data" Date: Tue, 21 May 2024 23:02:04 +0900 Subject: [PATCH 4/4] rollback useless bypass fix: os.exit -> exit --- comfy_cli/cmdline.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/comfy_cli/cmdline.py b/comfy_cli/cmdline.py index 9443b6c3..37247691 100644 --- a/comfy_cli/cmdline.py +++ b/comfy_cli/cmdline.py @@ -412,7 +412,6 @@ def msg_hook(stream): ConfigManager().write_config() # NOTE: os.exit(0) doesn't work. - # pylint: disable=protected-access os._exit(0) with logging_lock: @@ -479,7 +478,6 @@ def background_launch(extra): console.print(f"\n[bold red]Execution error: failed to launch ComfyUI[/bold red]\n") # NOTE: os.exit(0) doesn't work - # pylint: disable=protected-access os._exit(1) @@ -512,10 +510,10 @@ def launch_comfyui(extra): if reboot_path is None: print("[bold red]ComfyUI is not installed.[/bold red]\n") - os.exit(res) + exit(res) if not os.path.exists(reboot_path): - os.exit(res) + exit(res) os.remove(reboot_path) else: @@ -560,17 +558,14 @@ def redirector_stdout(): if reboot_path is None: print("[bold red]ComfyUI is not installed.[/bold red]\n") - # pylint: disable=protected-access os._exit(process.pid) if not os.path.exists(reboot_path): - # pylint: disable=protected-access os._exit(process.pid) os.remove(reboot_path) except KeyboardInterrupt: if process is not None: - # pylint: disable=protected-access os._exit(1)