Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions tests/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,52 @@ def f2(x):
return x


def test_import_kernels_does_not_retain_the_import_error(monkeypatch):
"""The vllm._C ImportError must be collectable after import_kernels.

``logger.warning_once`` caches its arguments forever (``lru_cache``). If
the live ImportError is passed as a format argument, the cache retains it
and — through ``__traceback__`` — every frame on the raising path.
``import_kernels`` runs inside ``LLM.__init__``, so on platforms without
compiled ``vllm._C`` kernels this pinned the ``LLM`` instance itself: it
could never be garbage collected and its EngineCore only exited with the
interpreter. See #54096.
"""
import gc
import weakref

from vllm.platforms.interface import Platform

blocked = ("vllm._C", "vllm._moe_C_stable_libtorch")
exc_refs = []

class _TrackedModuleNotFoundError(ModuleNotFoundError):
pass # python-level subclass; supports weak references

class _BlockKernelImports:
def find_spec(self, name, path=None, target=None):
if name in blocked:
exc = _TrackedModuleNotFoundError(f"No module named '{name}' (test)")
exc_refs.append(weakref.ref(exc))
raise exc
return None

for mod in blocked:
monkeypatch.delitem(sys.modules, mod, raising=False)
monkeypatch.setattr(sys, "meta_path", [_BlockKernelImports()] + sys.meta_path)

Platform.import_kernels()

assert exc_refs, "the blocked import was never attempted"
gc.collect()
leaked = [ref for ref in exc_refs if ref() is not None]
assert not leaked, (
"ImportError retained after import_kernels; a cached reference "
"(e.g. a live exception passed to logger.warning_once) pins its "
"traceback and every local on the raising path"
)


Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
def test_import_kernels_does_not_retain_the_import_error(monkeypatch):
"""The vllm._C ImportError must be collectable after import_kernels.
``logger.warning_once`` caches its arguments forever (``lru_cache``). If
the live ImportError is passed as a format argument, the cache retains it
and — through ``__traceback__`` — every frame on the raising path.
``import_kernels`` runs inside ``LLM.__init__``, so on platforms without
compiled ``vllm._C`` kernels this pinned the ``LLM`` instance itself: it
could never be garbage collected and its EngineCore only exited with the
interpreter. See #54096.
"""
import gc
import weakref
from vllm.platforms.interface import Platform
blocked = ("vllm._C", "vllm._moe_C_stable_libtorch")
exc_refs = []
class _TrackedModuleNotFoundError(ModuleNotFoundError):
pass # python-level subclass; supports weak references
class _BlockKernelImports:
def find_spec(self, name, path=None, target=None):
if name in blocked:
exc = _TrackedModuleNotFoundError(f"No module named '{name}' (test)")
exc_refs.append(weakref.ref(exc))
raise exc
return None
for mod in blocked:
monkeypatch.delitem(sys.modules, mod, raising=False)
monkeypatch.setattr(sys, "meta_path", [_BlockKernelImports()] + sys.meta_path)
Platform.import_kernels()
assert exc_refs, "the blocked import was never attempted"
gc.collect()
leaked = [ref for ref in exc_refs if ref() is not None]
assert not leaked, (
"ImportError retained after import_kernels; a cached reference "
"(e.g. a live exception passed to logger.warning_once) pins its "
"traceback and every local on the raising path"
)

We don't need a specific test for this small update

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

removed the test

def test_trace_function_call():
fd, path = tempfile.mkstemp()
cur_dir = os.path.dirname(__file__)
Expand Down
12 changes: 8 additions & 4 deletions vllm/platforms/cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,26 +497,30 @@ def import_kernels(cls) -> None:
try:
import vllm._C # noqa: F401
except ImportError as e:
logger.warning_once("Failed to import from vllm._C: %r", e)
logger.warning_once(
"Failed to import from vllm._C: %s", repr(e)
)
else:
try:
import vllm._C_AVX512 # noqa: F401
except ImportError as e:
if ignored_msg not in e.msg:
logger.warning_once(
"Failed to import from vllm._C_AVX512: %r", e
"Failed to import from vllm._C_AVX512: %s", repr(e)
)
else:
try:
import vllm._C_AVX2 # noqa: F401
except ImportError as e:
if ignored_msg not in e.msg:
logger.warning_once("Failed to import from vllm._C_AVX2: %r", e)
logger.warning_once(
"Failed to import from vllm._C_AVX2: %s", repr(e)
)
else:
try:
import vllm._C # noqa: F401
except ImportError as e:
logger.warning_once("Failed to import from vllm._C: %r", e)
logger.warning_once("Failed to import from vllm._C: %s", repr(e))

@classmethod
def pack_kv_cache(
Expand Down
4 changes: 3 additions & 1 deletion vllm/platforms/cuda.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,9 @@ def import_kernels(cls) -> None:
try:
import vllm._C_stable_libtorch # noqa: F401
except ImportError as e:
logger.warning_once("Failed to import from vllm._C_stable_libtorch: %r", e)
logger.warning_once(
"Failed to import from vllm._C_stable_libtorch: %s", repr(e)
)
with contextlib.suppress(ImportError):
import vllm._moe_C_stable_libtorch # noqa: F401
with contextlib.suppress(ImportError):
Expand Down
5 changes: 4 additions & 1 deletion vllm/platforms/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,10 @@ def import_kernels(cls) -> None:
try:
import vllm._C # noqa: F401
except ImportError as e:
logger.warning_once("Failed to import from vllm._C: %r", e)
# Format eagerly: warning_once caches its arguments forever, and a
# live exception would pin its traceback (and every local on the
# raising path, including the LLM under construction).
logger.warning_once("Failed to import from vllm._C: %s", repr(e))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
# Format eagerly: warning_once caches its arguments forever, and a
# live exception would pin its traceback (and every local on the
# raising path, including the LLM under construction).
logger.warning_once("Failed to import from vllm._C: %s", repr(e))
logger.warning_once("Failed to import from vllm._C: %s", repr(e))

Comment not needed

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

removed the comment

with contextlib.suppress(ImportError):
import vllm._moe_C_stable_libtorch # noqa: F401

Expand Down
Loading