mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-07 00:21:07 +01:00
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, https://github.com/malfet
36 lines
944 B
C++
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
|