gloo: `ProcessGroupGloo`'s own (unused) `connectFullMesh` collides with the default group's rendezvous keys → `Connection refused` / hang in `new_group()`
bug描述 Describe the Bug
Environment:
- paddle 3.2.2 (pip wheel), CPU-only build (
cuda: False,nccl: 0) - OS: Ubuntu, Linux 6.17, aarch64
- Python 3.12.14
- Backend: gloo, 4 ranks, single node, launched with
paddle.distributed.launch
Minimal repro (issue1_new_group_collision.py, no external deps beyond paddle):
"""Minimal repro: dist.new_group() on the gloo backend collides with the
rendezvous keys of the default group, because ProcessGroupGloo's
constructor runs a second, unprefixed connectFullMesh: gloo's full-mesh
rendezvous always uses keys "0".."world_size-1", so a later new_group()
reads/overwrites addresses the default group's connectFullMesh already
published there.
"""
# paddlepaddle == 3.2.2
import paddle
import paddle.distributed as dist
g0 = dist.init_parallel_env() # default group: gloo full-mesh rendezvous over keys "0".."3"
rank = dist.get_rank()
world_size = dist.get_world_size()
t = paddle.to_tensor([rank + 1.0])
dist.all_reduce(t)
print(f"[rank {rank}] default all_reduce {t.numpy()}", flush=True)
# Same 4 ranks as the default group -> connectFullMesh reuses keys "0".."3"
# a second time, racing/colliding with the addresses already stored there.
g = dist.new_group(list(range(world_size)), backend="gloo")
print(f"[rank {rank}] new_group id={g.id} ok", flush=True)
dist.barrier(group=g0)
print(f"[rank {rank}] done", flush=True)Command (4 ranks, one process each, CPU only):
export CUDA_VISIBLE_DEVICES="" GLOO_SOCKET_IFNAME=lo PADDLE_DISTRI_BACKEND=gloo
for r in 0 1 2 3; do
python3 -m paddle.distributed.launch --nnodes 4 --master 127.0.0.1:29771 \
--rank $r --log_dir log-$r issue1_new_group_collision.py &
done
wait(--nproc_per_node 4 in place of one launcher per rank fails in the launcher itself with an unrelated IndexError on this CUDA-less machine.)
Actual output. Run 10 times with a fresh port each time, this exact script (including the trailing barrier) fails on 7/10 runs. The dominant signature is an immediate Connection refused on one or more ranks, e.g. rank 0:
[rank 0] default all_reduce [10.]
Traceback (most recent call last):
File "issue1_new_group_collision.py", line 22, in <module>
g = dist.new_group(list(range(world_size)), backend="gloo")
File ".../paddle/distributed/collective.py", line 261, in new_group
pg = _new_process_group_impl(
File ".../paddle/distributed/collective.py", line 169, in _new_process_group_impl
pg = core.ProcessGroupGloo.create(store, rank, world_size, group_id)
RuntimeError: [/paddle/third_party/gloo/gloo/transport/tcp/pair.cc:784] connect [127.0.0.1]:40347: Connection refusedIn 3 of those 7 runs, one or more of the other ranks never progress at all — no traceback, still blocked (one either inside new_group()'s handshake, or past it and stuck at the trailing barrier() once a peer had already crashed and exited) when the external 90s timeout kills the whole run.
The remaining 3/10 runs happen to pass.
Expected: new_group() succeeds reliably, regardless of what groups were created earlier.
其他补充信息 Additional Supplementary Information
ProcessGroupGloo actually does two separate gloo rendezvous handshakes per group, and only one of them is safe. Source (develop, paddle/fluid/distributed/collective/process_group_gloo.cc):
std::shared_ptr<ProcessGroupGloo> ProcessGroupGloo::CreateProcessGroupGloo( // line 701
const std::shared_ptr<phi::distributed::Store>& store,
int rank, int size, int gid) {
...
phi::distributed::CommContextManager::CreateGlooCommContext( // line 715
store, std::to_string(gid), rank, size);
auto process_group =
std::make_shared<ProcessGroupGloo>(store, rank, size, gid, opts); // line 718
...
}
ProcessGroupGloo::ProcessGroupGloo(...) // line 155
: ProcessGroupWithoutStream(rank, world_size, gid), _tag(0),
_store(new GlooStore(store)) { // line 163
_context = std::make_shared<gloo::rendezvous::Context>(rank, world_size);
_context->connectFullMesh(*_store, options->device); // line 165
}CreateGlooCommContext(paddle/phi/core/distributed/comm_context_manager.cc, lines 168-186) wraps the store in agloo::rendezvous::PrefixStorekeyed bystd::to_string(gid)before rendezvousing. This is correct, and it's what every actual collective andSend/Recvuses viaGetCommContext()(line 723) /_comm_context.- Right after, the
ProcessGroupGlooconstructor it just invoked does a second, unprefixedconnectFullMeshon the raw store, storing the result in_context. This member is exposed only by the public getterget_context()(process_group_gloo.h, line 186), which nothing else in Paddle calls (verified via a full-repo code search) — it is dead weight.
Because step 2 uses the raw store with no prefix, gloo's full-mesh rendezvous (gloo/rendezvous/context.cc, Context::connectFullMesh, keys key << i for rank i, i.e. "0".."n-1") collides across every ProcessGroupGloo ever constructed in the process, including the default group created by init_parallel_env(). This is what produces the Connection refused / hang above. Torch avoids this at the Python layer: _new_process_group_helper in torch/distributed/distributed_c10d.py wraps the store in PrefixStore(f"{group_name}/", store) before it ever reaches ProcessGroupGloo.
Suggested fix: since _context/get_context() appear unused, drop the constructor's own connectFullMesh call (and the _context/_store members) and rely solely on the already-correct CreateGlooCommContext rendezvous. If get_context() must stay for API/ABI reasons, wrap _store in a gloo::rendezvous::PrefixStore(std::to_string(gid), *_store) first, matching CreateGlooCommContext.
Same code (same double rendezvous) is present on v3.0.0 and v3.1.0, with line numbers shifted by at most 1.
Found while testing subgroup loading in fastsafetensors (foundation-model-stack/fastsafetensors).
Source: PaddlePaddle/Paddle