Repository: pytorch
Issue: [pgnccl] unstable restarting of PGs
Labels: oncall: distributed,module: c10d

### 🐛 Describe the bug

We are writing a simple UT to verify if NCCL comms can be restarted (abort first, then re-initialize) e.g., https://github.com/pytorch/pytorch/pull/139123. However we found the test is unstable under non-blocking mode. Some time errors like below and we are curious on the root cause

```
[I1028 13:05:11.985271293 ProcessGroupNCCL.cpp:2485] [PG ID 1 PG GUID 0(default_pg) Rank 1] NCCL_DEBUG: INFO
devgpu009:2131383:2143887 [1] NCCL INFO Using non-device net plugin version 0
devgpu009:2131383:2143887 [1] NCCL INFO Using network Socket
devgpu009:2131383:2143887 [1] misc/socket.cc:484 NCCL WARN socketStartConnect: Connect to 2803:6080:a020:37e7::1<45353> failed : Software caused connection abort
devgpu009:2131383:2143887 [1] NCCL INFO misc/socket.cc:567 -> 2
devgpu009:2131383:2143887 [1] NCCL INFO misc/socket.cc:621 -> 2
devgpu009:2131383:2143887 [1] NCCL INFO bootstrap.cc:285 -> 2
devgpu009:2131383:2143887 [1] NCCL INFO init.cc:1534 -> 2
devgpu009:2131383:2143887 [1] NCCL INFO group.cc:64 -> 2 [Async thread]
devgpu009:2131383:2143886 [0] NCCL INFO group.cc:64 -> 2 [Async thread]
[rank1]:E1028 13:05:11.472000 2131383 site-packages/torch/testing/_internal/common_distributed.py:699] Caught exception:
[rank1]:E1028 13:05:11.472000 2131383 site-packages/torch/testing/_internal/common_distributed.py:699] Traceback (most recent call last):
[rank1]:E1028 13:05:11.472000 2131383 site-packages/torch/testing/_internal/common_distributed.py:699]   File "/home/sqzhang/.conda/envs/sqzhang_1/lib/python3.12/site-packages/torch/testing/_internal/common_distributed.py", line 692, in run_test
[rank1]:E1028 13:05:11.472000 2131383 site-packages/torch/testing/_internal/common_distributed.py:699]     getattr(self, test_name)()
[rank1]:E1028 13:05:11.472000 2131383 site-packages/torch/testing/_internal/common_distributed.py:699]   File "/home/sqzhang/.conda/envs/sqzhang_1/lib/python3.12/site-packages/torch/testing/_internal/common_distributed.py", line 565, in wrapper
[rank1]:E1028 13:05:11.472000 2131383 site-packages/torch/testing/_internal/common_distributed.py:699]     fn()
[rank1]:E1028 13:05:11.472000 2131383 site-packages/torch/testing/_internal/common_distributed.py:699]   File "/home/sqzhang/.conda/envs/sqzhang_1/lib/python3.12/site-packages/torch/testing/_internal/common_utils.py", line 2996, in wrapper
[rank1]:E1028 13:05:11.472000 2131383 site-packages/torch/testing/_internal/common_distributed.py:699]     method(*args, **kwargs)
[rank1]:E1028 13:05:11.472000 2131383 site-packages/torch/testing/_internal/common_distributed.py:699]   File "/home/sqzhang/pytorch/test/distributed/test_c10d_nccl.py", line 382, in test_restart_pg
[rank1]:E1028 13:05:11.472000 2131383 site-packages/torch/testing/_internal/common_distributed.py:699]     dist.all_reduce(t1)
[rank1]:E1028 13:05:11.472000 2131383 site-packages/torch/testing/_internal/common_distributed.py:699]   File "/home/sqzhang/.conda/envs/sqzhang_1/lib/python3.12/site-packages/torch/distributed/c10d_logger.py", line 83, in wrapper
[rank1]:E1028 13:05:11.472000 2131383 site-packages/torch/testing/_internal/common_distributed.py:699]     return func(*args, **kwargs)
[rank1]:E1028 13:05:11.472000 2131383 site-packages/torch/testing/_internal/common_distributed.py:699]            ^^^^^^^^^^^^^^^^^^^^^
[rank1]:E1028 13:05:11.472000 2131383 site-packages/torch/testing/_internal/common_distributed.py:699]   File "/home/sqzhang/.conda/envs/sqzhang_1/lib/python3.12/site-packages/torch/distributed/distributed_c10d.py", line 2698, in all_reduce
[rank1]:E1028 13:05:11.472000 2131383 site-packages/torch/testing/_internal/common_distributed.py:699]     work = group.allreduce([tensor], opts)
[rank1]:E1028 13:05:11.472000 2131383 site-packages/torch/testing/_internal/common_distributed.py:699]            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[rank1]:E1028 13:05:11.472000 2131383 site-packages/torch/testing/_internal/common_distributed.py:699] torch.distributed.DistBackendError: NCCL error in: /home/sqzhang/pytorch/torch/csrc/distributed/c10d/NCCLUtils.cpp:36, unhandled system error (run with NCCL_DEBUG=INFO for details), NCCL version 2.21.5
[rank1]:E1028 13:05:11.472000 2131383 site-packages/torch/testing/_internal/common_distributed.py:699] ncclSystemError: System call (e.g. socket, malloc) or external library call failed or device error.
[rank1]:E1028 13:05:11.472000 2131383 site-packages/torch/testing/_internal/common_distributed.py:699] Last error:
[rank1]:E1028 13:05:11.472000 2131383 site-packages/torch/testing/_internal/common_distributed.py:699] socketStartConnect: Connect to 2803:6080:a020:37e7::1<45353> failed : Software caused connection abort
```

