Repository: pytorch
Issue: Segfault, possibly due to recursion limit 
Labels: module: crash,module: multiprocessing,triaged

### 🐛 Describe the bug

I am occasionally getting a segfault towards the end of my programs. When I run in GDB to see the stack trace, I notice about ~20K frames of alternating between `torch::(anonymous namespace)::CudaIPCSentDataDelete(void*)` and `torch::CudaIPCSentData::~CudaIPCSentData()`, so I think with a stack frame this large its segfaulting due to the size. 

I can maybe try to get a minimal example and update this post if wanted, but I'm working on a fairly large (closed) codebase where I'm using a model with shared memory (`model.shared()`), spawning long running multiple processes using the torch multiprocessing library, and doing multiprocess inference to gather data (think like a search algorithm with a different problem instance for each process) and training in the single master process. The inference code and training works perfectly fine, I only segfault on cleanup.

I am creating my Pool as such:
```python
# Global stop token reference per process and disable spawned process signal handler
# Stop token is just a multiprocessing Event
def _init_pool_processes(_stop_token):
    signal.signal(signal.SIGINT, signal.SIG_IGN)
    global stop_token
    stop_token = _stop_token

pool = torch.multiprocessing.Pool(
        processes=num_threads,
        initializer=_init_pool_processes,
        initargs=(stop_token,),
    )
```
and I am closing the pool as such (where I think it segfaults):
```python
pool.close()
pool.join()
```

EDIT: I will note that I am using a global seed for experiments, and depending on the seed I am using, I will get this crash or not. There are experiments that can complete and clean up without crashing, so it can be a bit tricky to reproduce.

