Files
vLLM/vllm/v1/ple_offload/fatal.py
T
MOVZXandHinata 036608b012 [Engine] Detect a fail-closed fault or death on any rank
A fault on a non-zero TP rank could hang the engine: the worker's sentinel pipe
was inherited by a helper process and stayed open, so the monitor never saw
the exit. Earlier releases carry the same monitor and fail-closed exit path.

- Worker exit is detected by pidfd, with the sentinel as fallback, before and
  after READY and for every worker through the whole initialisation window.
- A per-worker fault pipe: ple_fail_closed writes one byte before os._exit and
  the monitor starts the failure ladder at once; the fd stays registered
  through worker teardown.
- worker_main marks inherited pipes close-on-exec.
- Any unexpected busy-loop failure exits 1; the termination ladder uses
  time.monotonic(); forced-exit paths no longer wait on logging.
- Tests on real fork/spawn workers for rank 0 and rank 1, with and without an
  fd-inheriting helper.

Co-Authored-By: Hinata <hinata@idihore.id>
Signed-off-by: MOVZX <movzx@yahoo.com>
2026-10-09 20:04:37 +07:00

305 lines
12 KiB
Python

# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""Fail-closed helpers for the PLE single-slot transport."""
import functools
import os
import time
from collections.abc import Callable
from types import TracebackType
from typing import Any, NoReturn, TypeVar
_F = TypeVar("_F", bound=Callable[..., Any])
_TRUSTED_ERROR_LABELS = (
(RuntimeError, "RuntimeError"),
(TimeoutError, "TimeoutError"),
(ValueError, "ValueError"),
(OSError, "OSError"),
(EOFError, "EOFError"),
(BrokenPipeError, "BrokenPipeError"),
(AssertionError, "AssertionError"),
(KeyError, "KeyError"),
(IndexError, "IndexError"),
(TypeError, "TypeError"),
(MemoryError, "MemoryError"),
(KeyboardInterrupt, "KeyboardInterrupt"),
)
# Slot readers for the dying path. Each is the C getter of the base type,
# called directly, so no __getattribute__, property, metaclass attribute or
# descriptor defined by the exception (or its class) can run. The traceback
# module is NOT safe here: traceback.format_exception calls str(error) (and
# str() on every chained exception) and reads error.__traceback__ through the
# instance's own __getattribute__, so an exception whose __str__ or
# __getattribute__ blocks kept the process alive forever -- the fail-closed
# path hung instead of dying (found by the blocking-exception transport tests).
_EXC_ARGS = BaseException.args.__get__
_EXC_TRACEBACK = BaseException.__traceback__.__get__
_EXC_CAUSE = BaseException.__cause__.__get__
_EXC_CONTEXT = BaseException.__context__.__get__
_EXC_SUPPRESS_CONTEXT = BaseException.__suppress_context__.__get__
_TYPE_QUALNAME = type.__dict__["__qualname__"].__get__
_TYPE_MODULE = type.__dict__["__module__"].__get__
_MAX_CHAIN = 16
_MAX_ARG_CHARS = 1024
# Write end of the per-worker fault pipe (WorkerProc.make_worker_process); the
# executor's worker monitor waits on the read end next to the process
# sentinels. None outside a worker process (tests, the offload child).
_FATAL_NOTIFY_FD: int | None = None
def set_fatal_notify_fd(fd: int | None) -> None:
"""Register the fault pipe worker_main received from the executor."""
global _FATAL_NOTIFY_FD
_FATAL_NOTIFY_FD = fd
def notify_parent_fatal() -> None:
"""Tell the executor this worker is dying, before the process is gone.
The executor's only other signal is the process sentinel, which the kernel
closes when the LAST task of the worker leaves -- after the address-space
teardown, not at os._exit. On a rank whose GPU work is paired with a live
peer that teardown can wait for the peer's exit (in a rank-1 fault test, a
fail-closed fault on PP0_TP1 left its leader Z and unreapable for 300 s
while rank 0 waited on it in turn; the engine hung on the response
queue). One byte here wakes the monitor at once so the failure ladder ends
the peers, which is what frees the teardown. Nonblocking, never raises:
the caller os._exits right after, whatever happens here.
"""
fd = _FATAL_NOTIFY_FD
if fd is None:
return
try:
os.set_blocking(fd, False)
os.write(fd, b"F")
except BaseException:
pass
def _safe_type_label(cls: type) -> str:
"""``module.qualname`` of ``cls`` without running class-defined code."""
try:
qualname = _TYPE_QUALNAME(cls)
module = _TYPE_MODULE(cls)
except BaseException:
return "<unknown type>"
if type(qualname) is not str:
qualname = "<unknown type>"
if type(module) is str and module != "builtins":
return f"{module}.{qualname}"
return qualname
def _safe_exception_line(error: BaseException) -> str:
"""Type label plus the exact-``str``/``int`` args; nothing else is shown."""
label = _safe_type_label(type(error))
args = _EXC_ARGS(error)
if type(args) is not tuple or len(args) == 0:
return label
parts = []
for arg in args[:4]:
if type(arg) is str:
parts.append(arg[:_MAX_ARG_CHARS])
elif type(arg) is int and -(10**18) < arg < 10**18:
parts.append(str(arg))
else:
parts.append(f"<{_safe_type_label(type(arg))}>")
if len(args) > 4:
parts.append("...")
if len(args) == 1:
return f"{label}: {parts[0]}"
return f"{label}: ({', '.join(parts)})"
def _safe_frames(error: BaseException) -> list[str]:
lines = []
tb = _EXC_TRACEBACK(error)
while type(tb) is TracebackType:
code = tb.tb_frame.f_code
filename = code.co_filename
name = code.co_qualname
lineno = tb.tb_lineno
lines.append(
f' File "{filename if type(filename) is str else "?"}", '
f"line {lineno if type(lineno) is int else '?'}, "
f"in {name if type(name) is str else '?'}\n"
)
tb = tb.tb_next
return lines
def _safe_format_exception(error: BaseException) -> str:
"""A traceback.format_exception look-alike that runs no exception code.
Frames come from the traceback objects (file, line, function; no source
lines, which would mean file I/O on the dying path); the message is the
exception's exact-``str`` args, never ``str(error)``. Chains follow
``__cause__`` / ``__context__`` like the interpreter, bounded and
cycle-safe.
"""
chain: list[tuple[BaseException, str]] = []
seen: set[int] = set()
current: BaseException | None = error
link = ""
while current is not None and len(chain) < _MAX_CHAIN:
if id(current) in seen:
break
seen.add(id(current))
chain.append((current, link))
cause = _EXC_CAUSE(current)
if cause is not None:
current = cause
link = (
"\nThe above exception was the direct cause of the "
"following exception:\n\n"
)
elif _EXC_SUPPRESS_CONTEXT(current) is not True:
current = _EXC_CONTEXT(current)
link = (
"\nDuring handling of the above exception, another "
"exception occurred:\n\n"
)
else:
current = None
out: list[str] = []
# Innermost first, as the interpreter prints; each outer exception's link
# text sits between it and the exception it chains to.
for index in range(len(chain) - 1, -1, -1):
exc, _ = chain[index]
out.append("Traceback (most recent call last):\n")
out.extend(_safe_frames(exc))
out.append(_safe_exception_line(exc) + "\n")
if index > 0:
out.append(chain[index][1])
return "".join(out)
def ple_fail_closed(
*,
where: str,
rank: int,
layer: str,
sequence: int,
error: BaseException,
) -> NoReturn:
"""Emit one best-effort nonblocking diagnostic and terminate immediately."""
try:
os.set_blocking(2, False)
error_type = type(error)
error_name = "Exception"
trusted_error = False
for trusted_type, trusted_name in _TRUSTED_ERROR_LABELS:
if error_type is trusted_type:
error_name = trusted_name
trusted_error = True
break
safe_where = where[:128] if type(where) is str else "unknown"
safe_layer = layer[:256] if type(layer) is str else "unknown"
safe_rank = rank if type(rank) is int else -1
safe_sequence = sequence if type(sequence) is int else -1
detail_suffix = ""
if trusted_error:
args = BaseException.args.__get__(error, error_type)
if type(args) is tuple and len(args) == 1 and type(args[0]) is str:
detail = args[0][:512].replace("\n", "\\n")
detail_suffix = f":{detail}"
message = (
f"PLE fatal: where={safe_where} rank={safe_rank} layer={safe_layer} "
f"sequence={safe_sequence} error={error_name}{detail_suffix} "
f"pid={os.getpid()} "
f"monotonic={time.monotonic():.9f} wall={time.time():.9f}\n"
)
os.write(2, message.encode("utf-8", "backslashreplace")[:2048])
except BaseException:
pass
# THE TRACEBACK, unconditionally, in its own try so it cannot cost us the
# line above.
#
# This path os._exit(1)s, which runs no handlers and flushes nothing, so
# whatever is not written here is gone. The line above names the TYPE only
# when the type is one of _TRUSTED_ERROR_LABELS and flattens everything
# else to the literal string "Exception" -- and the flattened set includes
# the two that matter most here: torch.AcceleratorError (a RuntimeError
# SUBCLASS, so the `is` comparison misses it) and Triton CompilationError.
# An earlier boot died exactly that way: both PP0 ranks
# reported "error=Exception" and there was no traceback anywhere in the
# log, in any rank, so the cause could not be named at all and the next
# step was a re-run rather than a fix.
#
# A fail-closed path is where evidence matters most, not least: it fires
# once, it kills the process, and it is the only witness. Printing the type
# of a wrapped exception without the frames that raised it is not a
# diagnostic.
try:
# Three-arg form: it does not depend on being inside an active except
# block, and this function is called from one frame further out than
# the raise.
# _safe_format_exception, not traceback.format_exception: see the note
# above _EXC_ARGS -- the stdlib formatter calls str(error), which is
# exception-defined code and can block the dying process forever.
tb = _safe_format_exception(error)
raw = tb.encode("utf-8", "backslashreplace")
# Truncate from the END, not the start. format_exception puts the
# frames first and the exception TYPE AND MESSAGE last, so a head-side
# cut throws away the one line that names what went wrong and keeps the
# import-time frames nobody needs. A long chained traceback is exactly
# the case where that matters.
clipped = len(raw) > 8192
raw = raw[-8192:]
os.write(
2,
b"PLE fatal traceback (tail) follows:\n"
if clipped
else b"PLE fatal traceback follows:\n",
)
os.write(2, raw)
if not raw.endswith(b"\n"):
os.write(2, b"\n")
except BaseException:
# Never silent HERE of all places: without this the reader cannot tell
# "the wrapper predates the traceback fix" from "the traceback itself
# could not be produced", and those call for different next steps.
# Still guarded, because os._exit below must run whatever happens: an
# escape from this handler would leave the worker alive in the state
# the fail-closed path exists to end.
try: # noqa: SIM105
os.write(2, b"PLE fatal: traceback unavailable\n")
except BaseException:
# SIM105 declined: an earlier revision made this helper call no
# exception-defined code on the dying path. contextlib.suppress
# runs a Python __exit__ between here and os._exit below; a bare
# except keeps that window empty.
pass
notify_parent_fatal()
os._exit(1)
def fail_ple_execute_model(func: _F) -> _F:
"""Fail a non-shutdown worker-step escape only on PLE-owning ranks.
This includes ``sample_tokens``, the second half of the split execute-model
step. A rank without a PLE connector rethrows the original exception.
"""
@functools.wraps(func)
def wrapped(self: Any, *args: Any, **kwargs: Any) -> Any:
try:
return func(self, *args, **kwargs)
except SystemExit:
# WorkerProc's signal handler uses SystemExit for clean shutdown.
raise
except BaseException as error:
connector = getattr(self, "_ple_offload_connector", None)
if connector is None:
model_runner = getattr(self, "model_runner", None)
connector = getattr(model_runner, "_ple_offload_connector", None)
if connector is None:
raise
connector._fail_closed(func.__name__, error)
return wrapped # type: ignore[return-value]