[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>
This commit is contained in:
2026-10-09 20:04:37 +07:00
co-authored by Hinata
parent 6c956ee3bd
commit 036608b012
4 changed files with 410 additions and 91 deletions
+40 -13
View File
@@ -116,6 +116,24 @@ _R = TypeVar("_R") # Return type for collective_rpc
_TEARDOWN_SIGNAL_BOUND_S = 30.0
# How long the guard's forced-exit paths wait for their diagnostic line. The
# line is written from a helper thread; a blocked handler, a held logging lock
# or a full stderr pipe costs at most this, never the exit.
_TEARDOWN_EXIT_LOG_BOUND_S = 0.5
def _log_bounded(emit: Callable[[], None], bound_s: float) -> None:
"""Run ``emit`` (logging, flushes) on a daemon thread; wait <= bound_s."""
try:
thread = threading.Thread(
target=emit, daemon=True, name="EngineCoreTeardownLog"
)
thread.start()
thread.join(bound_s)
except BaseException: # noqa: BLE001 -- the caller's exit must still run
pass
class _TeardownSignalGuard:
"""EngineCore teardown: defer ONE late SIGTERM/SIGINT, bounded.
@@ -154,25 +172,34 @@ class _TeardownSignalGuard:
)
self._watchdog.start()
return
logger.warning(
"[shutdown] EngineCore: second late signal (%s) during resource "
"teardown; not deferring again",
name,
)
# Default disposition first, so a third signal also ends the process
# while the (bounded) diagnostic line is written.
signal.signal(signum, signal.SIG_DFL)
_log_bounded(
lambda: logger.warning(
"[shutdown] EngineCore: second late signal (%s) during "
"resource teardown; not deferring again",
name,
),
_TEARDOWN_EXIT_LOG_BOUND_S,
)
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()
def emit() -> None:
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()
_log_bounded(emit, _TEARDOWN_EXIT_LOG_BOUND_S)
os._exit(1)
def disarm(self) -> None: