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
51 lines
1.5 KiB
C++
51 lines
1.5 KiB
C++
#pragma once
|
|
#include <pybind11/pybind11.h>
|
|
#include <pybind11/stl.h>
|
|
#include <torch/csrc/jit/api/module.h>
|
|
#include <torch/csrc/utils/pybind.h>
|
|
#include <tuple>
|
|
|
|
namespace py = pybind11;
|
|
|
|
namespace torch::jit {
|
|
|
|
inline std::optional<Module> as_module(py::handle obj) {
|
|
PYBIND11_CONSTINIT static py::gil_safe_call_once_and_store<py::object>
|
|
storage;
|
|
auto& ScriptModule =
|
|
storage
|
|
.call_once_and_store_result([]() -> py::object {
|
|
return py::module_::import("torch.jit").attr("ScriptModule");
|
|
})
|
|
.get_stored();
|
|
if (py::isinstance(obj, ScriptModule)) {
|
|
return py::cast<Module>(obj.attr("_c"));
|
|
}
|
|
return std::nullopt;
|
|
}
|
|
|
|
inline std::optional<Object> as_object(py::handle obj) {
|
|
PYBIND11_CONSTINIT static py::gil_safe_call_once_and_store<
|
|
std::tuple<py::object, py::object>>
|
|
storage;
|
|
auto& [ScriptObject, RecursiveScriptClass] =
|
|
storage
|
|
.call_once_and_store_result(
|
|
[]() -> std::tuple<py::object, py::object> {
|
|
return {
|
|
py::module_::import("torch").attr("ScriptObject"),
|
|
py::module_::import("torch.jit")
|
|
.attr("RecursiveScriptClass")};
|
|
})
|
|
.get_stored();
|
|
if (py::isinstance(obj, ScriptObject)) {
|
|
return py::cast<Object>(obj);
|
|
}
|
|
if (py::isinstance(obj, RecursiveScriptClass)) {
|
|
return py::cast<Object>(obj.attr("_c"));
|
|
}
|
|
return std::nullopt;
|
|
}
|
|
|
|
} // namespace torch::jit
|