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: https://github.com/pytorch/pytorch/blob/2f35715f0d230f8d5527bdf01b9793f6613f3a2e/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