Pure virtual function call exception on Python interpreter exit when using debug wheel
Labels: triaged, module: python frontend

### 🐛 Describe the bug

I built a PyTorch wheel debug version. The problem is that after I import torch in Python interpreter and quit the interpreter I'm receiving pure virtual function call from the torch library. From my naive testing seems that https://github.com/pytorch/pytorch/blob/4eaaa086231b911ef768dd2d959f837b082efee6/CMakeLists.txt#L888 setting  -fno-omit-frame-pointer -O0 exposes the problem. Commenting on that line and using debug mode makes the problem non-existent. 

Steps to reproduce the problem:
```
git clone https://github.com/pytorch/pytorch.git
cd pytorch
git checkout v2.0.1
git submodule sync
git submodule update --init --recursive
curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh -b -p `pwd`/miniconda
. `pwd`/miniconda/etc/profile.d/conda.sh

# Hide local python packages from Conda (Conflicts with conan)
export PYTHONNOUSERSITE=1
conda create python=python3.8.10 ninja pyyaml setuptools cmake cffi typing git --prefix `pwd`/buildenv -y
conda activate `pwd`/buildenv

pip3 install -r requirements.txt

export USE_CUDA=0
export USE_MKLDNN=0
export DEBUG=1
export BUILD_TEST=0
export PYTORCH_BUILD_VERSION=2.0.1
export PYTORCH_BUILD_NUMBER=0
python setup.py bdist_wheel
ls -l dist/
cd dist 
python3 -m venv test
. /test/bin/activate.sh
pip3 install torch-2.0.1-cp38-cp38-linux_x86_64.whl
python3

Python 3.8.10 (default, Jun 22 2022, 20:18:18)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> quit()
pure virtual method called
terminate called without an active exception
Aborted
```

Stack trace:
```
#0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
#1  0x00007ffff7dda859 in __GI_abort () at abort.c:79
#2  0x00007ffff714d911 in ?? () from /lib/x86_64-linux-gnu/libstdc++.so.6
#3  0x00007ffff715938c in ?? () from /lib/x86_64-linux-gnu/libstdc++.so.6
#4  0x00007ffff71593f7 in std::terminate() () from /lib/x86_64-linux-gnu/libstdc++.so.6
#5  0x00007ffff715a155 in __cxa_pure_virtual () from /lib/x86_64-linux-gnu/libstdc++.so.6
#6  0x00007ffff58a898f in c10::SafePyObject::~SafePyObject (this=0x4458808, __in_chrg=<optimized out>) at ../c10/core/SafePyObject.h:32
#7  0x00007ffff618e7e4 in torch::impl::dispatch::PythonKernelHolder::~PythonKernelHolder (this=0x44587f0, __in_chrg=<optimized out>) at ../torch/csrc/utils/python_dispatch.cpp:103
#8  0x00007ffff618e810 in torch::impl::dispatch::PythonKernelHolder::~PythonKernelHolder (this=0x44587f0, __in_chrg=<optimized out>) at ../torch/csrc/utils/python_dispatch.cpp:103
#9  0x00007fffe46e7df9 in c10::intrusive_ptr<c10::OperatorKernel, c10::detail::intrusive_target_default_null_type<c10::OperatorKernel> >::reset_ (this=0xadbb50) at ../c10/util/intrusive_ptr.h:291
#10 0x00007fffe46e5a4a in c10::intrusive_ptr<c10::OperatorKernel, c10::detail::intrusive_target_default_null_type<c10::OperatorKernel> >::~intrusive_ptr (this=0xadbb50, __in_chrg=<optimized out>) at ../c10/util/intrusive_ptr.h:370
#11 0x00007fffe46e1be8 in c10::BoxedKernel::~BoxedKernel (this=0xadbb50, __in_chrg=<optimized out>) at ../aten/src/ATen/core/boxing/BoxedKernel.h:74
#12 0x00007fffe46e3484 in c10::KernelFunction::~KernelFunction (this=0xadbb50, __in_chrg=<optimized out>) at ../aten/src/ATen/core/boxing/KernelFunction.h:74
#13 0x00007fffe4a842f6 in std::array<c10::KernelFunction, 112ul>::~array (this=0xadb9f0, __in_chrg=<optimized out>) at /usr/include/c++/9/array:94
#14 0x00007fffe4a84396 in c10::impl::OperatorEntry::~OperatorEntry (this=0xadb970, __in_chrg=<optimized out>) at ../aten/src/ATen/core/dispatch/OperatorEntry.h:70
#15 0x00007fffe4a8c2dc in c10::Dispatcher::OperatorDef::~OperatorDef (this=0xadb970, __in_chrg=<optimized out>) at ../aten/src/ATen/core/dispatch/Dispatcher.h:66
#16 0x00007fffe4a8c300 in __gnu_cxx::new_allocator<std::_List_node<c10::Dispatcher::OperatorDef> >::destroy<c10::Dispatcher::OperatorDef> (this=0x7ffff48df7c0 <c10::Dispatcher::realSingleton()::_singleton>, __p=0xadb970) at /usr/include/c++/9/ext/new_allocator.h:152
#17 0x00007fffe4a8a693 in std::allocator_traits<std::allocator<std::_List_node<c10::Dispatcher::OperatorDef> > >::destroy<c10::Dispatcher::OperatorDef> (__a=..., __p=0xadb970) at /usr/include/c++/9/bits/alloc_traits.h:496
#18 0x00007fffe4a880c4 in std::_List_base<c10::Dispatcher::OperatorDef, std::allocator<c10::Dispatcher::OperatorDef> >::_M_clear (this=0x7ffff48df7c0 <c10::Dispatcher::realSingleton()::_singleton>) at /usr/include/c++/9/bits/list.tcc:77
#19 0x00007fffe4a85bbc in std::_List_base<c10::Dispatcher::OperatorDef, std::allocator<c10::Dispatcher::OperatorDef> >::~_List_base (this=0x7ffff48df7c0 <c10::Dispatcher::realSingleton()::_singleton>, __in_chrg=<optimized out>) at /usr/include/c++/9/bits/stl_list.h:495
#20 0x00007fffe4a847b0 in std::list<c10::Dispatcher::OperatorDef, std::allocator<c10::Dispatcher::OperatorDef> >::~list (this=0x7ffff48df7c0 <c10::Dispatcher::realSingleton()::_singleton>, __in_chrg=<optimized out>) at /usr/include/c++/9/bits/stl_list.h:823
#21 0x00007fffe4a7e370 in c10::Dispatcher::~Dispatcher (this=0x7ffff48df7c0 <c10::Dispatcher::realSingleton()::_singleton>, __in_chrg=<optimized out>) at ../aten/src/ATen/core/dispatch/Dispatcher.h:61
#22 0x00007ffff7dfe8a7 in __run_exit_handlers (status=0, listp=0x7ffff7fa4718 <__exit_funcs>, run_list_atexit=run_list_atexit@entry=true, run_dtors=run_dtors@entry=true) at exit.c:108
#23 0x00007ffff7dfea60 in __GI_exit (status=<optimized out>) at exit.c:139
#24 0x00007ffff7ddc08a in __libc_start_main (main=0x4efd60 <main>, argc=2, argv=0x7fffffffe308, init=<optimized out>, fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7fffffffe2f8) at ../csu/libc-start.c:342
#25 0x00000000005fc5fe in _start ()
```
Used compiler: gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
Used python version 3.8.10

### Versions

Collecting environment information...
PyTorch version: 2.0.1
Is debug build: True
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.4 LTS (x86_64)
GCC version: (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
Clang version: 10.0.0-4ubuntu1
CMake version: version 3.18.4
Libc version: glibc-2.31

Python version: 3.8.10 (default, Jun 22 2022, 20:18:18)  [GCC 9.4.0] (64-bit runtime)
Python platform: Linux-5.4.0-121-generic-x86_64-with-glibc2.29
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
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
Address sizes:                   43 bits physical, 48 bits virtual
CPU(s):                          256
On-line CPU(s) list:             0-255
Thread(s) per core:              2
Core(s) per socket:              64
Socket(s):                       2
NUMA node(s):                    8
Vendor ID:                       AuthenticAMD
CPU family:                      23
Model:                           49
Model name:                      AMD EPYC 7742 64-Core Processor
Stepping:                        0
CPU MHz:                         3079.007
BogoMIPS:                        4491.47
Virtualization:                  AMD-V
L1d cache:                       4 MiB
L1i cache:                       4 MiB
L2 cache:                        64 MiB
L3 cache:                        512 MiB
NUMA node0 CPU(s):               0-15,128-143
NUMA node1 CPU(s):               16-31,144-159
NUMA node2 CPU(s):               32-47,160-175
NUMA node3 CPU(s):               48-63,176-191
NUMA node4 CPU(s):               64-79,192-207
NUMA node5 CPU(s):               80-95,208-223
NUMA node6 CPU(s):               96-111,224-239
NUMA node7 CPU(s):               112-127,240-255
Vulnerability Itlb multihit:     Not affected
Vulnerability L1tf:              Not affected
Vulnerability Mds:               Not affected
Vulnerability Meltdown:          Not affected
Vulnerability Mmio stale data:   Not affected
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl and seccomp
Vulnerability Spectre v1:        Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:        Mitigation; Retpolines, IBPB conditional, IBRS_FW, STIBP conditional, RSB filling
Vulnerability Srbds:             Not affected
Vulnerability Tsx async abort:   Not affected
Flags:                           fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc cpuid extd_apicid aperfmperf pni pclmulqdq monitor ssse3 fma cx16 sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb cat_l3 cdp_l3 hw_pstate sme ssbd mba sev ibrs ibpb stibp vmmcall fsgsbase bmi1 avx2 smep bmi2 cqm rdt_a rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local clzero irperf xsaveerptr wbnoinvd arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif umip rdpid overflow_recov succor smca

Versions of relevant libraries:
[pip3] torch==2.0.1
[conda] Could not collect
pure virtual method called
terminate called without an active exception
Aborted

cc @albanD