kempnerforge.resilience.signal_handler

Graceful shutdown via signal handling.

Handles SIGTERM (SLURM preemption / graceful shutdown) and SIGUSR1 (SLURM requeue) by setting a flag that the training loop checks after each step. On signal, the loop saves an emergency checkpoint and exits.

Timeout protection ensures the process exits even if graceful shutdown stalls (e.g., stuck in NCCL collective).

Module Attributes

SHUTDOWN_LEAD_ENV

Seconds a launch script gives its ranks from the shutdown SIGTERM until they may be killed.

SHUTDOWN_LEAD_FRACTION

Share of that lead the forced-exit timer of a SIGTERM defaults to, so it fires first.

DEFAULT_SHUTDOWN_TIMEOUT_SEC

The forced-exit timeout when neither the config nor the launch script sets one.

Functions

ignore_shutdown_signals_in_worker(worker_id)

worker_init_fn that makes a DataLoader worker ignore shutdown signals.

resolve_shutdown_timeouts(configured)

The forced-exit timeouts, in seconds, for train.shutdown_timeout_sec.

Classes

ShutdownHandler

Cooperative shutdown handler for long-running training jobs.

kempnerforge.resilience.signal_handler.SHUTDOWN_LEAD_ENV = 'KEMPNERFORGE_SHUTDOWN_LEAD_SEC'

Seconds a launch script gives its ranks from the shutdown SIGTERM until they may be killed.

kempnerforge.resilience.signal_handler.SHUTDOWN_LEAD_FRACTION = 0.75

Share of that lead the forced-exit timer of a SIGTERM defaults to, so it fires first.

kempnerforge.resilience.signal_handler.DEFAULT_SHUTDOWN_TIMEOUT_SEC = 600.0

The forced-exit timeout when neither the config nor the launch script sets one.

kempnerforge.resilience.signal_handler.resolve_shutdown_timeouts(configured)[source]

The forced-exit timeouts, in seconds, for train.shutdown_timeout_sec.

The launch scripts under scripts/slurm export KEMPNERFORGE_SHUTDOWN_LEAD_SEC, the seconds their ranks have from the shutdown SIGTERM until they may be killed: the --signal lead of the scripts that launch the ranks with srun, since at the time limit SLURM’s SIGTERM can make srun kill them at once, and the 30 s that torchrun gives its workers by default in singlenode.sh. Left unset, a SIGTERM gets three quarters of that lead, so a rank whose emergency save, flush or teardown hangs exits through the timer, which logs Graceful shutdown timed out, before the launcher kills it; without the variable it gets 600 s. A SIGUSR1, the manual stop, which no kill follows, and a shutdown another rank started get 600 s. 0 disables the timer. Any other value applies to both, with a warning when it is not below the lead, since the launcher can then kill the rank before the timer fires.

Returns:

(timeout_sec, sigterm_timeout_sec), as ShutdownHandler takes them.

Raises:

ValueError – KEMPNERFORGE_SHUTDOWN_LEAD_SEC is set to something other than a positive number of seconds.

Parameters:

configured (float | None)

Return type:

tuple[float, float]

kempnerforge.resilience.signal_handler.ignore_shutdown_signals_in_worker(worker_id)[source]

worker_init_fn that makes a DataLoader worker ignore shutdown signals.

SLURM preemption and torchrun teardown signal the whole process group, not a single PID, so SIGTERM reaches every DataLoader worker as well as the rank’s main process. Workers install no handler, take the default action and die at once. The main process — which handles the signal cooperatively, by setting a flag and finishing its step — then fails fetching its next micro-batch, and that failure escapes before the loop reaches its should_shutdown() check, so no emergency checkpoint is written.

Ignoring the shutdown signals in the worker keeps it serving long enough for the step to finish, which puts the emergency save back on the loop’s normal path rather than on an exception path.

The cost: multiprocessing.Process.terminate() is itself SIGTERM, so a shielded worker cannot be reaped that way, and multiprocessing’s exit hook, which terminates leftover workers and then joins them, would block on one forever. Workers are therefore shut down through the loader’s sentinel path: StatefulDataLoader.close and StreamingDataLoader.close, which the training loop calls on every exit path and a process calls at exit for every loader whose workers are still alive. Nothing closes a plain DataLoader at exit: its caller has to call shutdown_workers itself.

Parameters:

worker_id (int) – Worker index, supplied by DataLoader. Unused.

Return type:

None

class kempnerforge.resilience.signal_handler.ShutdownHandler[source]

Bases: object

Cooperative shutdown handler for long-running training jobs.

Register this handler before the training loop. The training loop checks should_shutdown() after each step and takes appropriate action (save checkpoint, clean up, exit).

If the graceful shutdown exceeds its timeout, a forced exit is triggered via os._exit to avoid hanging on stuck collectives.

Usage:

handler = ShutdownHandler(timeout_sec=120)
handler.register()

for step in range(max_steps):
    train_step()
    handler.reinstall()
    if handler.should_shutdown():
        save_checkpoint()
        break
wait_for_checkpoint()
handler.finish()
Parameters:
  • timeout_sec – Seconds a shutdown that a SIGUSR1 or request() starts may take before the forced exit; 0 disables it. None resolves it, and sigterm_timeout_sec with it, as training does for an unset train.shutdown_timeout_sec (see resolve_shutdown_timeouts).

  • sigterm_timeout_sec – The same for a SIGTERM, which the launcher may follow with a kill; None takes timeout_sec, or the resolved value. A SIGTERM that arrives once a longer timer is running brings the forced exit forward to its own timeout.

__init__(timeout_sec=None, sigterm_timeout_sec=None)[source]
Parameters:
  • timeout_sec (float | None)

  • sigterm_timeout_sec (float | None)

Return type:

None

property shutdown_requested: bool

Whether a shutdown was requested, by a signal or by request().

property signal_received: Signals | None

The signal that triggered shutdown, or None.

should_shutdown()[source]

Check if the training loop should exit.

Call this after each training step.

Return type:

bool

request()[source]

Request a shutdown without a signal, as when another rank received one.

Sets the flag and starts the forced-exit timer with timeout_sec, as a SIGUSR1 does, so a rank that stops because another rank was signaled is bounded by its own timer too; its own SIGTERM, if one follows, brings the exit forward. Calling it again changes nothing.

Return type:

None

register()[source]

Register signal handlers for SIGTERM and SIGUSR1.

Must be called from the main thread.

Return type:

None

reinstall()[source]

Install the handlers again; a no-op unless registered, or off the main thread.

Native code can replace a handler below Python, where signal.getsignal cannot see it: the LLVM inside Triton does on its first in-process compile, after which SIGUSR1 does nothing. Only the main thread can install a handler, so on any other thread, such as a training loop run off the main thread, this does nothing.

Return type:

None

unregister()[source]

Restore the original signal handlers; the forced-exit timer stays armed.

Return type:

None

cancel_watchdog()[source]

Cancel the forced-exit timer, leaving the signal handlers installed.

Return type:

None

finish()[source]

Call once the emergency checkpoint is durable and teardown is done.

Restores the signal handlers and cancels the forced-exit timer, so it must come after the last step that a second signal or a hang could cut short: the checkpoint flush, and tearing down the process group.

Return type:

None