Here is my stacktrace when it segfaults (note that this repeats for the 28K more stack frames:

```
... (28K lines of repeated)
#28301 0x00007fff98c6047a in torch::CudaIPCSentData::~CudaIPCSentData() () from /home/tuero/miniconda3/envs/hptslite/lib/python3.11/site-packages/torch/lib/libtorch_cuda.so
#28302 0x00007fff98c62405 in torch::(anonymous namespace)::CudaIPCSentDataDelete(void*) () from /home/tuero/miniconda3/envs/hptslite/lib/python3.11/site-packages/torch/lib/libtorch_cuda.so
#28303 0x00007fff98c6047a in torch::CudaIPCSentData::~CudaIPCSentData() () from /home/tuero/miniconda3/envs/hptslite/lib/python3.11/site-packages/torch/lib/libtorch_cuda.so
#28304 0x00007fff98c62405 in torch::(anonymous namespace)::CudaIPCSentDataDelete(void*) () from /home/tuero/miniconda3/envs/hptslite/lib/python3.11/site-packages/torch/lib/libtorch_cuda.so
#28305 0x00007fff98c6047a in torch::CudaIPCSentData::~CudaIPCSentData() () from /home/tuero/miniconda3/envs/hptslite/lib/python3.11/site-packages/torch/lib/libtorch_cuda.so
#28306 0x00007fff98c62405 in torch::(anonymous namespace)::CudaIPCSentDataDelete(void*) () from /home/tuero/miniconda3/envs/hptslite/lib/python3.11/site-packages/torch/lib/libtorch_cuda.so
#28307 0x00007fff98c6047a in torch::CudaIPCSentData::~CudaIPCSentData() () from /home/tuero/miniconda3/envs/hptslite/lib/python3.11/site-packages/torch/lib/libtorch_cuda.so
#28308 0x00007fff98c62405 in torch::(anonymous namespace)::CudaIPCSentDataDelete(void*) () from /home/tuero/miniconda3/envs/hptslite/lib/python3.11/site-packages/torch/lib/libtorch_cuda.so
#28309 0x00007fff98c6047a in torch::CudaIPCSentData::~CudaIPCSentData() () from /home/tuero/miniconda3/envs/hptslite/lib/python3.11/site-packages/torch/lib/libtorch_cuda.so
#28310 0x00007fff98c62405 in torch::(anonymous namespace)::CudaIPCSentDataDelete(void*) () from /home/tuero/miniconda3/envs/hptslite/lib/python3.11/site-packages/torch/lib/libtorch_cuda.so
#28311 0x00007fff98c6047a in torch::CudaIPCSentData::~CudaIPCSentData() () from /home/tuero/miniconda3/envs/hptslite/lib/python3.11/site-packages/torch/lib/libtorch_cuda.so
#28312 0x00007fff98c62405 in torch::(anonymous namespace)::CudaIPCSentDataDelete(void*) () from /home/tuero/miniconda3/envs/hptslite/lib/python3.11/site-packages/torch/lib/libtorch_cuda.so
#28313 0x00007fff98c6047a in torch::CudaIPCSentData::~CudaIPCSentData() () from /home/tuero/miniconda3/envs/hptslite/lib/python3.11/site-packages/torch/lib/libtorch_cuda.so
#28314 0x00007fff98c62405 in torch::(anonymous namespace)::CudaIPCSentDataDelete(void*) () from /home/tuero/miniconda3/envs/hptslite/lib/python3.11/site-packages/torch/lib/libtorch_cuda.so
#28314 0x00007fff98c62405 in torch::(anonymous namespace)::CudaIPCSentDataDelete(void*) () from /home/tuero/miniconda3/envs/hptslite/lib/python3.11/site-packages/torch/lib/libtorch_cuda.so
#28315 0x00007fffe27c0810 in c10::StorageImpl::~StorageImpl() () from /home/tuero/miniconda3/envs/hptslite/lib/python3.11/site-packages/torch/lib/libtorch_python.so
#28316 0x00007fffe3d5ba5f in c10::intrusive_ptr<c10::StorageImpl, c10::detail::intrusive_target_default_null_type<c10::StorageImpl> >::reset_() () from /home/tuero/miniconda3/envs/hptslite/lib/python3.11/site-packages/torch/lib/libc10.so
#28317 0x00007fffe3d54cdb in c10::TensorImpl::~TensorImpl() () from /home/tuero/miniconda3/envs/hptslite/lib/python3.11/site-packages/torch/lib/libc10.so
#28318 0x00007fffe3d54e89 in c10::TensorImpl::~TensorImpl() () from /home/tuero/miniconda3/envs/hptslite/lib/python3.11/site-packages/torch/lib/libc10.so
#28319 0x00007fffe2a7cf98 in THPVariable_clear(THPVariable*) () from /home/tuero/miniconda3/envs/hptslite/lib/python3.11/site-packages/torch/lib/libtorch_python.so
#28320 0x00007fffe2a7d2e6 in THPVariable_subclass_dealloc(_object*) () from /home/tuero/miniconda3/envs/hptslite/lib/python3.11/site-packages/torch/lib/libtorch_python.so
#28321 0x000000000050283a in _Py_Dealloc (op=<optimized out>) at /usr/local/src/conda/python-3.11.8/Objects/object.c:2390
#28322 Py_DECREF (op=<optimized out>) at /usr/local/src/conda/python-3.11.8/Include/object.h:538
#28323 Py_XDECREF (op=<optimized out>) at /usr/local/src/conda/python-3.11.8/Include/object.h:602
#28324 free_keys_object (keys=0x7fff183021b0) at /usr/local/src/conda/python-3.11.8/Objects/dictobject.c:664
#28325 dictkeys_decref (dk=0x7fff183021b0) at /usr/local/src/conda/python-3.11.8/Objects/dictobject.c:324
#28326 dict_dealloc (mp=0x7fff105c6140) at /usr/local/src/conda/python-3.11.8/Objects/dictobject.c:2378
#28327 0x0000000000502978 in _Py_Dealloc (op=<optimized out>) at /usr/local/src/conda/python-3.11.8/Objects/object.c:2390
#28328 Py_DECREF (op=<optimized out>) at /usr/local/src/conda/python-3.11.8/Include/object.h:538
#28329 Py_XDECREF (op=<optimized out>) at /usr/local/src/conda/python-3.11.8/Include/object.h:602
#28330 free_keys_object (keys=0x15475820) at /usr/local/src/conda/python-3.11.8/Objects/dictobject.c:672
#28331 dictkeys_decref (dk=0x15475820) at /usr/local/src/conda/python-3.11.8/Objects/dictobject.c:324
#28332 dict_dealloc (mp=0x7fff19d92ed0) at /usr/local/src/conda/python-3.11.8/Objects/dictobject.c:2378
#28333 0x000000000055e860 in _Py_Dealloc (op=<optimized out>) at /usr/local/src/conda/python-3.11.8/Objects/object.c:2373
#28334 Py_DECREF (op=<optimized out>) at /usr/local/src/conda/python-3.11.8/Include/object.h:538
#28335 Py_XDECREF (op=<optimized out>) at /usr/local/src/conda/python-3.11.8/Include/object.h:602
#28336 _PyObject_FreeInstanceAttributes (self=0x7fff2437ee90) at /usr/local/src/conda/python-3.11.8/Objects/dictobject.c:5583
#28337 subtype_dealloc (self=0x7fff2437ee90) at /usr/local/src/conda/python-3.11.8/Objects/typeobject.c:1433
#28338 0x00000000005029cf in Py_DECREF (op=<optimized out>) at /usr/local/src/conda/python-3.11.8/Include/object.h:538
#28339 Py_XDECREF (op=<optimized out>) at /usr/local/src/conda/python-3.11.8/Include/object.h:602
#28340 dict_dealloc (mp=0x7fff2432fd00) at /usr/local/src/conda/python-3.11.8/Objects/dictobject.c:2371
#28341 0x000000000055ea02 in Py_DECREF (op=<optimized out>) at /usr/local/src/conda/python-3.11.8/Include/object.h:538
#28342 subtype_dealloc (self=0x7fff105c6510) at /usr/local/src/conda/python-3.11.8/Objects/typeobject.c:1429
#28343 0x000000000055e788 in _Py_Dealloc (op=<optimized out>) at /usr/local/src/conda/python-3.11.8/Objects/object.c:2390
#28344 Py_DECREF (op=<optimized out>) at /usr/local/src/conda/python-3.11.8/Include/object.h:538
#28345 Py_XDECREF (op=<optimized out>) at /usr/local/src/conda/python-3.11.8/Include/object.h:602
#28346 _PyObject_FreeInstanceAttributes (self=0x7fff251c4e90) at /usr/local/src/conda/python-3.11.8/Objects/dictobject.c:5583
#28347 subtype_dealloc (self=0x7fff251c4e90) at /usr/local/src/conda/python-3.11.8/Objects/typeobject.c:1433
#28348 0x0000000000521be8 in _Py_Dealloc (op=<optimized out>) at /usr/local/src/conda/python-3.11.8/Objects/object.c:2390
#28349 Py_DECREF (op=<optimized out>) at /usr/local/src/conda/python-3.11.8/Include/object.h:538
#28350 Py_XDECREF (op=<optimized out>) at /usr/local/src/conda/python-3.11.8/Include/object.h:602
#28351 _PyFrame_Clear (frame=0x7ffff7fb4108) at /usr/local/src/conda/python-3.11.8/Python/frame.c:141
#28352 0x0000000000510e62 in _PyEvalFrameClearAndPop (frame=0x7ffff7fb4108, tstate=0x8a85f8 <_PyRuntime+166328>) at /usr/local/src/conda/python-3.11.8/Python/ceval.c:6406
#28353 _PyEval_EvalFrameDefault (tstate=<optimized out>, frame=0x7ffff7fb4020, throwflag=<optimized out>) at /usr/local/src/conda/python-3.11.8/Python/ceval.c:2435
#28354 0x00000000005cb78a in _PyEval_EvalFrame (throwflag=0, frame=0x7ffff7fb4020, tstate=0x8a85f8 <_PyRuntime+166328>) at /usr/local/src/conda/python-3.11.8/Include/internal/pycore_ceval.h:73
#28355 _PyEval_Vector (tstate=0x8a85f8 <_PyRuntime+166328>, func=0x7ffff6dcdf80, locals=0x7ffff6df2440, args=<optimized out>, argcount=<optimized out>, kwnames=<optimized out>) at /usr/local/src/conda/python-3.11.8/Python/ceval.c:6434
#28356 0x00000000005cae5f in PyEval_EvalCode (co=<optimized out>, globals=0x7ffff6df2440, locals=<optimized out>) at /usr/local/src/conda/python-3.11.8/Python/ceval.c:1148
#28357 0x00000000005ebf17 in run_eval_code_obj (tstate=0x8a85f8 <_PyRuntime+166328>, co=0x979910, globals=0x7ffff6df2440, locals=0x7ffff6df2440) at /usr/local/src/conda/python-3.11.8/Python/pythonrun.c:1741
#28358 0x00000000005e7aa0 in run_mod (mod=<optimized out>, filename=<optimized out>, globals=0x7ffff6df2440, locals=0x7ffff6df2440, flags=<optimized out>, arena=<optimized out>) at /usr/local/src/conda/python-3.11.8/Python/pythonrun.c:1762
#28359 0x00000000005fcaa2 in pyrun_file (fp=fp@entry=0x914fd0, filename=filename@entry=0x7ffff6d5c390, start=start@entry=257, globals=globals@entry=0x7ffff6df2440, locals=locals@entry=0x7ffff6df2440, closeit=closeit@entry=1, flags=0x7fffffffda28) at /usr/local/src/conda/python-3.11.8/Python/pythonrun.c:1657
#28360 0x00000000005fbe6f in _PyRun_SimpleFileObject (fp=0x914fd0, filename=0x7ffff6d5c390, closeit=1, flags=0x7fffffffda28) at /usr/local/src/conda/python-3.11.8/Python/pythonrun.c:440
#28361 0x00000000005fbb93 in _PyRun_AnyFileObject (fp=0x914fd0, filename=0x7ffff6d5c390, closeit=1, flags=0x7fffffffda28) at /usr/local/src/conda/python-3.11.8/Python/pythonrun.c:79
#28362 0x00000000005f67de in pymain_run_file_obj (skip_source_first_line=0, filename=0x7ffff6d5c390, program_name=0x7ffff6c1d050) at /usr/local/src/conda/python-3.11.8/Modules/main.c:360
#28363 pymain_run_file (config=0x88e640 <_PyRuntime+59904>) at /usr/local/src/conda/python-3.11.8/Modules/main.c:379
#28364 pymain_run_python (exitcode=0x7fffffffda20) at /usr/local/src/conda/python-3.11.8/Modules/main.c:601
#28365 Py_RunMain () at /usr/local/src/conda/python-3.11.8/Modules/main.c:680
#28366 0x00000000005bb5c9 in Py_BytesMain (argc=<optimized out>, argv=<optimized out>) at /usr/local/src/conda/python-3.11.8/Modules/main.c:734
#28367 0x00007ffff7c23510 in __libc_start_call_main (main=main@entry=0x5bb520 <main>, argc=argc@entry=12, argv=argv@entry=0x7fffffffdc78) at ../sysdeps/nptl/libc_start_call_main.h:58
#28368 0x00007ffff7c235c9 in __libc_start_main_impl (main=0x5bb520 <main>, argc=12, argv=0x7fffffffdc78, init=<optimized out>, fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7fffffffdc68) at ../csu/libc-start.c:381
#28369 0x00000000005bb413 in _start ()
```

### Versions

```
$ python collect_env.py
Collecting environment information...
PyTorch version: 2.4.0.dev20240515+cu121
Is debug build: False
CUDA used to build PyTorch: 12.1
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.10 (x86_64)
GCC version: (Ubuntu 12.3.0-1ubuntu1~22.10) 12.3.0
Clang version: 18.0.0git (https://github.com/llvm/llvm-project.git b7376c319630a6b8395f3df5a46ba73e8fe29ea9)
CMake version: version 3.26.2
Libc version: glibc-2.36

Python version: 3.11.8 (main, Feb 26 2024, 21:39:34) [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-5.19.0-46-generic-x86_64-with-glibc2.36
Is CUDA available: True
CUDA runtime version: 12.3.107
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: NVIDIA GeForce RTX 3090
Nvidia driver version: 545.23.08
cuDNN version: Probably one of the following:
/usr/local/cuda-11.8/targets/x86_64-linux/lib/libcudnn.so.8.9.1
/usr/local/cuda-11.8/targets/x86_64-linux/lib/libcudnn_adv_infer.so.8.9.1
/usr/local/cuda-11.8/targets/x86_64-linux/lib/libcudnn_adv_train.so.8.9.1
/usr/local/cuda-11.8/targets/x86_64-linux/lib/libcudnn_cnn_infer.so.8.9.1
/usr/local/cuda-11.8/targets/x86_64-linux/lib/libcudnn_cnn_train.so.8.9.1
/usr/local/cuda-11.8/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8.9.1
/usr/local/cuda-11.8/targets/x86_64-linux/lib/libcudnn_ops_train.so.8.9.1
/usr/local/cuda-12.1/targets/x86_64-linux/lib/libcudnn.so.8.8.1
/usr/local/cuda-12.1/targets/x86_64-linux/lib/libcudnn_adv_infer.so.8.8.1
/usr/local/cuda-12.1/targets/x86_64-linux/lib/libcudnn_adv_train.so.8.8.1
/usr/local/cuda-12.1/targets/x86_64-linux/lib/libcudnn_cnn_infer.so.8.8.1
/usr/local/cuda-12.1/targets/x86_64-linux/lib/libcudnn_cnn_train.so.8.8.1
/usr/local/cuda-12.1/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8.8.1
/usr/local/cuda-12.1/targets/x86_64-linux/lib/libcudnn_ops_train.so.8.8.1
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
Address sizes:                   46 bits physical, 48 bits virtual
Byte Order:                      Little Endian
CPU(s):                          32
On-line CPU(s) list:             0-31
Vendor ID:                       GenuineIntel
Model name:                      Intel(R) Core(TM) i9-7960X CPU @ 2.80GHz
CPU family:                      6
Model:                           85
Thread(s) per core:              2
Core(s) per socket:              16
Socket(s):                       1
Stepping:                        4
CPU(s) scaling MHz:              65%
CPU max MHz:                     4400.0000
CPU min MHz:                     1200.0000
BogoMIPS:                        5599.85
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 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 cat_l3 cdp_l3 invpcid_single pti ssbd mba ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm 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 hwp hwp_act_window hwp_pkg_req md_clear flush_l1d arch_capabilities
Virtualization:                  VT-x
L1d cache:                       512 KiB (16 instances)
L1i cache:                       512 KiB (16 instances)
L2 cache:                        16 MiB (16 instances)
L3 cache:                        22 MiB (1 instance)
NUMA node(s):                    1
NUMA node0 CPU(s):               0-31
Vulnerability Itlb multihit:     KVM: Mitigation: VMX disabled
Vulnerability L1tf:              Mitigation; PTE Inversion; VMX conditional cache flushes, SMT vulnerable
Vulnerability Mds:               Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Meltdown:          Mitigation; PTI
Vulnerability Mmio stale data:   Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Retbleed:          Mitigation; IBRS
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:        Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:        Mitigation; IBRS, IBPB conditional, STIBP conditional, RSB filling, PBRSB-eIBRS Not affected
Vulnerability Srbds:             Not affected
Vulnerability Tsx async abort:   Mitigation; Clear CPU buffers; SMT vulnerable

Versions of relevant libraries:
[pip3] mypy==1.9.0
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.26.4
[pip3] pytorch-triton==3.0.0+45fff310c8
[pip3] torch==2.4.0.dev20240515+cu121
[pip3] torchaudio==2.2.0.dev20240515+cu121
[pip3] torchvision==0.19.0.dev20240515+cu121
[pip3] triton==2.3.0
[conda] blas                      1.0                         mkl
[conda] ffmpeg                    4.3                  hf484d3e_0    pytorch
[conda] libjpeg-turbo             2.0.0                h9bf148f_0    pytorch
[conda] mkl                       2023.1.0         h213fc3f_46344
[conda] mkl-service               2.4.0           py311h5eee18b_1
[conda] mkl_fft                   1.3.8           py311h5eee18b_0
[conda] mkl_random                1.2.4           py311hdb19cb5_0
[conda] numpy                     1.26.4          py311h08b1b3b_0
[conda] numpy-base                1.26.4          py311hf175353_0
[conda] pytorch-cuda              12.1                 ha16c6d3_5    pytorch
[conda] pytorch-mutex             1.0                        cuda    pytorch
[conda] pytorch-triton            3.0.0+45fff310c8          pypi_0    pypi
[conda] torch                     2.4.0.dev20240515+cu121          pypi_0    pypi
[conda] torchaudio                2.2.0.dev20240515+cu121          pypi_0    pypi
[conda] torchvision               0.19.0.dev20240515+cu121          pypi_0    pypi
[conda] triton                    2.3.0                    pypi_0    pypi
```

cc @VitalyFedyunin