Repository: pytorch
Issue: Python memory allocator called without holding the GIL when running torchrun under Python debug version
Labels: triaged,oncall: r2p

### 🐛 Describe the bug

I built Python 3.8.13 myself with pydebug, and used PyTorch 1.11.0.

First start with any script:
```python
# hello.py
print('Hello')
```

Then run `torchrun` with
```bash
torchrun --node_rank=0 --nproc_per_node=1 --nnodes=1 --master_addr=127.0.0.1 --master_port=1234 hello.py
```

This will throw the following error:
```
Fatal Python error: Python memory allocator called without holding the GIL
Python runtime state: initialized

Current thread 0x00007f41936513c0 (most recent call first):
  File "/mnt/efs/gq/conda-env-gq-dbg/lib/python3.8/site-packages/torch/distributed/elastic/utils/store.py", line 31 in get_all
  File "/mnt/efs/gq/conda-env-gq-dbg/lib/python3.8/site-packages/torch/distributed/elastic/utils/store.py", line 53 in synchronize
  File "/mnt/efs/gq/conda-env-gq-dbg/lib/python3.8/site-packages/torch/distributed/elastic/agent/server/api.py", line 647 in _share_and_gather
  File "/mnt/efs/gq/conda-env-gq-dbg/lib/python3.8/site-packages/torch/distributed/elastic/agent/server/api.py", line 610 in _assign_worker_ranks
  File "/mnt/efs/gq/conda-env-gq-dbg/lib/python3.8/site-packages/torch/distributed/elastic/metrics/api.py", line 125 in wrapper
  File "/mnt/efs/gq/conda-env-gq-dbg/lib/python3.8/site-packages/torch/distributed/elastic/agent/server/api.py", line 541 in _rendezvous
  File "/mnt/efs/gq/conda-env-gq-dbg/lib/python3.8/site-packages/torch/distributed/elastic/metrics/api.py", line 125 in wrapper
  File "/mnt/efs/gq/conda-env-gq-dbg/lib/python3.8/site-packages/torch/distributed/elastic/agent/server/api.py", line 678 in _initialize_workers
  File "/mnt/efs/gq/conda-env-gq-dbg/lib/python3.8/site-packages/torch/distributed/elastic/metrics/api.py", line 125 in wrapper
  File "/mnt/efs/gq/conda-env-gq-dbg/lib/python3.8/site-packages/torch/distributed/elastic/agent/server/api.py", line 844 in _invoke_run
  File "/mnt/efs/gq/conda-env-gq-dbg/lib/python3.8/site-packages/torch/distributed/elastic/agent/server/api.py", line 709 in run
  File "/mnt/efs/gq/conda-env-gq-dbg/lib/python3.8/site-packages/torch/distributed/elastic/metrics/api.py", line 125 in wrapper
  File "/mnt/efs/gq/conda-env-gq-dbg/lib/python3.8/site-packages/torch/distributed/launcher/api.py", line 236 in launch_agent
  File "/mnt/efs/gq/conda-env-gq-dbg/lib/python3.8/site-packages/torch/distributed/launcher/api.py", line 131 in __call__
  File "/mnt/efs/gq/conda-env-gq-dbg/lib/python3.8/site-packages/torch/distributed/run.py", line 715 in run
  File "/mnt/efs/gq/conda-env-gq-dbg/lib/python3.8/site-packages/torch/distributed/run.py", line 724 in main
  File "/mnt/efs/gq/conda-env-gq-dbg/lib/python3.8/site-packages/torch/distributed/elastic/multiprocessing/errors/__init__.py", line 345 in wrapper
  File "/mnt/efs/gq/conda-env-gq-dbg/bin/torchrun", line 8 in <module>
Aborted (core dumped)
```

Probably related to #26475.

### Versions

`collect_env.py` output goes as follows:
```
Collecting environment information...
PyTorch version: 1.11.0+cu102
Is debug build: False
CUDA used to build PyTorch: 10.2
ROCM used to build PyTorch: N/A

OS: Ubuntu 18.04.5 LTS (x86_64)
GCC version: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Clang version: Could not collect
CMake version: version 3.21.0
Libc version: glibc-2.27

Python version: 3.8.13 (tags/v3.8.13-dirty:ea673213dd, May 22 2022, 12:36:27)  [GCC 7.5.0] (64-bit runtime)
Python platform: Linux-5.4.0-1055-aws-x86_64-with-glibc2.27
Is CUDA available: True
CUDA runtime version: 11.1.105
GPU models and configuration:
GPU 0: Tesla M60
GPU 1: Tesla M60
GPU 2: Tesla M60
GPU 3: Tesla M60

Nvidia driver version: 450.142.00
cuDNN version: Probably one of the following:
/usr/local/cuda-10.1/targets/x86_64-linux/lib/libcudnn.so.7.6.5
/usr/local/cuda-10.2/targets/x86_64-linux/lib/libcudnn.so.7.6.5
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn.so.8.0.5
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_adv_infer.so.8.0.5
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_adv_train.so.8.0.5
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_cnn_infer.so.8.0.5
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_cnn_train.so.8.0.5
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8.0.5
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_ops_train.so.8.0.5
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn.so.8.0.5
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_adv_infer.so.8.0.5
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_adv_train.so.8.0.5
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_cnn_infer.so.8.0.5
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_cnn_train.so.8.0.5
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8.0.5
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_ops_train.so.8.0.5
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

Versions of relevant libraries:
[pip3] numpy==1.22.4
[pip3] torch==1.11.0
[conda] numpy                     1.22.4                   pypi_0    pypi
[conda] torch                     1.11.0                   pypi_0    pypi
```

I built my Python 3.8.13 within a conda environment that has a default Python 3.8 installation:
```
conda create -n py38-dbg python=3.8
```
with the following commands:
```bash
git clone https://github.com/python/cpython
cd cpython
git checkout v3.8.13
mkdir debug
cd debug
CPPFLAGS=-I$CONDA_PREFIX/include LDFLAGS="-L$CONDA_PREFIX/lib -Wl,-rpath,$CONDA_PREFIX/lib" ../configure --with-pydebug --prefix=$CONDA_PREFIX
make -j12
make install
```
I also installed `numpy` and `scipy`.