Skip to content
10 changes: 9 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,14 @@ def setup_database():
logging.error(f"Failed to initialize database. Please ensure you have installed the latest requirements. If the error persists, please report this as in future the database will be required: {e}")


def prompt_worker_failfast(*args, **kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious, what happens in your test case if you infinite loop here?

Instead of os._exit() if you just while(True) with your exception discard does is recover?

Copy link
Author

@lixiang007666 lixiang007666 Jan 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point.
In fact, by the time we reach os._exit(), the prompt_worker thread is already no longer alive. If we replace os._exit() with while True, the process won’t exit; requests will continue to be enqueued, but the queue will never be consumed.

try:
return prompt_worker(*args, **kwargs)
except Exception:
logging.critical("Unhandled exception in prompt_worker; exiting process.", exc_info=True)
os._exit(1)


def start_comfyui(asyncio_loop=None):
"""
Starts the ComfyUI server using the provided asyncio event loop or creates a new one.
Expand Down Expand Up @@ -370,7 +378,7 @@ def start_comfyui(asyncio_loop=None):
prompt_server.add_routes()
hijack_progress(prompt_server)

threading.Thread(target=prompt_worker, daemon=True, args=(prompt_server.prompt_queue, prompt_server,)).start()
threading.Thread(target=prompt_worker_failfast, daemon=True, args=(prompt_server.prompt_queue, prompt_server,)).start()

if args.quick_test_for_ci:
exit(0)
Expand Down