Skip to content

When a checkpoint save's pre-write agreement fails, the rank whose save failed raises only the collective's error, not its own #780

In review

Description

@amazloumi

What happened

When the all-reduce that CheckpointManager.save() runs before the write fails, as when another rank reaches it only after the manager group's timeout, a rank whose own part of the save had already failed raises the collective's error instead of its own, so no rank's output names the cause, such as a full disk.

_agree_to_write (kempnerforge/checkpoint/manager.py:1839-1886 at 3899909) keeps the error from this rank's _prepare_save or from its wait for the previous async write in own (:1857), and raises it only after its own dist.all_reduce on the manager's gloo group returns (:1870, :1884-1885). When that all-reduce raises, own is neither raised, chained nor logged: _attempt already returned it, so it is not even the new error's __context__. The all-reduce fails when another rank is still in its part of the save before the agreement (creating the step directories or building the state, :1898-1908) as the group's timeout ends, which training sets to distributed.nccl_timeout_sec (kempnerforge/training/entry.py:229), or when another rank's process exits while this one waits. The late rank then fails with a collective error as well, so the run ends without naming what went wrong.

agree_across_ranks (kempnerforge/distributed/utils.py:285-305) has the same gap on main, and #776 makes it raise the rank's own error with the collective's failure in a note; _agree_to_write runs its own all-reduce, so that change does not reach it. Other agreements in the manager hold a rank's error across a collective of their own in the same order, among them _optimizer_saved_on_any_rank (:3108-3129, an all-reduce like this one), _check_model_fits (:1986-2019) and _gather_rank_states (:2242-2254).

Expected: a rank whose part of the save failed raises its own error whether or not the agreement completes, with the collective's failure in a note.

Possible fix: when the all-reduce raises, raise own with the collective's failure in a note, as #776 does in agree_across_ranks, and do the same in the agreements above.

Related: #737 (added _agree_to_write), #776, #424.

Reproduction

CPU only, two gloo ranks, with a 5 s timeout on the manager's group in place of distributed.nccl_timeout_sec. Rank 1's creation of the step directory raises as on a full disk. In the late case rank 0 starts its save only once rank 1's has raised, standing in for a rank whose part of the save before the agreement outlasts the timeout: save() runs no collective before the agreement, so to rank 1 the two are the same. Save as repro.py and run uv run python repro.py from the repository root:

"""Two CPU ranks over gloo save step 1; rank 1 cannot create the step directory."""

import multiprocessing
import tempfile
import time
import traceback
from pathlib import Path

import torch
import torch.distributed as dist

from kempnerforge.checkpoint.manager import CheckpointManager
from kempnerforge.config.schema import CheckpointConfig

GROUP_TIMEOUT_S = 5
OWN_ERROR = "No space left on device"


def fail_step_directories() -> None:
    """Make every later mkdir of a ``step_*`` directory in this process raise as on a full disk."""
    mkdir = Path.mkdir

    def mkdir_on_a_full_disk(path: Path, *args, **kwargs) -> None:
        if path.name.startswith("step_"):
            raise OSError(28, OWN_ERROR)
        mkdir(path, *args, **kwargs)

    Path.mkdir = mkdir_on_a_full_disk


def run_rank(rank: int, root: str, case: str) -> None:
    """Save step 1 on ``rank``; in the ``late`` case rank 0 starts once rank 1 has raised."""
    dist.init_process_group("gloo", init_method=f"file://{root}/store", rank=rank, world_size=2)
    model = torch.nn.Linear(4, 4)
    optimizer = torch.optim.SGD(model.parameters(), lr=0.1)
    config = CheckpointConfig(dir=f"{root}/ckpt")
    manager = CheckpointManager(config, model, optimizer, group_timeout_sec=GROUP_TIMEOUT_S)
    rank_1_outcome = Path(root, "rank1.txt")
    if rank == 1:
        fail_step_directories()
    elif case == "late":
        deadline = time.monotonic() + 300
        while not rank_1_outcome.exists() and time.monotonic() < deadline:
            time.sleep(0.1)
    start = time.monotonic()
    try:
        manager.save(step=1)
        lines = [f"{case}: rank {rank} saved"]
    except Exception as error:
        shown = "".join(traceback.format_exception(error))
        own = "yes" if OWN_ERROR in shown else "no"
        if rank == 0 and case == "late":
            lines = [f"{case}: rank 0 raised; its error shows rank 1's OSError: {own}"]
        elif rank == 0:
            lines = [f"{case}: rank 0 raised {type(error).__name__}: {error}"]
        else:
            lines = [
                f"{case}: rank 1 raised {type(error).__name__}: {error}",
                f"{case}: rank 1 raised after about {round(time.monotonic() - start)} s; "
                f"its own OSError in what it raised: {own}",
            ]
            if case == "late":
                lines.append(shown)
    Path(root, f"rank{rank}.txt").write_text("\n".join(lines) + "\n")


def main() -> None:
    """Run the ``on time`` and ``late`` cases; print each rank's outcome."""
    context = multiprocessing.get_context("spawn")
    with tempfile.TemporaryDirectory() as tmp:
        for case in ("on time", "late"):
            root = Path(tmp, case.replace(" ", "_"))
            root.mkdir()
            ranks = [context.Process(target=run_rank, args=(r, str(root), case)) for r in range(2)]
            for process in ranks:
                process.start()
            for process in ranks:
                process.join(600)
            for rank, process in enumerate(ranks):
                if process.is_alive():
                    process.kill()
                    process.join()
                    print(f"{case}: rank {rank} was still running after 600 s")
            for rank in range(2):
                outcome = root / f"rank{rank}.txt"
                text = outcome.read_text() if outcome.exists() else f"{case}: rank {rank}: none\n"
                print(text, end="")


if __name__ == "__main__":
    main()

Error / traceback

When rank 0 is on time, rank 1 raises its own error. When rank 0 is late, rank 1 raises only the timeout of the agreement's all-reduce, after the group's 5 s, and rank 0's error does not show rank 1's either:

on time: rank 0 raised RuntimeError: Checkpoint save failed on ranks [1]
on time: rank 1 raised OSError: [Errno 28] No space left on device
on time: rank 1 raised after about 0 s; its own OSError in what it raised: yes
late: rank 0 raised; its error shows rank 1's OSError: no
late: rank 1 raised RuntimeError: [/__w/pytorch/pytorch/third_party/gloo/gloo/transport/tcp/unbound_buffer.cc:78] Timed out waiting 5000ms for recv operation to complete
late: rank 1 raised after about 5 s; its own OSError in what it raised: no

Rank 1's traceback in the late case:

Traceback (most recent call last):
  File "repro.py", line 47, in run_rank
    manager.save(step=1)
  File "kempnerforge/checkpoint/manager.py", line 2463, in save
    self._agree_to_write(error, flush_error)
  File "kempnerforge/checkpoint/manager.py", line 1870, in _agree_to_write
    dist.all_reduce(flags, group=self._sync_group)
...
  File "torch/distributed/distributed_c10d.py", line 3913, in all_reduce
    work.wait()
RuntimeError: [/__w/pytorch/pytorch/third_party/gloo/gloo/transport/tcp/unbound_buffer.cc:78] Timed out waiting 5000ms for recv operation to complete

Environment

  • GPU type and count: none (CPU reproduction, 2 gloo ranks)
  • Nodes: 1
  • PyTorch version: 2.14.1+cu130
  • Python: 3.12.11
  • KempnerForge commit: 3899909

Activity

  1. added
    bug
    core
    low
    on Dec 5, 2026
  2. self-assigned this
    on Dec 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugcorelow

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions