diff --git a/core/backend_log.py b/core/backend_log.py index b25faec0..4bb4fa9b 100644 --- a/core/backend_log.py +++ b/core/backend_log.py @@ -90,6 +90,10 @@ def _memory(): try: import app.logger as comfy_log except Exception: # noqa: BLE001 + # Without ComfyUI on the path, `app` resolves to our own folder (a namespace package) and stays cached, + # which would later hide ComfyUI's real `app` package. Forget it. + if getattr(sys.modules.get("app"), "__file__", True) is None: + sys.modules.pop("app", None) return None logs = comfy_log.logs if logs is not None and not isinstance(logs, Kept): diff --git a/core/tests/test_backend_log_app_import.py b/core/tests/test_backend_log_app_import.py new file mode 100644 index 00000000..8901766e --- /dev/null +++ b/core/tests/test_backend_log_app_import.py @@ -0,0 +1,15 @@ +import subprocess +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[2] + + +def test_failed_comfy_logger_import_leaves_no_stale_app_package(): + """Outside ComfyUI, `app` is our own folder; it must not stay cached and hide ComfyUI's real `app` later.""" + code = ( + "import sys; sys.path.insert(0, %r); import core.backend_log; " + "print('app' in sys.modules and getattr(sys.modules['app'], '__file__', 1) is None)" + ) % str(ROOT) + out = subprocess.run([sys.executable, "-I", "-c", code], capture_output=True, text=True, cwd=str(ROOT)) + assert out.stdout.strip().endswith("False"), out.stdout + out.stderr