-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
) * Backport of google/pybind11clif#30088 (main PR) and google/pybind11clif#30092 (minor fixes). Note for completeness: These are identical to the current versions on the pybind11clif main branch (@ commit 4841661df5daf26ecdedaace54e64d0782e63f64): * test_class_release_gil_before_calling_cpp_dtor.cpp * test_class_release_gil_before_calling_cpp_dtor.py * Fix potential data race in test_class_release_gil_before_calling_cpp_dtor.cpp The original intent was to let the singleton leak, but making that tread-safe is slightly more involved than this solution. It's totally fine in this case if the RegistryType destructor runs on process teardown. See also: #5522 (comment) --------- Co-authored-by: Ralf W. Grosse-Kunstleve <[email protected]>
- Loading branch information
Showing
5 changed files
with
132 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include <pybind11/pybind11.h> | ||
|
||
#include "pybind11_tests.h" | ||
|
||
#include <string> | ||
#include <unordered_map> | ||
|
||
namespace pybind11_tests { | ||
namespace class_release_gil_before_calling_cpp_dtor { | ||
|
||
using RegistryType = std::unordered_map<std::string, int>; | ||
|
||
static RegistryType &PyGILState_Check_Results() { | ||
static RegistryType singleton; // Local static variables have thread-safe initialization. | ||
return singleton; | ||
} | ||
|
||
template <int> // Using int as a trick to easily generate a series of types. | ||
struct ProbeType { | ||
private: | ||
std::string unique_key; | ||
|
||
public: | ||
explicit ProbeType(const std::string &unique_key) : unique_key{unique_key} {} | ||
|
||
~ProbeType() { | ||
RegistryType ® = PyGILState_Check_Results(); | ||
assert(reg.count(unique_key) == 0); | ||
reg[unique_key] = PyGILState_Check(); | ||
} | ||
}; | ||
|
||
} // namespace class_release_gil_before_calling_cpp_dtor | ||
} // namespace pybind11_tests | ||
|
||
TEST_SUBMODULE(class_release_gil_before_calling_cpp_dtor, m) { | ||
using namespace pybind11_tests::class_release_gil_before_calling_cpp_dtor; | ||
|
||
py::class_<ProbeType<0>>(m, "ProbeType0").def(py::init<std::string>()); | ||
|
||
py::class_<ProbeType<1>>(m, "ProbeType1", py::release_gil_before_calling_cpp_dtor()) | ||
.def(py::init<std::string>()); | ||
|
||
m.def("PopPyGILState_Check_Result", [](const std::string &unique_key) -> std::string { | ||
RegistryType ® = PyGILState_Check_Results(); | ||
if (reg.count(unique_key) == 0) { | ||
return "MISSING"; | ||
} | ||
int res = reg[unique_key]; | ||
reg.erase(unique_key); | ||
return std::to_string(res); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from __future__ import annotations | ||
|
||
import gc | ||
|
||
import pytest | ||
|
||
from pybind11_tests import class_release_gil_before_calling_cpp_dtor as m | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("probe_type", "unique_key", "expected_result"), | ||
[ | ||
(m.ProbeType0, "without_manipulating_gil", "1"), | ||
(m.ProbeType1, "release_gil_before_calling_cpp_dtor", "0"), | ||
], | ||
) | ||
def test_gil_state_check_results(probe_type, unique_key, expected_result): | ||
probe_type(unique_key) | ||
gc.collect() | ||
result = m.PopPyGILState_Check_Result(unique_key) | ||
assert result == expected_result |