Skip to content

Reclaim a comm whose init stops in bootstrap - #2633

Open
kodlan wants to merge 1 commit into
NVIDIA:devfrom
kodlan:fix/reclaim-incomplete-init
Open

kodlan wants to merge 1 commit into
NVIDIA:devfrom
kodlan:fix/reclaim-incomplete-init

Conversation

@kodlan

@kodlan kodlan commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Description

Refs #2585

If the init of a communicator stops in the bootstrap, ncclCommAbort and ncclCommDestroy return ncclSuccess but free nothing, because commReclaim only does work when comm->intraComm0 is set. Example: rank 0 of 2 starts a non-blocking init, rank 1 never shows up, the app aborts and retries. Every attempt keeps about 1 MiB of device memory, 1.2 MiB of host memory, 3 sockets and the bootstrap root thread.

This PR follows path A from the issue. commReclaim gets a branch for bootstrap != NULL && peerInfo == NULL (and the init not in progress any more) that runs commDestroySync + commCleanup for this communicator alone. The rest makes that safe:

  1. ncclGroupJobAbort and ncclGroupJobComplete join the group thread through std::call_once, so every caller returns after the join.
  2. A split or shrink child whose init fails takes a reference on the group job of its parent, so its abort waits for the same join. A child has no group job of its own, and without this the "child aborted before the parent" case is still a use-after-free.
  3. The bootstrap root owns an abort flag and adds itself to a list of the running roots. bootstrapRootAbort sets the flag and waits until the root is gone. The early reclaim calls it first.

Three places where I did not follow the sketch exactly:

  • The roots are in an intrusive list under a mutex, not in a map. The lookup is by magic. It needs no allocation that can throw and it works when two roots have the same magic (NCCL_COMM_ID).
  • Only the roots that the rank sent its check-in to are stopped (the bootstrap state remembers their magic, at most 2). With comm->magic, ncclCommInitRankScalable keeps its second root, and a rank that failed before it reached the root breaks a retry with the same id that works today.
  • A root with an abort flag has non-blocking sockets, and a plain ncclSocketAccept on it spins (I measured 91% of a core). So the root uses the asynchronous accept and sleeps while no rank is connecting: 1 ms, and 16 ms after about 250 ms without a connection. If you prefer another way to wake the root, tell me.

The fail: hunk that unlinks parent->childAbortFlag is the same 4 lines as #2587. This PR needs it. If #2587 lands first I drop it here.

Related Issues

#2585, case 1 only. #2587 / #2586 for the shared hunk.

Changes & Impact

init.cc, group.cc, group.h, bootstrap.cc, bootstrap.h. No API change.

What changes for code that never aborts: the bootstrap root polls its listen socket and no longer blocks in accept, and a second caller of ncclGroupJobAbort / ncclGroupJobComplete waits for the join.

Not covered: case 2 of the issue, an init that fails before or inside commAlloc, a blocking init that still runs in another thread when the handle is aborted, and an unused unique id (it keeps its root). bootstrapInit also does not close its listen socket for the root on an early return. That is the 1 fd in the table, I can send it separately.

Tested on 2.32.6 with an RTX 5070 Ti and an RTX 2070 (SHM). Per attempt, 100 attempts:

before after
peer never joins, ncclCommAbort 1085 kB device, 1268 kB RSS, 3 fds, 1 thread 0 kB, 0 kB, 1 fd, 0 threads
ncclCommGrow, new rank never joins 1024 kB device, 1264 kB RSS, 3 fds 0 kB, 14 kB, 1 fd
complete 2-rank init time 13.9 ms 14.0 ms
rank 1 joins 50 ms late, init time 65.1 ms 65.7 ms
CPU while 1 root waits / 64 unused ids 0.3% / 0.3% of a core 0.4% / 5.6%

ASAN + UBSAN:

  • Two threads abort two communicators of one non-blocking group: 0 of 20 runs report. Without the group.cc change: heap-use-after-free in 5 of 20.
  • Non-blocking split, one child bootstrap fails (LD_PRELOAD shim on socket/bind/accept/connect), child aborted before the parents: 12 of 12 clean. Without the group job reference: use-after-free at 4 of 5 fault points.
  • Blocking split child with a failed bootstrap, both abort orders, splitShare 0 and 1: 64 of 64 clean.
  • ncclCommInitRankScalable with 2 ids, a peer connected to the root at abort time, NCCL_COMM_ID, NCCL_ENQUEUE_REARCH_ENABLE=1: clean.
  • Fault sweep, 248 runs: non-blocking init, split and split of a split with one failed socket, setsockopt, bind, listen, connect, accept, getifaddrs, open or pipe call, then abort of everything. No sanitizer report, no crash, no hang.

nccl-tests -c 1 pass on both GPUs, each GPU alone, -t 2, mpirun -np 2 and -np 4. No build warnings, the changed files build with WERROR=1, clang-format clean.

Not tested: multi-node, IB/RoCE, NCCL_OOB_NET_ENABLE, the TLS socket mode, Windows, more than 4 ranks.

Performance Impact

None on collectives, and no change of the init time in my measurements. A bootstrap root that waits for ranks wakes up every 1 ms (16 ms after about 250 ms) instead of sleeping in accept.

@besnardjb besnardjb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @kodlan, for the excellent work on this PR. It looks good overall, and my testing confirms that it fixes nearly all the leaks. I found one remaining file descriptor leak and left a few inline comments, including some suggestions to make the bootstrap root handling more robust.

Also, would you mind shortening the commit message a bit? Just the problem and the fix would be perfect.

Could you also rebase on the dev branch to ease integration? There are a few minor conflicts.

Thanks again for working on this!

Comment thread src/bootstrap.cc Resolved
Comment thread src/bootstrap.cc Resolved
Comment thread src/bootstrap.cc Resolved
@kodlan
kodlan force-pushed the fix/reclaim-incomplete-init branch from 12a9515 to ce22ca7 Compare November 25, 2026 20:28
@kodlan
kodlan changed the base branch from master to dev November 25, 2026 20:30
Problem: when the init of a communicator stops inside the bootstrap (a
peer never joins, a bootstrap socket fails), ncclCommAbort and
ncclCommDestroy return ncclSuccess but free nothing, because
commReclaim only works when comm->intraComm0 is set. Every attempt
keeps about 1 MiB of device memory, 1.2 MiB of host memory, the
bootstrap sockets and the bootstrap root thread.

Solution: commReclaim reclaims such a communicator alone (bootstrap
set, peerInfo not allocated, init not in progress). To make that safe,
every caller of ncclGroupJobAbort and ncclGroupJobComplete waits for
the join of the group thread (std::call_once), a failed split or shrink
child takes a reference on the group job of its parent, and the reclaim
first stops the bootstrap roots of this process that the rank checked
in with. bootstrapInit closes its root sockets on every failure path.

Refs NVIDIA#2585

Signed-off-by: Stanislav Bardyuk <6258615+kodlan@users.noreply.github.com>
@kodlan
kodlan force-pushed the fix/reclaim-incomplete-init branch from ce22ca7 to 2e25067 Compare November 25, 2026 20:32
@besnardjb

Copy link
Copy Markdown
Contributor

/mirror dev

@besnardjb

Copy link
Copy Markdown
Contributor

Thank you very much, @kodlan! I tested the updated version and no longer see any leaks. Moving it to internal review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants