pytorch/torch/csrc/utils/object_ptr.cpp
albanD cf012c43f4 Do not call decref if python runtime is already dead (#106334)
Same treatment as many other objects such as https://github.com/pytorch/pytorch/blob/main/torch/csrc/autograd/python_hook.cpp#L99
This one can outlive the python runtime due to structs like: 2f35715f0d/torch/csrc/autograd/python_cpp_function.cpp (L232)

With the pybind patch and this one, the 3.12 build at https://github.com/pytorch/pytorch/pull/106083 stops segfaulting and runs test_autograd.py just fine.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/106334
Approved by: https://github.com/ezyang
2023-08-01 17:22:42 +00:00

29 lines
592 B
C++

#include <c10/macros/Macros.h>
#include <torch/csrc/utils/object_ptr.h>
#include <torch/csrc/python_headers.h>
template <>
void THPPointer<PyObject>::free() {
if (ptr && C10_LIKELY(Py_IsInitialized()))
Py_DECREF(ptr);
}
template class THPPointer<PyObject>;
template <>
void THPPointer<PyCodeObject>::free() {
if (ptr && C10_LIKELY(Py_IsInitialized()))
Py_DECREF(ptr);
}
template class THPPointer<PyCodeObject>;
template <>
void THPPointer<PyFrameObject>::free() {
if (ptr && C10_LIKELY(Py_IsInitialized()))
Py_DECREF(ptr);
}
template class THPPointer<PyFrameObject>;