[Engine] Drain-order shutdown

- EngineCore survives a late SIGTERM during teardown (bounded deferral) so
  every worker drains its guard telemetry.
- A worker exits right after its orderly shutdown.
- A worker failure racing shutdown stays a failure (exit 1).
- Rank 0 waits for the PLE child's whole thread group, bounded, instead of its
  reap; drain-aware failure ladder with a bounded grace for survivors whose
  shutdown already started.
- tests: the sorted-emit graph arm takes max_len on the host before capture.

Co-Authored-By: Hinata <hinata@idihore.id>
Signed-off-by: MOVZX <movzx@yahoo.com>
This commit is contained in:
2026-10-09 20:04:37 +07:00
co-authored by Hinata
parent eeea036c12
commit 6c956ee3bd
3 changed files with 423 additions and 19 deletions
+93 -5
View File
@@ -4,12 +4,13 @@ import gc
import os
import queue
import signal
import sys
import threading
import time
from collections import defaultdict, deque
from collections.abc import Callable, Generator, Sequence
from concurrent.futures import Future
from contextlib import ExitStack, contextmanager
from contextlib import ExitStack, contextmanager, suppress
from enum import IntEnum
from functools import partial
from inspect import isclass, signature
@@ -108,6 +109,76 @@ HANDSHAKE_TIMEOUT_MINS = 5
_R = TypeVar("_R") # Return type for collective_rpc
# How long EngineCore teardown may run after it deferred a late SIGTERM/SIGINT
# before the process exits anyway. Covers the whole of engine_core.shutdown(),
# not only the executor's worker ladder (grace + 4 s): a hang in later teardown
# (scheduler, distributed cleanup) must not make the process unkillable.
_TEARDOWN_SIGNAL_BOUND_S = 30.0
class _TeardownSignalGuard:
"""EngineCore teardown: defer ONE late SIGTERM/SIGINT, bounded.
Dying on the first late signal would SIGKILL every worker
(PR_SET_PDEATHSIG) before its shutdown drain; see the finally block of
EngineCoreProc.run_engine_core. The deferral is bounded two ways:
- a SECOND late signal restores the default disposition and re-delivers
itself, so the process dies of it (the maintainer's escalation);
- after the first deferred signal a watchdog ends the process with
exit code 1 if teardown is still running _TEARDOWN_SIGNAL_BOUND_S later.
"""
def __init__(self, bound_s: float | None = None) -> None:
self.bound_s = _TEARDOWN_SIGNAL_BOUND_S if bound_s is None else bound_s
self.deferred = 0
self._done = threading.Event()
self._watchdog: threading.Thread | None = None
def __call__(self, signum, frame) -> None:
name = signal.Signals(signum).name
if self._done.is_set():
return
self.deferred += 1
if self.deferred == 1:
logger.warning(
"[shutdown] EngineCore: %s during resource teardown deferred; "
"workers finish their shutdown (FINAL DRAIN) first (a second "
"signal, or %.0f s of teardown, ends the process)",
name,
self.bound_s,
)
self._watchdog = threading.Thread(
target=self._expire,
daemon=True,
name="EngineCoreTeardownWatchdog",
)
self._watchdog.start()
return
logger.warning(
"[shutdown] EngineCore: second late signal (%s) during resource "
"teardown; not deferring again",
name,
)
signal.signal(signum, signal.SIG_DFL)
os.kill(os.getpid(), signum)
def _expire(self) -> None:
if self._done.wait(self.bound_s):
return
logger.error(
"[shutdown] EngineCore: resource teardown still running %.0f s "
"after a deferred signal; exiting (code 1)",
self.bound_s,
)
for stream in (sys.stdout, sys.stderr):
with suppress(Exception):
stream.flush()
os._exit(1)
def disarm(self) -> None:
self._done.set()
class EngineCore:
"""Inner loop of vLLM's Engine."""
@@ -1441,12 +1512,29 @@ class EngineCoreProc(EngineCore):
engine_core._send_engine_dead()
raise e
finally:
signal.signal(signal.SIGTERM, signal.SIG_DFL)
signal.signal(signal.SIGINT, signal.SIG_DFL)
# Workers carry PR_SET_PDEATHSIG=SIGKILL (fail-closed PLE teardown),
# so if this process dies before model_executor.shutdown() has reaped
# them, every worker is SIGKILLed mid-shutdown and its bounds-guard
# FINAL DRAIN never runs. The API server's process manager sends a
# second SIGTERM right after the signal that started this shutdown;
# with SIG_DFL here it killed teardown before the executor waited for
# the workers. Defer ONE late signal until teardown ends; a second
# signal or _TEARDOWN_SIGNAL_BOUND_S of teardown after the deferral
# still ends the process (_TeardownSignalGuard), as do the
# executor's terminate/kill ladder and the process manager's timeout
# (kill_process_tree -> SIGKILL).
teardown_guard = _TeardownSignalGuard()
signal.signal(signal.SIGTERM, teardown_guard)
signal.signal(signal.SIGINT, teardown_guard)
if signal_callback is not None:
signal_callback.stop()
if engine_core is not None:
engine_core.shutdown()
try:
if engine_core is not None:
engine_core.shutdown()
finally:
signal.signal(signal.SIGTERM, signal.SIG_DFL)
signal.signal(signal.SIGINT, signal.SIG_DFL)
teardown_guard.disarm()
if clean_shutdown:
from vllm.platforms import current_platform