### Versions

Resolving fwdproxy (fwdproxy)... 2401:db00:31ff:ff2f:face:b00c:0:1e10
Connecting to fwdproxy (fwdproxy)|2401:db00:31ff:ff2f:face:b00c:0:1e10|:8080... connected.
Proxy request sent, awaiting response... 200 OK
Length: 24366 (24K) [text/plain]
Saving to: ‘collect_env.py’

collect_env.py                                     100%[=============================================================================================================>]  23.79K  --.-KB/s    in 0.002s

2024-10-28 16:36:32 (10.1 MB/s) - ‘collect_env.py’ saved [24366/24366]

(sqzhang_1) [sqzhang@devgpu009.cln1 ~/pytorch (4a75dbd7)]$ python collect_env.py
Collecting environment information...
PyTorch version: N/A
Is debug build: N/A
CUDA used to build PyTorch: N/A
ROCM used to build PyTorch: N/A

OS: CentOS Stream 9 (x86_64)
GCC version: (GCC) 11.5.0 20240719 (Red Hat 11.5.0-2)
Clang version: 18.1.8 (CentOS 18.1.8-3.el9)
CMake version: version 3.26.4
Libc version: glibc-2.34

Python version: 3.12.0 | packaged by Anaconda, Inc. | (main, Oct  2 2023, 17:29:18) [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-5.12.0-0_fbk16_zion_7661_geb00762ce6d2-x86_64-with-glibc2.34
Is CUDA available: N/A
CUDA runtime version: 12.0.140
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration:
GPU 0: NVIDIA PG509-210
GPU 1: NVIDIA PG509-210
GPU 2: NVIDIA PG509-210
GPU 3: NVIDIA PG509-210
GPU 4: NVIDIA PG509-210
GPU 5: NVIDIA PG509-210
GPU 6: NVIDIA PG509-210
GPU 7: NVIDIA PG509-210

Nvidia driver version: 525.105.17
cuDNN version: Probably one of the following:
/usr/lib64/libcudnn.so.8.9.4
/usr/lib64/libcudnn_adv_infer.so.8.9.4
/usr/lib64/libcudnn_adv_train.so.8.9.4
/usr/lib64/libcudnn_cnn_infer.so.8.9.4
/usr/lib64/libcudnn_cnn_train.so.8.9.4
/usr/lib64/libcudnn_ops_infer.so.8.9.4
/usr/lib64/libcudnn_ops_train.so.8.9.4
/usr/local/cuda-12.0/targets/x86_64-linux/lib/libcudnn.so.8.9.2
/usr/local/cuda-12.0/targets/x86_64-linux/lib/libcudnn_adv_infer.so.8.9.2
/usr/local/cuda-12.0/targets/x86_64-linux/lib/libcudnn_adv_train.so.8.9.2
/usr/local/cuda-12.0/targets/x86_64-linux/lib/libcudnn_cnn_infer.so.8.9.2
/usr/local/cuda-12.0/targets/x86_64-linux/lib/libcudnn_cnn_train.so.8.9.2
/usr/local/cuda-12.0/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8.9.2
/usr/local/cuda-12.0/targets/x86_64-linux/lib/libcudnn_ops_train.so.8.9.2
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: N/A

CPU:
Architecture:                    x86_64
CPU op-mode(s):                  32-bit, 64-bit
Address sizes:                   46 bits physical, 48 bits virtual
Byte Order:                      Little Endian
CPU(s):                          192
On-line CPU(s) list:             0-191
Vendor ID:                       GenuineIntel
Model name:                      Intel(R) Xeon(R) Platinum 8339HC CPU @ 1.80GHz
CPU family:                      6
Model:                           85
Thread(s) per core:              2
Core(s) per socket:              24
Socket(s):                       4
Stepping:                        11
Frequency boost:                 enabled
CPU(s) scaling MHz:              100%
CPU max MHz:                     1801.0000
CPU min MHz:                     800.0000
BogoMIPS:                        3600.00
Flags:                           fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 cdp_l3 invpcid_single intel_ppin ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm mpx rdt_a avx512f avx512dq rdseed adx smap clflushopt clwb intel_pt avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local avx512_bf16 dtherm ida arat pln pts hwp hwp_act_window hwp_epp hwp_pkg_req pku ospke avx512_vnni md_clear flush_l1d arch_capabilities
Virtualization:                  VT-x
L1d cache:                       3 MiB (96 instances)
L1i cache:                       3 MiB (96 instances)
L2 cache:                        96 MiB (96 instances)
L3 cache:                        132 MiB (4 instances)
NUMA node(s):                    4
NUMA node0 CPU(s):               0-23,96-119
NUMA node1 CPU(s):               24-47,120-143
NUMA node2 CPU(s):               48-71,144-167
NUMA node3 CPU(s):               72-95,168-191
Vulnerability Itlb multihit:     Not affected
Vulnerability L1tf:              Not affected
Vulnerability Mds:               Not affected
Vulnerability Meltdown:          Not affected
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl and seccomp
Vulnerability Spectre v1:        Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:        Mitigation; Enhanced IBRS, IBPB conditional, RSB filling
Vulnerability Srbds:             Not affected
Vulnerability Tsx async abort:   Not affected

Versions of relevant libraries:
[pip3] flake8==6.1.0
[pip3] flake8-bugbear==23.3.23
[pip3] flake8-comprehensions==3.15.0
[pip3] flake8-executable==2.1.3
[pip3] flake8-logging-format==0.9.0
[pip3] flake8-pyi==23.3.1
[pip3] flake8-simplify==0.19.3
[pip3] mypy==1.11.2
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.26.0
[pip3] optree==0.13.0
[pip3] pytorch-sphinx-theme==0.0.24
[pip3] pytorch-triton==2.2.0+e28a256d71
[pip3] torch==2.3.0a0+git5894af8
[conda] magma-cuda110             2.5.2                         1    pytorch
[conda] mkl                       2023.1.0         h213fc3f_46344
[conda] mkl-include               2023.1.0         h06a4308_46344
[conda] numpy                     1.26.0                   pypi_0    pypi
[conda] optree                    0.13.0                   pypi_0    pypi
[conda] pytorch-sphinx-theme      0.0.24                    dev_0    <develop>
[conda] pytorch-triton            2.2.0+e28a256d71          pypi_0    pypi
[conda] torch                     2.3.0a0+gitdeb4f2c          pypi_0    pypi
[conda] torchfix                  0.4.0                    pypi_0    pypi

cc @H-Huang @awgu @kwen2501 @wanchaol @fegin @fduwjj @wz337 @wconstab @d4l3k @c-p-i-o