From 66e6b68d82086460b4d01579477cea0517e97c95 Mon Sep 17 00:00:00 2001 From: Avaya Aggarwal <119044997+OnePunchMonk@users.noreply.github.com> Date: Sat, 10 Oct 2026 06:35:12 +0530 Subject: [PATCH] [diffusion] comfyui: report the real runtime import error (#43178) --- .../ComfyUI_SGLDiffusion/core/generator.py | 14 ++++- .../ComfyUI_SGLDiffusion/executors/base.py | 13 ++++- .../test/unit/test_comfyui_generator.py | 52 +++++++++++++++++++ 3 files changed, 75 insertions(+), 4 deletions(-) diff --git a/python/sglang/multimodal_gen/apps/ComfyUI_SGLDiffusion/core/generator.py b/python/sglang/multimodal_gen/apps/ComfyUI_SGLDiffusion/core/generator.py index 27a12af829..59f733d5e0 100644 --- a/python/sglang/multimodal_gen/apps/ComfyUI_SGLDiffusion/core/generator.py +++ b/python/sglang/multimodal_gen/apps/ComfyUI_SGLDiffusion/core/generator.py @@ -101,10 +101,16 @@ def _load_state_dict_for_detection(model_path: str): try: from sglang.multimodal_gen import DiffGenerator -except ImportError: +except ImportError as exc: + # Keep the nodes importable so ComfyUI shows this; fail clearly on first use. + _RUNTIME_IMPORT_ERROR: ImportError | None = exc logger.error( - "Error: sglang.multimodal_gen is not installed. Please install it using 'pip install sglang[diffusion]'" + "Failed to import the SGLang diffusion runtime: %r. Install the diffusion " + "extras with 'pip install sglang[diffusion]'.", + exc, ) +else: + _RUNTIME_IMPORT_ERROR = None def _load_executor_classes(): @@ -185,6 +191,10 @@ class SGLDiffusionGenerator: """Initialize the diffusion generator.""" if self.generator is not None: return self.generator + if _RUNTIME_IMPORT_ERROR is not None: + raise RuntimeError( + "SGLang diffusion runtime failed to import" + ) from _RUNTIME_IMPORT_ERROR if kwargs is None: kwargs = {} # Set comfyui_mode for ComfyUI integration diff --git a/python/sglang/multimodal_gen/apps/ComfyUI_SGLDiffusion/executors/base.py b/python/sglang/multimodal_gen/apps/ComfyUI_SGLDiffusion/executors/base.py index 6db631867a..8dbdef8602 100644 --- a/python/sglang/multimodal_gen/apps/ComfyUI_SGLDiffusion/executors/base.py +++ b/python/sglang/multimodal_gen/apps/ComfyUI_SGLDiffusion/executors/base.py @@ -9,10 +9,15 @@ import torch try: from sglang.multimodal_gen.configs.sample.sampling_params import SamplingParams from sglang.multimodal_gen.runtime.entrypoints.utils import prepare_request -except ImportError: +except ImportError as exc: + # Keep the nodes importable so ComfyUI shows this; fail clearly on first use. + _RUNTIME_IMPORT_ERROR: ImportError | None = exc print( - "Error: sglang.multimodal_gen is not installed. Please install it using 'pip install sglang[diffusion]'" + f"Error: failed to import the SGLang diffusion runtime: {exc!r}. " + "Install the diffusion extras with 'pip install sglang[diffusion]'." ) +else: + _RUNTIME_IMPORT_ERROR = None class SGLDiffusionExecutor(torch.nn.Module): @@ -115,6 +120,10 @@ class SGLDiffusionExecutor(torch.nn.Module): } def _execute_packed(self, packed, x, timestep): + if _RUNTIME_IMPORT_ERROR is not None: + raise RuntimeError( + "SGLang diffusion runtime failed to import" + ) from _RUNTIME_IMPORT_ERROR ensure = getattr(self, "_ensure_runtime", None) if ensure is not None: ensure(self) diff --git a/python/sglang/multimodal_gen/test/unit/test_comfyui_generator.py b/python/sglang/multimodal_gen/test/unit/test_comfyui_generator.py index d89d7206a1..47828da723 100644 --- a/python/sglang/multimodal_gen/test/unit/test_comfyui_generator.py +++ b/python/sglang/multimodal_gen/test/unit/test_comfyui_generator.py @@ -83,3 +83,55 @@ def test_kill_generator_only_touches_owned_workers() -> None: runtime.kill_generator() assert owned.terminated is True assert owned.killed is False + + +def _load_with_blocked_import(relpath, module_name, blocked): + """Import a private copy of a plugin module while ``blocked`` fails to import.""" + import importlib.util + import sys + from pathlib import Path + from unittest import mock + + import sglang.multimodal_gen.apps.ComfyUI_SGLDiffusion as plugin + + path = Path(plugin.__file__).parent / relpath + spec = importlib.util.spec_from_file_location(module_name, path) + module = importlib.util.module_from_spec(spec) + with mock.patch.dict(sys.modules, {blocked: None}): # None makes import raise + spec.loader.exec_module(module) + return module + + +def test_executor_reports_real_runtime_import_error(capsys) -> None: + import pytest + import torch + + blocked = "sglang.multimodal_gen.runtime.entrypoints.utils" + base = _load_with_blocked_import("executors/base.py", "_sgld_base_blocked", blocked) + printed = capsys.readouterr().out + assert blocked in printed + assert "is not installed" not in printed + + class _Executor(base.SGLDiffusionExecutor): + def __init__(self): + torch.nn.Module.__init__(self) + + with pytest.raises(RuntimeError, match="failed to import") as err: + _Executor()._execute_packed(None, None, None) + assert isinstance(err.value.__cause__, ImportError) + assert blocked in str(err.value.__cause__) + + +def test_generator_reports_real_runtime_import_error(caplog) -> None: + import pytest + + blocked = "sglang.multimodal_gen" + module = _load_with_blocked_import( + "core/generator.py", + "sglang.multimodal_gen.apps.ComfyUI_SGLDiffusion.core._generator_blocked", + blocked, + ) + assert any(blocked in record.getMessage() for record in caplog.records) + with pytest.raises(RuntimeError, match="failed to import") as err: + module.SGLDiffusionGenerator().init_generator("flux", "FluxPipeline", {}) + assert isinstance(err.value.__cause__, ImportError)