pytorch/torch/csrc/jit/python
Xuehai Pan ace6decc99 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, https://github.com/malfet
2024-07-25 05:53:09 +00:00
..
init.cpp Add debug repr to SymNode (#129925) 2024-07-12 18:31:23 +00:00
init.h
module_python.h Fix static py::object dangling pointer with py::gil_safe_call_once_and_store (#130341) 2024-07-25 05:53:09 +00:00
pybind_utils.cpp [1/N] Change #include <c10/util/Optional.h> to #include <optional> (#128301) 2024-07-08 07:03:53 +00:00
pybind_utils.h [1/N] Change #include <c10/util/Optional.h> to #include <optional> (#128301) 2024-07-08 07:03:53 +00:00
pybind.h
python_arg_flatten.cpp
python_arg_flatten.h
python_custom_class.cpp [Clang Tidy] Fix misc-header-include-cycle errors in clang-tidy and ignore some files (#127233) 2024-06-10 23:49:58 +00:00
python_custom_class.h [Clang Tidy] Fix misc-header-include-cycle errors in clang-tidy and ignore some files (#127233) 2024-06-10 23:49:58 +00:00
python_dict.cpp
python_dict.h
python_interpreter.cpp
python_ir.cpp [1/N] Change #include <c10/util/Optional.h> to #include <optional> (#128301) 2024-07-08 07:03:53 +00:00
python_ir.h
python_ivalue.h Fix static py::object dangling pointer with py::gil_safe_call_once_and_store (#130341) 2024-07-25 05:53:09 +00:00
python_list.cpp
python_list.h [1/N] Change #include <c10/util/Optional.h> to #include <optional> (#128301) 2024-07-08 07:03:53 +00:00
python_sugared_value.cpp [1/N] Change #include <c10/util/Optional.h> to #include <optional> (#128301) 2024-07-08 07:03:53 +00:00
python_sugared_value.h [1/N] Change #include <c10/util/Optional.h> to #include <optional> (#128301) 2024-07-08 07:03:53 +00:00
python_tracer.cpp
python_tracer.h
python_tree_views.cpp [1/N] Change #include <c10/util/Optional.h> to #include <optional> (#128301) 2024-07-08 07:03:53 +00:00
python_tree_views.h
script_init.cpp [2/N] Change #include <c10/util/Optional.h> to #include <optional> (#130236) 2024-07-09 03:17:24 +00:00
script_init.h
update_graph_executor_opt.cpp
update_graph_executor_opt.h
utf8_decoding_ignore.cpp
utf8_decoding_ignore.h