ray icon indicating copy to clipboard operation
ray copied to clipboard

[Bug] _NcclGroup.recv() synchronizes _recv_stream before comm.recv() and omits post-recv synchronization, returning stale buffer contents

Open SendoRay opened this issue 5 days ago • 1 comments

Summary

In python/ray/experimental/channel/nccl_group.py (commit e4462656e827790245c66d69e800cdfd8df0630a, lines 208-240), when use_communication_streams=True, _NcclGroup.recv() calls self._recv_stream.synchronize() before enqueuing self._comm.recv(buf, peer_rank, stream=self._recv_stream) and returns buf without synchronizing or inserting wait_stream(self._recv_stream) on the caller's stream after recv:

# python/ray/experimental/channel/nccl_group.py:208-240
if self._use_communication_streams:
    self._recv_stream.synchronize()
self._comm.recv(...)
# missing post-recv wait_stream / synchronize before returning buf

Because self._comm.recv runs asynchronously on self._recv_stream, returning buf without waiting for self._recv_stream allows any default-stream kernel or clone executed by the caller to read stale zeros/previous contents from buf—directly violating _NcclGroup.recv()'s guarantee that buf is safe to consume on return.

Affected Location

  • Repository: ray-project/ray (e4462656e827790245c66d69e800cdfd8df0630a)
  • File & lines: python/ray/experimental/channel/nccl_group.py:208-240 (_NcclGroup.recv)

Minimal Standalone Reproduction

Executed across 2x NVIDIA A800-SXM4-80GB ranks with NCCL 2.27.5 (use_communication_streams pattern, 16,777,216 float32 elements, expected value 33.0):

  1. Rank 0 queues GPU work then sends 33.0 on a communication stream.
  2. Rank 1 pre-syncs comm_stream, issues dist.recv(buf, src=0) inside with torch.cuda.stream(comm_stream):, and immediately clones buf on the default stream without wait_stream(comm_stream).

Observed behavior vs. waited-stream control:

  • Without post-recv wait_stream: Rank 1 observed 8/8 stale-read runs with 16,777,216 / 16,777,216 elements reading 0.0 instead of 33.0.
  • With torch.cuda.current_stream(device).wait_stream(comm_stream) after recv: 0/4 stale runs.

Suggested Fix

  1. Call torch.cuda.current_stream().wait_stream(self._recv_stream) (or record/wait a CUDA event or self._recv_stream.synchronize(), matching the use_communication_streams=False guarantee) after self._comm.recv(...) before returning buf.
  2. Add a 2-GPU NCCL regression test checking immediate default-stream reads after _NcclGroup.recv().

SendoRay avatar Nov 29 '26 06:11 SendoRay

Hi, I'd like to work on this issue. I'll first investigate the existing NCCL communication-stream handling and regression-test structure, then work on a minimal fix that preserves the expected synchronization guarantees. I'll share my findings and proposed changes before opening a PR.

nidhishkk avatar Nov 30 '26 13:11 nidhishkk