Repository: pytorch
Issue: distributed.batch_isend_irecv() crash when send/recv refers to itself
Labels: oncall: distributed,module: crash,module: c10d

### 🐛 Describe the bug

Hi, I am implementing the all-to-all wrapper for gloo backend using batched_isend_irecv. The program crashes when the isend and irecv set the rank to be the current rank. The traceback does not refer to any Python code. 
```python
import torch
import torch.distributed as dist
def all_to_all(in_tensors: List[torch.Tensor]) -> List[torch.Tensor]:
    world_size = dist.get_world_size()
    rank = dist.get_rank()
    part2size = [None] * world_size
    sizes = [in_tensor.shape for in_tensor in in_tensors]
    dist.all_gather_object(part2size, sizes)
    op_list = []
    buffer = [None] * world_size
    for i in range(world_size):
        # if i == rank: # This fix the crash
        #    buffer[i] = in_tensors[i]
        #    continue
        op_list.append(dist.P2POp(dist.isend, in_tensors[i], i))
        buffer[i] = torch.empty(part2size[i][rank], dtype=in_tensors[i].dtype)
        op_list.append(dist.P2POp(dist.irecv, buffer[i], i))
    reqs = dist.batch_isend_irecv(op_list)
    _ = [req.wait() for req in reqs]
    return buffer
```
This is a test case.
```python
data = [
    [
        torch.tensor([0, 1]),
        torch.tensor([2, 3]),
        torch.tensor([4]),
        torch.tensor([5]),
    ],
    [
        torch.tensor([10, 11, 12]),
        torch.tensor([13, 14]),
        torch.tensor([15, 16]),
        torch.tensor([17, 18]),
    ],
    [
        torch.tensor([20, 21]),
        torch.tensor([22]),
        torch.tensor([23]),
        torch.tensor([24]),
    ],
    [
        torch.tensor([30, 31]),
        torch.tensor([32, 33]),
        torch.tensor([34, 35]),
        torch.tensor([36]),
    ],
]

res = all_to_all(data[dist.get_rank()])
```

### Versions

Collecting environment information...
PyTorch version: 1.13.1+cu117
Is debug build: False
CUDA used to build PyTorch: 11.7
ROCM used to build PyTorch: N/A

OS: Amazon Linux 2 (x86_64)
GCC version: (GCC) 7.3.1 20180712 (Red Hat 7.3.1-15)
Clang version: Could not collect
CMake version: Could not collect
Libc version: glibc-2.2.5

Python version: 3.7.16 (default, Mar 10 2023, 03:25:26)  [GCC 7.3.1 20180712 (Red Hat 7.3.1-15)] (64-bit runtime)
Python platform: Linux-5.10.179-166.674.amzn2.x86_64-x86_64-with-glibc2.2.5
Is CUDA available: True
CUDA runtime version: 11.7.99
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: Tesla T4
Nvidia driver version: 525.85.12
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture:        x86_64
CPU op-mode(s):      32-bit, 64-bit
Byte Order:          Little Endian
CPU(s):              32
On-line CPU(s) list: 0-31
Thread(s) per core:  2
Core(s) per socket:  16
Socket(s):           1
NUMA node(s):        1
Vendor ID:           GenuineIntel
CPU family:          6
Model:               85
Model name:          Intel(R) Xeon(R) Platinum 8259CL CPU @ 2.50GHz
Stepping:            7
CPU MHz:             3149.801
BogoMIPS:            4999.99
Hypervisor vendor:   KVM
Virtualization type: full
L1d cache:           32K
L1i cache:           32K
L2 cache:            1024K
L3 cache:            36608K
NUMA node0 CPU(s):   0-31
Flags:               fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single pti fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx avx512f avx512dq rdseed adx smap clflushopt clwb avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves ida arat pku ospke avx512_vnni

Versions of relevant libraries:
[pip3] numpy==1.21.6
[pip3] torch==1.13.1
[pip3] torchaudio==0.13.1
[pip3] torchvision==0.14.1
[conda] No relevant packages

cc @mrshenli @pritamdamania87 @zhaojuanmao @satgera @rohan-varma @gqchen @aazzolini @osalpekar @jiayisuse @H-Huang @kwen2501 @awgu @penguinwu