Distributed workers can connect to each other and still stall when they reach a collective operation. To fix a PyTorch Gloo collective timeout caused by mismatched calls, use Flight Recorder to find where the ranks diverge and make them enter the same operation.

Both Gloo ranks report the reduced sum

A receive timeout tells you a rank was left waiting. When the traces show different operations at the same sequence in the same process group, the mismatch points to a difference in the code each rank executed.

The reproduction uses two CPU ranks on one Linux host with a Gloo-enabled build of PyTorch 2.15. It uses Flight Recorder's private JSON dump API to capture each rank's trace when the collective raises an error.

Steps to fix Gloo collective timeouts in PyTorch:

  1. Create collective.py in an empty working directory with your text editor.
    collective.py
    from datetime import timedelta
    from pathlib import Path
     
    import torch
    import torch.distributed as dist
    from torch._C._distributed_c10d import _dump_fr_trace_json
     
    dist.init_process_group("gloo", timeout=timedelta(seconds=8))
    rank = dist.get_rank()
    value = torch.tensor([rank + 1])

    The two ranks start with values 1 and 2. The short timeout keeps the deliberate stall brief.

  2. Append the mismatched collective calls to the end of collective.py.
    def run_collective():
        if rank == 1:
            dist.broadcast(value, src=0)
        else:
            dist.all_reduce(value)

    Rank 1 waits for a broadcast from rank 0, which instead enters an all-reduce.

  3. Append the code that captures collective errors to the end of collective.py.
    try:
        run_collective()
    except RuntimeError:
        trace = _dump_fr_trace_json(backend="gloo")
        Path(f"rank-{rank}.json").write_bytes(trace)
        raise
    else:
        print(f"Rank {rank}: sum = {value.item()}\n", end="", flush=True)
    finally:
        dist.destroy_process_group()

    Each rank writes its trace before releasing the process group and passes the original collective error back to torchrun.

  4. Save collective.py.
  5. Launch the two ranks from the directory containing collective.py.

    The launch requires an isolated host, a trusted network, or host ingress controls that block untrusted access to TCP port 29500. Even with a loopback rendezvous address, TCPStore can listen on all interfaces. Port 29500 must be free.

    GLOO_SOCKET_IFNAME=lo TORCH_FR_BUFFER_SIZE=16 \
      torchrun --nnodes=1 --nproc-per-node=2 \
      --master-addr=127.0.0.1 --master-port=29500 collective.py

    lo selects the Linux loopback interface for Gloo traffic, and 127.0.0.1 is where local workers contact the rendezvous store. The trace buffer keeps the last 16 operations. Both ranks report Timed out waiting 8000ms for recv operation to complete and the launcher exits with an error.

  6. Compare the operation names at the same collective sequence in the two rank traces.
    python - <<'PY'
    import json
    from pathlib import Path
     
    for rank in range(2):
        trace = json.loads(Path(f"rank-{rank}.json").read_text())
        for entry in trace["entries"]:
            seq = entry["collective_seq_id"]
            name = entry["profiling_name"]
            state = entry["state"]
            print(f"Rank {rank}, sequence {seq}: {name} ({state})")
    PY

    Sequence 1 shows gloo:all_reduce on rank 0 and gloo:broadcast on rank 1. Both entries are scheduled, so neither trace records a completed operation for that sequence.

  7. Open collective.py in your text editor.
  8. Replace the run_collective() function in collective.py with the following definition.
    def run_collective():
        dist.all_reduce(value)

    all_reduce() sums the two input values by default and stores the result in the tensor on each rank. The same repair in a training script means aligning the collective calls and their order across the process group.

  9. Save collective.py.
  10. Rerun collective.py with the same torchrun command.

    The launcher starts a fresh process group and exits normally after both workers finish.

  11. Confirm that ranks 0 and 1 both print sum = 3.
    Rank 1: sum = 3
    Rank 0: sum = 3

    The rank lines can appear in either order.

Share this guide