Repository: pytorch
Issue: ProcessGroup is not automatically destroyed when the process exits
Labels: oncall: distributed

### 🐛 Describe the bug

I may have discovered a bug when I registered the corresponding ProcessGroup development.
I found that the destructor of the ProcessGroup subclass object was not called when the program exited.
The destroy_process_group() function must be explicitly called to destruct the corresponding ProcessGroup object.

In my current test, there is a problem with 2.1, but there is no problem with 2.0.1 and previous versions.

The following is a simple use case of nccl. You need to add printing, etc. to the destructor `~ProcessGroupNCCL` in advance to see whether the destructor is called. (`torch/csrc/distributed/c10d/ProcessGroupNCCL.cpp`)

```
import os
import sys
import gc

import torch
import torch.distributed as dist
from torch.testing._internal.common_utils import TestCase, run_tests

class Test(TestCase):
    world_size_2p = 1
    data = torch.randn(10, 20)

    @classmethod
    def _init_dist_nccl(cls, rank, world_size):
        dist.init_process_group(
            backend='nccl', init_method= "tcp://127.0.0.1:29502", world_size=world_size, rank=rank)
        return dist

    def _test_alltoall_2p(cls, rank, data, world_size, init_pg):
        pg = init_pg(rank, world_size)
        print(torch.distributed.distributed_c10d.GroupMember.WORLD)
        print("----------")
        print("sys", sys.getrefcount(torch.distributed.distributed_c10d.GroupMember.WORLD))
        print("gc", gc.get_referrers(torch.distributed.distributed_c10d.GroupMember.WORLD))
        
        #pg.destroy_process_group()

    def _test_multiprocess_2p(self, f, init_pg):
        ws = self.world_size_2p
        data = self.data
        expected = []
        self._test_alltoall_2p(0, data, ws, init_pg)

    def test_alltoall_2p_dist(self):
        
        self._test_multiprocess_2p(
            Test._test_alltoall_2p,
            Test._init_dist_nccl)
        print('test_alltoall_2p_dist ')

if __name__ == '__main__':
    run_tests()
```

### Versions

Collecting environment information...
PyTorch version: 2.1.0a0+git591cb77
Is debug build: False
CUDA used to build PyTorch: 11.7
ROCM used to build PyTorch: N/A

OS: Ubuntu 18.04.1 LTS (x86_64)
GCC version: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Clang version: Could not collect
CMake version: version 3.25.2
Libc version: glibc-2.31

Python version: 3.8.13 (default, Mar 28 2022, 11:38:47)  [GCC 7.5.0] (64-bit runtime)
Python platform: Linux-4.15.0-184-generic-x86_64-with-glibc2.17
Is CUDA available: True
CUDA runtime version: 11.7.64
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration:
GPU 0: NVIDIA A100-PCIE-40GB
GPU 1: NVIDIA A100-PCIE-40GB
GPU 2: NVIDIA A100-PCIE-40GB
GPU 3: NVIDIA A100-PCIE-40GB
GPU 4: NVIDIA A100-PCIE-40GB
GPU 5: NVIDIA A100-PCIE-40GB
GPU 6: NVIDIA A100-PCIE-40GB
GPU 7: NVIDIA A100-PCIE-40GB

Nvidia driver version: 525.125.06
cuDNN version: Probably one of the following:
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn.so.8
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_adv_infer.so.8
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_adv_train.so.8
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_cnn_infer.so.8
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_cnn_train.so.8
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_ops_train.so.8
/usr/local/cuda-11.3/targets/x86_64-linux/lib/libcudnn.so.8
/usr/local/cuda-11.3/targets/x86_64-linux/lib/libcudnn_adv_infer.so.8
/usr/local/cuda-11.3/targets/x86_64-linux/lib/libcudnn_adv_train.so.8
/usr/local/cuda-11.3/targets/x86_64-linux/lib/libcudnn_cnn_infer.so.8
/usr/local/cuda-11.3/targets/x86_64-linux/lib/libcudnn_cnn_train.so.8
/usr/local/cuda-11.3/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8
/usr/local/cuda-11.3/targets/x86_64-linux/lib/libcudnn_ops_train.so.8
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):              72
On-line CPU(s) list: 0-71
Thread(s) per core:  2
Core(s) per socket:  18
Socket(s):           2
NUMA node(s):        2
Vendor ID:           GenuineIntel
CPU family:          6
Model:               85
Model name:          Intel(R) Xeon(R) Gold 6240 CPU @ 2.60GHz
Stepping:            7
CPU MHz:             1001.086
CPU max MHz:         2601.0000
CPU min MHz:         1000.0000
BogoMIPS:            5200.00
Virtualization:      VT-x
L1d cache:           32K
L1i cache:           32K
L2 cache:            1024K
L3 cache:            25344K
NUMA node0 CPU(s):   0-17,36-53
NUMA node1 CPU(s):   18-35,54-71
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 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 ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid 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 dtherm ida arat pln pts pku ospke avx512_vnni md_clear flush_l1d arch_capabilities

Versions of relevant libraries:
[pip3] flake8==4.0.1
[pip3] numpy==1.22.4
[pip3] pytorch-triton==2.1.0+e6216047b8
[pip3] torch==2.1.0a0+git591cb77
[pip3] torchvision==0.7.0
[conda] cudatoolkit               11.3.1               h2bc3f7f_2    https://repo.anaconda.com/pkgs/main
[conda] mkl                       2023.0.0                 pypi_0    pypi
[conda] mkl-include               2023.0.0                 pypi_0    pypi
[conda] numpy                     1.22.4                   pypi_0    pypi
[conda] pytorch-triton            2.1.0+e6216047b8          pypi_0    pypi
[conda] torch                     2.1.0a0+git591cb77          pypi_0    pypi
[conda] torchvision               0.9.0a0+8fb5838          pypi_0    pypi


cc @mrshenli @pritamdamania87 @zhaojuanmao @satgera @rohan-varma @gqchen @aazzolini @osalpekar @jiayisuse @H-Huang @kwen2501 @awgu @penguinwu