Repository: pytorch
Issue: Seeing unit test failures for ProcessGroupShareTensorTest
Labels: high priority,triage review,oncall: distributed,module: nccl,module: c10d

## 🐛 Bug

Seeing unit test failures for `ProcessGroupShareTensorTest.test_shared_{allgather, allreduce, broadcast}_nccl`

## To Reproduce

Steps to reproduce the behavior:

```
python test/run_test.py -i distributed/test_c10d_spawn_nccl -- -v -k allgather
```

Interestingly, I'm seeing different error messages on different machines

### Machine 1

```
# python test/run_test.py -i distributed/test_c10d_spawn_nccl -- -v -k allgather
Running distributed/test_c10d_spawn_nccl ... [2021-08-04 22:57:15.112665]
Executing ['/opt/conda/bin/python', 'distributed/test_c10d_spawn_nccl.py', '-v', '-k', 'allgather'] ... [2021-08-04 22:57:15.112709]
test_shared_allgather_nccl (__main__.ProcessGroupShareTensorTest) ... ok

----------------------------------------------------------------------
Ran 1 test in 8.490s

OK
[W CudaIPCTypes.cpp:15] Producer process has been terminated before all shared CUDA tensors released. See Note [Sharing CUDA tensors]
Traceback (most recent call last):
  File "test/run_test.py", line 1092, in <module>
    main()
  File "test/run_test.py", line 1071, in main
    raise RuntimeError(err_message)
RuntimeError: distributed/test_c10d_spawn_nccl failed! Received signal: SIGSEGV
```

stacktraces:

```
#0  0x00007feb2f59579f in torch::(anonymous namespace)::CudaIPCSentDataLimbo::collect() () from /opt/pytorch/pytorch/torch/lib/libtorch_cuda.so
#1  0x00007feb2f596a3b in torch::(anonymous namespace)::CudaIPCGlobalEntities::~CudaIPCGlobalEntities() () from /opt/pytorch/pytorch/torch/lib/libtorch_cuda.so
#2  0x00007feba9507a27 in ?? () from /usr/lib/x86_64-linux-gnu/libc.so.6
#3  0x00007feba9507be0 in exit () from /usr/lib/x86_64-linux-gnu/libc.so.6
#4  0x0000564f1c2d06c8 in Py_Exit (sts=0) at /tmp/build/80754af9/python-split_1628000493704/work/Python/pylifecycle.c:2295
#5  0x0000564f1c2d06fb in handle_system_exit () at /tmp/build/80754af9/python-split_1628000493704/work/Python/pythonrun.c:684
#6  0x0000564f1c2d0740 in _PyErr_PrintEx (tstate=0x564f1caa5cf0, set_sys_last_vars=1) at /tmp/build/80754af9/python-split_1628000493704/work/Python/pythonrun.c:694
#7  0x0000564f1c2d36c4 in pyrun_simple_file (flags=0x7ffd9e53a148, closeit=<optimized out>, filename=0x7feba927f1b0, fp=<optimized out>)
    at /tmp/build/80754af9/python-split_1628000493704/work/Python/pythonrun.c:445
#8  PyRun_SimpleFileExFlags (fp=<optimized out>, filename=<optimized out>, closeit=<optimized out>, flags=0x7ffd9e53a148) at /tmp/build/80754af9/python-split_1628000493704/work/Python/pythonrun.c:472
#9  0x0000564f1c2d3ae9 in pymain_run_file (cf=0x7ffd9e53a148, config=0x564f1caa5170) at /tmp/build/80754af9/python-split_1628000493704/work/Modules/main.c:391
#10 pymain_run_python (exitcode=0x7ffd9e53a140) at /tmp/build/80754af9/python-split_1628000493704/work/Modules/main.c:616
#11 Py_RunMain () at /tmp/build/80754af9/python-split_1628000493704/work/Modules/main.c:695
#12 0x0000564f1c2d3ce9 in Py_BytesMain (argc=<optimized out>, argv=<optimized out>) at /tmp/build/80754af9/python-split_1628000493704/work/Modules/main.c:1127
#13 0x00007feba94e50b3 in __libc_start_main () from /usr/lib/x86_64-linux-gnu/libc.so.6
```

