pytorch/torch/csrc/utils/python_symnode.cpp
Xuehai Pan a17d1e5322 Fix static py::object dangling pointer with py::gil_safe_call_once_and_store (#130341)
Fix static `py::object`s with `py::gil_safe_call_once_and_store`.

The following code will leak a `py::object` which will call its destructor when shutdown the program. The destructor will call `Py_DECREF(obj.m_ptr)` which may raise a segmentation fault.

```c++
void func() {
    static py::object obj = py::module_::import("foo").attr("bar");

    ...
}
```

The correct code is to use raw pointers rather than the instance.

```c++
void func() {
    static py::object* obj_ptr = new py::object{py::module_::import("foo").attr("bar")};
    py::object obj = *obj_ptr;

    ...
}
```

This PR uses the `py::gil_safe_call_once_and_store` function from `pybind11`, which can run arbitrary initialization code only once under the Python GIL thread safely.

```c++
void func() {
    PYBIND11_CONSTINIT static py::gil_safe_call_once_and_store<py::object> storage;
    py::object obj = storage
                         .call_once_and_store_result(
                             []() -> py::object {
                                 return py::module_::import("foo").attr("bar");
                             }
                         )
                         .get_stored();

    ...
}
```

Pull Request resolved: https://github.com/pytorch/pytorch/pull/130341
Approved by: https://github.com/ezyang
2024-07-10 04:23:37 +00:00

36 lines
944 B
C++

#include <torch/csrc/utils/python_symnode.h>
namespace torch {
py::handle get_symint_class() {
PYBIND11_CONSTINIT static py::gil_safe_call_once_and_store<py::object>
storage;
return storage
.call_once_and_store_result([]() -> py::object {
return py::module::import("torch").attr("SymInt");
})
.get_stored();
}
py::handle get_symfloat_class() {
PYBIND11_CONSTINIT static py::gil_safe_call_once_and_store<py::object>
storage;
return storage
.call_once_and_store_result([]() -> py::object {
return py::module::import("torch").attr("SymFloat");
})
.get_stored();
}
py::handle get_symbool_class() {
PYBIND11_CONSTINIT static py::gil_safe_call_once_and_store<py::object>
storage;
return storage
.call_once_and_store_result([]() -> py::object {
return py::module::import("torch").attr("SymBool");
})
.get_stored();
}
} // namespace torch