### Machine 2

```
======================================================================
ERROR [2.094s]: test_shared_allgather_nccl (__main__.ProcessGroupShareTensorTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/opt/pytorch/pytorch/torch/testing/_internal/common_utils.py", line 2588, in wrapper
    return func(*args, **kwargs)
  File "/opt/pytorch/pytorch/torch/testing/_internal/common_utils.py", line 2588, in wrapper
    return func(*args, **kwargs)
  File "distributed/test_c10d_spawn_nccl.py", line 61, in test_shared_allgather_nccl
    self._test_multiprocess(
  File "/opt/pytorch/pytorch/test/distributed/test_c10d_spawn.py", line 47, in _test_multiprocess
    p.start()
  File "/opt/conda/lib/python3.8/multiprocessing/process.py", line 121, in start
    self._popen = self._Popen(self)
  File "/opt/conda/lib/python3.8/multiprocessing/context.py", line 284, in _Popen
    return Popen(process_obj)
  File "/opt/conda/lib/python3.8/multiprocessing/popen_spawn_posix.py", line 32, in __init__
    super().__init__(process_obj)
  File "/opt/conda/lib/python3.8/multiprocessing/popen_fork.py", line 19, in __init__
    self._launch(process_obj)
  File "/opt/conda/lib/python3.8/multiprocessing/popen_spawn_posix.py", line 47, in _launch
    reduction.dump(process_obj, fp)
  File "/opt/conda/lib/python3.8/multiprocessing/reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
  File "/opt/pytorch/pytorch/torch/multiprocessing/reductions.py", line 247, in reduce_tensor
    event_sync_required) = storage._share_cuda_()
RuntimeError: falseINTERNAL ASSERT FAILED at "../aten/src/ATen/MapAllocator.cpp":263, please report a bug to PyTorch. unable to open shared memory object </torch_18235_0> in read-write mode
```

stacktraces:

Not available for now. 

## Expected behavior

No fail

## Environment

pytorch is built from source with the submodule nccl

```
Collecting environment information...
PyTorch version: 1.10.0a0+git773a8ee
Is debug build: False
CUDA used to build PyTorch: 11.4
ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.2 LTS (x86_64)
GCC version: (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Clang version: Could not collect
CMake version: version 3.19.6
Libc version: glibc-2.31

Python version: 3.8.11 (default, Aug  3 2021, 15:09:35)  [GCC 7.5.0] (64-bit runtime)
Python platform: Linux-5.4.0-74-generic-x86_64-with-glibc2.17
Is CUDA available: True
CUDA runtime version: 11.4.x
GPU models and configuration:
GPU 0: A100-SXM4-40GB
GPU 1: A100-SXM4-40GB
GPU 2: A100-SXM4-40GB
GPU 3: A100-SXM4-40GB
GPU 4: A100-SXM4-40GB
GPU 5: A100-SXM4-40GB
GPU 6: A100-SXM4-40GB
GPU 7: A100-SXM4-40GB

Nvidia driver version: 450.119.04
cuDNN version: Probably one of the following:

HIP runtime version: N/A
MIOpen runtime version: N/A

Versions of relevant libraries:
[pip3] numpy==1.20.3
[pip3] torch==1.10.0a0+git773a8ee
[pip3] torchvision==0.11.0a0+876117b
[conda] numpy                     1.20.3           py38h3dbb7de_0
[conda] torch                     1.10.0a0+git773a8ee           dev_0    <develop>
[conda] torchvision               0.11.0a0+876117b          pypi_0    pypi
```

## Additional context

The test failures started to appear intermittently last week.

cc @ezyang @gchanan @zou3519 @bdhirsh @jbschlosser @anjali411 @pietern @mrshenli @pritamdamania87 @zhaojuanmao @satgera @rohan-varma @gqchen @aazzolini @osalpekar @jiayisuse @agolynski @SciPioneer @H-Huang @mrzzd @cbalioglu @gcramer23 @ptrblck @mcarilli