mirror of
https://github.com/pybind/pybind11.git
synced 2026-03-14 20:27:47 +00:00
* Add new argument to `gil_safe_call_once_and_store::call_once_and_store_result` * Add per-interpreter storage for `gil_safe_call_once_and_store` * Make `~gil_safe_call_once_and_store` a no-op * Fix C++11 compatibility * Improve thread-safety and add default finalizer * Try fix thread-safety * Try fix thread-safety * Add a warning comment * Simplify `PYBIND11_INTERNALS_VERSION >= 12` * Try fix thread-safety * Try fix thread-safety * Revert get_pp() * Update comments * Move call-once storage out of internals * Revert internal version bump * Cleanup outdated comments * Move atomic_bool alias into pybind11::detail namespace The `using atomic_bool = ...` declaration was at global scope, polluting the global namespace. Move it into pybind11::detail to avoid potential conflicts with user code. * Add explicit #include <unordered_map> for subinterpreter support The subinterpreter branch uses std::unordered_map but relied on transitive includes. Add an explicit include for robustness. * Remove extraneous semicolon after destructor definition Style fix: remove trailing semicolon after ~call_once_storage() destructor body. * Add comment explaining unused finalize parameter Clarify why the finalize callback parameter is intentionally ignored when subinterpreter support is disabled: the storage is process-global and leaked to avoid destructor calls after interpreter finalization. * Add comment explaining error_scope usage Clarify why error_scope is used: to preserve any existing Python error state that might be cleared or modified by dict_getitemstringref. * Improve exception safety in get_or_create_call_once_storage_map() Use std::unique_ptr to hold the newly allocated storage map until the capsule is successfully created. This prevents a memory leak if capsule creation throws an exception. * Add timeout-minutes: 3 to cpptest workflow steps Add a 3-minute timeout to all C++ test (cpptest) steps across all platforms to detect hangs early. This uses GitHub Actions' built-in timeout-minutes property which works on Linux, macOS, and Windows. * Add progress reporter for test_with_catch Catch2 runner Add a custom Catch2 streaming reporter that prints one line per test case as it starts and ends, with immediate flushing to keep CI logs current. This makes it easy to see where the embedded/interpreter tests are spending time and to pinpoint which test case is stuck when builds hang (e.g., free-threading issues). The reporter: - Prints "[ RUN ]" when each test starts - Prints "[ OK ]" or "[ FAILED ]" when each test ends - Prints the Python version once at the start via Py_GetVersion() - Uses StreamingReporterBase for immediate output (not buffered) - Is set as the default reporter via CATCH_CONFIG_DEFAULT_REPORTER This approach gives visibility into all tests without changing their behavior, turning otherwise opaque 90-minute CI timeouts into locatable issues in the Catch output. * clang-format auto-fix (overlooked before) * Disable "Move Subinterpreter" test on free-threaded Python 3.14+ This test hangs in Py_EndInterpreter() when the subinterpreter is destroyed from a different thread than it was created on. The hang was observed: - Intermittently on macOS with Python 3.14.0t - Predictably on macOS, Ubuntu, and Windows with Python 3.14.1t and 3.14.2t Root cause analysis points to an interaction between pybind11's subinterpreter creation code and CPython's free-threaded runtime, specifically around PyThreadState_Swap() after PyThreadState_DeleteCurrent(). See detailed analysis: https://github.com/pybind/pybind11/pull/5933 * style: pre-commit fixes * Add test for gil_safe_call_once_and_store per-interpreter isolation This test verifies that gil_safe_call_once_and_store provides separate storage for each interpreter when subinterpreter support is enabled. The test caches the interpreter ID in the main interpreter, then creates a subinterpreter and verifies it gets its own cached value (not the main interpreter's). Without per-interpreter storage, the subinterpreter would incorrectly see the main interpreter's cached object. * Add STARTING/DONE timestamps to test_with_catch output Print UTC timestamps at the beginning and end of the test run to make it immediately clear when tests started and whether they ran to completion. The DONE message includes the Catch session result value. Example output: [ STARTING ] 2025-12-21 03:23:20.497Z [ PYTHON ] 3.14.2 ... [ RUN ] Threads [ OK ] Threads [ DONE ] 2025-12-21 03:23:20.512Z (result 0) * Disable stdout buffering in test_with_catch Ensure test output appears immediately in CI logs by disabling stdout buffering. Without this, output may be lost if the process is killed by a timeout, making it difficult to diagnose which test was hanging. * EXPERIMENT: Re-enable hanging test to verify CI log buffering fix This is a temporary commit to verify that the unbuffered stdout fix makes the hanging test visible in CI logs. REVERT THIS COMMIT after confirming the output appears. * Revert "Disable stdout buffering in test_with_catch" This reverts commit0f8f32a92a. * Use USES_TERMINAL for cpptest to show output immediately Ninja buffers subprocess output until completion. When a test hangs, the output is never shown, making it impossible to diagnose which test is hanging. USES_TERMINAL gives the command direct terminal access, bypassing ninja's buffering. This explains why Windows CI showed test progress but Linux/macOS did not - Windows uses MSBuild which doesn't buffer the same way. * Fix clang-tidy performance-avoid-endl warning Use '\n' instead of std::endl since USES_TERMINAL now handles output buffering at the CMake level. * Add SIGTERM handler to show when test is killed by timeout When a test hangs and is killed by `timeout`, Catch2 marks it as failed but the process exits before printing [ DONE ]. This made it unclear whether the test failed normally or was terminated. The signal handler prints a clear message when SIGTERM is received, making timeout-related failures obvious in CI logs. * Fix typo: atleast -> at_least * Fix GCC warn_unused_result error for write() in signal handler Assign the return value to a variable to satisfy GCC's warn_unused_result attribute, then cast to void to suppress unused variable warning. * Add USES_TERMINAL to other C++ test targets Apply the same ninja output buffering fix to test_cross_module_rtti and test_pure_cpp targets. Also add explanatory comments to all USES_TERMINAL usages. * Revert "EXPERIMENT: Re-enable hanging test to verify CI log buffering fix" This reverts commita3abdeea89. * Update comment to reference PR #5940 for Move Subinterpreter fix * Add alias `interpid_t = std::int64_t` * Add isolation and gc test for `gil_safe_call_once_and_store` * Add thread local cache for gil_safe_call_once_and_store * Revert "Add thread local cache for gil_safe_call_once_and_store" This reverts commit 5d6681956d2d326fe74c7bf80e845c8e8ddb2a7c. * Revert changes according to code review * Relocate multiple-interpreters tests * Add more tests for multiple interpreters * Remove copy constructor * Apply suggestions from code review * Refactor to use per-storage capsule instead * Update comments * Update singleton tests * Use interpreter id type for `get_num_interpreters_seen()` * Suppress unused variable warning * HACKING * Revert "HACKING" This reverts commit534235ea55. * Try fix concurrency * Test even harder * Reorg code to avoid duplicates * Fix unique_ptr::reset -> unique_ptr::release * Extract reusable functions * Fix indentation * Appease warnings for MSVC * Appease warnings for MSVC * Appease warnings for MSVC * Try fix concurrency by not using `get_num_interpreters_seen() > 1` * Try fix tests * Make Python path handling more robust * Update comments and assertion messages * Revert changes according to code review * Disable flaky tests * Use `@pytest.mark.xfail` rather than `pytest.skip` * Retrigger CI * Retrigger CI * Revert file moves * Refactor atomic_get_or_create_in_state_dict: improve API and fix on_fetch_ bug Three improvements to atomic_get_or_create_in_state_dict: 1. Return std::pair<Payload*, bool> instead of just Payload* - The bool indicates whether storage was newly created (true) or already existed (false), following std::map::insert convention. - This fixes a bug where on_fetch_ was called even for newly created internals, when it should only run for fetched (existing) ones. (Identified by @b-pass in code review) 2. Change LeakOnInterpreterShutdown from template param to runtime arg - Renamed to `clear_destructor` to describe what it does locally, rather than embedding assumptions about why it's used. - Reduces template instantiations (header-only library benefits). - The check is in the slow path (create) anyway, so negligible cost. 3. Remove unnecessary braces around the fast-path lookup - The braces created a nested scope but declared no local variables that would benefit from scoping. * Remove unused PYBIND11_MULTIPLE_INTERPRETERS_TEST_FILES variable This variable was defined but never used. --------- Co-authored-by: Ralf W. Grosse-Kunstleve <rgrossekunst@nvidia.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
574 lines
21 KiB
C++
574 lines
21 KiB
C++
#include <pybind11/embed.h>
|
|
#ifdef PYBIND11_HAS_SUBINTERPRETER_SUPPORT
|
|
# include <pybind11/gil_safe_call_once.h>
|
|
# include <pybind11/subinterpreter.h>
|
|
|
|
// Silence MSVC C++17 deprecation warning from Catch regarding std::uncaught_exceptions (up to
|
|
// catch 2.0.1; this should be fixed in the next catch release after 2.0.1).
|
|
PYBIND11_WARNING_DISABLE_MSVC(4996)
|
|
|
|
# include "catch_skip.h"
|
|
|
|
# include <catch.hpp>
|
|
# include <cstdlib>
|
|
# include <fstream>
|
|
# include <functional>
|
|
# include <thread>
|
|
# include <utility>
|
|
|
|
namespace py = pybind11;
|
|
using namespace py::literals;
|
|
|
|
bool has_state_dict_internals_obj();
|
|
uintptr_t get_details_as_uintptr();
|
|
|
|
void unsafe_reset_internals_for_single_interpreter() {
|
|
// NOTE: This code is NOT SAFE unless the caller guarantees no other threads are alive
|
|
// NOTE: This code is tied to the precise implementation of the internals holder
|
|
|
|
// first, unref the thread local internals
|
|
py::detail::get_internals_pp_manager().unref();
|
|
py::detail::get_local_internals_pp_manager().unref();
|
|
|
|
// we know there are no other interpreters, so we can lower this. SUPER DANGEROUS
|
|
py::detail::get_num_interpreters_seen() = 1;
|
|
|
|
// now we unref the static global singleton internals
|
|
py::detail::get_internals_pp_manager().unref();
|
|
py::detail::get_local_internals_pp_manager().unref();
|
|
|
|
// finally, we reload the static global singleton
|
|
py::detail::get_internals();
|
|
py::detail::get_local_internals();
|
|
}
|
|
|
|
py::object &get_dict_type_object() {
|
|
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("builtins").attr("dict"); })
|
|
.get_stored();
|
|
}
|
|
|
|
py::object &get_ordered_dict_type_object() {
|
|
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("collections").attr("OrderedDict"); })
|
|
.get_stored();
|
|
}
|
|
|
|
py::object &get_default_dict_type_object() {
|
|
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("collections").attr("defaultdict"); })
|
|
.get_stored();
|
|
}
|
|
|
|
TEST_CASE("Single Subinterpreter") {
|
|
unsafe_reset_internals_for_single_interpreter();
|
|
|
|
py::module_::import("external_module"); // in the main interpreter
|
|
|
|
// Add tags to the modules in the main interpreter and test the basics.
|
|
py::module_::import("__main__").attr("main_tag") = "main interpreter";
|
|
{
|
|
auto m = py::module_::import("widget_module");
|
|
m.attr("extension_module_tag") = "added to module in main interpreter";
|
|
|
|
REQUIRE(m.attr("add")(1, 2).cast<int>() == 3);
|
|
}
|
|
REQUIRE(has_state_dict_internals_obj());
|
|
|
|
auto main_int
|
|
= py::module_::import("external_module").attr("internals_at")().cast<uintptr_t>();
|
|
|
|
/// Create and switch to a subinterpreter.
|
|
{
|
|
py::scoped_subinterpreter ssi;
|
|
|
|
// The subinterpreter has internals populated
|
|
REQUIRE(has_state_dict_internals_obj());
|
|
|
|
py::list(py::module_::import("sys").attr("path")).append(py::str("."));
|
|
|
|
auto ext_int
|
|
= py::module_::import("external_module").attr("internals_at")().cast<uintptr_t>();
|
|
py::detail::get_internals();
|
|
REQUIRE(get_details_as_uintptr() == ext_int);
|
|
REQUIRE(ext_int != main_int);
|
|
|
|
// Modules tags should be gone.
|
|
REQUIRE_FALSE(py::hasattr(py::module_::import("__main__"), "tag"));
|
|
{
|
|
auto m = py::module_::import("widget_module");
|
|
REQUIRE_FALSE(py::hasattr(m, "extension_module_tag"));
|
|
|
|
// Function bindings should still work.
|
|
REQUIRE(m.attr("add")(1, 2).cast<int>() == 3);
|
|
}
|
|
}
|
|
|
|
REQUIRE(py::hasattr(py::module_::import("__main__"), "main_tag"));
|
|
REQUIRE(py::hasattr(py::module_::import("widget_module"), "extension_module_tag"));
|
|
REQUIRE(has_state_dict_internals_obj());
|
|
|
|
unsafe_reset_internals_for_single_interpreter();
|
|
}
|
|
|
|
# if PY_VERSION_HEX >= 0x030D0000
|
|
TEST_CASE("Move Subinterpreter") {
|
|
std::unique_ptr<py::subinterpreter> sub(new py::subinterpreter(py::subinterpreter::create()));
|
|
|
|
// on this thread, use the subinterpreter and import some non-trivial junk
|
|
{
|
|
py::subinterpreter_scoped_activate activate(*sub);
|
|
|
|
py::list(py::module_::import("sys").attr("path")).append(py::str("."));
|
|
py::module_::import("datetime");
|
|
py::module_::import("threading");
|
|
py::module_::import("external_module");
|
|
}
|
|
|
|
auto t = std::thread([&]() {
|
|
// Use it again
|
|
{
|
|
py::subinterpreter_scoped_activate activate(*sub);
|
|
py::module_::import("external_module");
|
|
}
|
|
sub.reset();
|
|
});
|
|
|
|
// on 3.14.1+ destructing a sub-interpreter does a stop-the-world. we need to detach our
|
|
// thread state in order for that to be possible.
|
|
{
|
|
py::gil_scoped_release nogil;
|
|
t.join();
|
|
}
|
|
|
|
REQUIRE(!sub);
|
|
|
|
unsafe_reset_internals_for_single_interpreter();
|
|
}
|
|
# endif
|
|
|
|
TEST_CASE("GIL Subinterpreter") {
|
|
|
|
PyInterpreterState *main_interp = PyInterpreterState_Get();
|
|
|
|
{
|
|
auto sub = py::subinterpreter::create();
|
|
|
|
REQUIRE(main_interp == PyInterpreterState_Get());
|
|
|
|
PyInterpreterState *sub_interp = nullptr;
|
|
|
|
{
|
|
py::subinterpreter_scoped_activate activate(sub);
|
|
|
|
sub_interp = PyInterpreterState_Get();
|
|
REQUIRE(sub_interp != main_interp);
|
|
|
|
py::list(py::module_::import("sys").attr("path")).append(py::str("."));
|
|
py::module_::import("datetime");
|
|
py::module_::import("threading");
|
|
py::module_::import("external_module");
|
|
|
|
{
|
|
py::subinterpreter_scoped_activate main(py::subinterpreter::main());
|
|
REQUIRE(PyInterpreterState_Get() == main_interp);
|
|
|
|
{
|
|
py::gil_scoped_release nogil{};
|
|
{
|
|
py::gil_scoped_acquire yesgil{};
|
|
REQUIRE(PyInterpreterState_Get() == main_interp);
|
|
}
|
|
}
|
|
|
|
REQUIRE(PyInterpreterState_Get() == main_interp);
|
|
}
|
|
|
|
REQUIRE(PyInterpreterState_Get() == sub_interp);
|
|
|
|
{
|
|
py::gil_scoped_release nogil{};
|
|
{
|
|
py::gil_scoped_acquire yesgil{};
|
|
REQUIRE(PyInterpreterState_Get() == sub_interp);
|
|
}
|
|
}
|
|
|
|
REQUIRE(PyInterpreterState_Get() == sub_interp);
|
|
}
|
|
|
|
REQUIRE(PyInterpreterState_Get() == main_interp);
|
|
|
|
{
|
|
py::gil_scoped_release nogil{};
|
|
{
|
|
py::gil_scoped_acquire yesgil{};
|
|
REQUIRE(PyInterpreterState_Get() == main_interp);
|
|
}
|
|
}
|
|
|
|
REQUIRE(PyInterpreterState_Get() == main_interp);
|
|
|
|
bool thread_result;
|
|
|
|
{
|
|
thread_result = false;
|
|
py::gil_scoped_release nogil{};
|
|
std::thread([&]() {
|
|
{
|
|
py::subinterpreter_scoped_activate ssa{sub};
|
|
}
|
|
{
|
|
py::gil_scoped_acquire gil{};
|
|
thread_result = (PyInterpreterState_Get() == main_interp);
|
|
}
|
|
}).join();
|
|
}
|
|
REQUIRE(thread_result);
|
|
|
|
{
|
|
thread_result = false;
|
|
py::gil_scoped_release nogil{};
|
|
std::thread([&]() {
|
|
py::gil_scoped_acquire gil{};
|
|
thread_result = (PyInterpreterState_Get() == main_interp);
|
|
}).join();
|
|
}
|
|
REQUIRE(thread_result);
|
|
}
|
|
|
|
REQUIRE(PyInterpreterState_Get() == main_interp);
|
|
unsafe_reset_internals_for_single_interpreter();
|
|
}
|
|
|
|
TEST_CASE("Multiple Subinterpreters") {
|
|
unsafe_reset_internals_for_single_interpreter();
|
|
|
|
// Make sure the module is in the main interpreter and save its pointer
|
|
auto *main_ext = py::module_::import("external_module").ptr();
|
|
auto main_int
|
|
= py::module_::import("external_module").attr("internals_at")().cast<uintptr_t>();
|
|
py::module_::import("external_module").attr("multi_interp") = "1";
|
|
|
|
{
|
|
py::subinterpreter si1 = py::subinterpreter::create();
|
|
std::unique_ptr<py::subinterpreter> psi2;
|
|
|
|
PyObject *sub1_ext = nullptr;
|
|
PyObject *sub2_ext = nullptr;
|
|
uintptr_t sub1_int = 0;
|
|
uintptr_t sub2_int = 0;
|
|
|
|
{
|
|
py::subinterpreter_scoped_activate scoped(si1);
|
|
py::list(py::module_::import("sys").attr("path")).append(py::str("."));
|
|
|
|
// The subinterpreter has its own copy of this module which is completely separate from
|
|
// main
|
|
sub1_ext = py::module_::import("external_module").ptr();
|
|
REQUIRE(sub1_ext != main_ext);
|
|
REQUIRE_FALSE(py::hasattr(py::module_::import("external_module"), "multi_interp"));
|
|
py::module_::import("external_module").attr("multi_interp") = "2";
|
|
// The subinterpreter also has its own internals
|
|
sub1_int
|
|
= py::module_::import("external_module").attr("internals_at")().cast<uintptr_t>();
|
|
REQUIRE(sub1_int != main_int);
|
|
|
|
// while the old one is active, create a new one
|
|
psi2.reset(new py::subinterpreter(py::subinterpreter::create()));
|
|
}
|
|
|
|
{
|
|
py::subinterpreter_scoped_activate scoped(*psi2);
|
|
py::list(py::module_::import("sys").attr("path")).append(py::str("."));
|
|
|
|
// The second subinterpreter is separate from both main and the other subinterpreter
|
|
sub2_ext = py::module_::import("external_module").ptr();
|
|
REQUIRE(sub2_ext != main_ext);
|
|
REQUIRE(sub2_ext != sub1_ext);
|
|
REQUIRE_FALSE(py::hasattr(py::module_::import("external_module"), "multi_interp"));
|
|
py::module_::import("external_module").attr("multi_interp") = "3";
|
|
// The subinterpreter also has its own internals
|
|
sub2_int
|
|
= py::module_::import("external_module").attr("internals_at")().cast<uintptr_t>();
|
|
REQUIRE(sub2_int != main_int);
|
|
REQUIRE(sub2_int != sub1_int);
|
|
}
|
|
|
|
{
|
|
py::subinterpreter_scoped_activate scoped(si1);
|
|
REQUIRE(
|
|
py::cast<std::string>(py::module_::import("external_module").attr("multi_interp"))
|
|
== "2");
|
|
}
|
|
|
|
// out here we should be in the main interpreter, with the GIL, with the other 2 still
|
|
// alive
|
|
|
|
auto post_int
|
|
= py::module_::import("external_module").attr("internals_at")().cast<uintptr_t>();
|
|
// Make sure internals went back the way it was before
|
|
REQUIRE(main_int == post_int);
|
|
|
|
REQUIRE(py::cast<std::string>(py::module_::import("external_module").attr("multi_interp"))
|
|
== "1");
|
|
}
|
|
|
|
// now back to just main
|
|
|
|
auto post_int
|
|
= py::module_::import("external_module").attr("internals_at")().cast<uintptr_t>();
|
|
// Make sure internals went back the way it was before
|
|
REQUIRE(main_int == post_int);
|
|
|
|
REQUIRE(py::cast<std::string>(py::module_::import("external_module").attr("multi_interp"))
|
|
== "1");
|
|
|
|
unsafe_reset_internals_for_single_interpreter();
|
|
}
|
|
|
|
// Test that gil_safe_call_once_and_store provides per-interpreter storage.
|
|
// Without the per-interpreter storage fix, the subinterpreter would see the value
|
|
// cached by the main interpreter, which is invalid (different interpreter's object).
|
|
TEST_CASE("gil_safe_call_once_and_store per-interpreter isolation") {
|
|
unsafe_reset_internals_for_single_interpreter();
|
|
|
|
// This static simulates a typical usage pattern where a module caches
|
|
// an imported object using gil_safe_call_once_and_store.
|
|
PYBIND11_CONSTINIT static py::gil_safe_call_once_and_store<py::object> storage;
|
|
|
|
// Get the interpreter ID in the main interpreter
|
|
auto main_interp_id = PyInterpreterState_GetID(PyInterpreterState_Get());
|
|
|
|
// Store a value in the main interpreter - we'll store the interpreter ID as a Python int
|
|
auto &main_value = storage
|
|
.call_once_and_store_result([]() {
|
|
return py::int_(PyInterpreterState_GetID(PyInterpreterState_Get()));
|
|
})
|
|
.get_stored();
|
|
REQUIRE(main_value.cast<int64_t>() == main_interp_id);
|
|
|
|
py::object dict_type = get_dict_type_object();
|
|
py::object ordered_dict_type = get_ordered_dict_type_object();
|
|
py::object default_dict_type = get_default_dict_type_object();
|
|
|
|
int64_t sub_interp_id = -1;
|
|
int64_t sub_cached_value = -1;
|
|
|
|
bool sub_default_dict_type_destroyed = false;
|
|
|
|
// Create a subinterpreter and check that it gets its own storage
|
|
{
|
|
py::scoped_subinterpreter ssi;
|
|
|
|
sub_interp_id = PyInterpreterState_GetID(PyInterpreterState_Get());
|
|
REQUIRE(sub_interp_id != main_interp_id);
|
|
|
|
// Access the same static storage from the subinterpreter.
|
|
// With per-interpreter storage, this should call the lambda again
|
|
// and cache a NEW value for this interpreter.
|
|
// Without per-interpreter storage, this would return main's cached value.
|
|
auto &sub_value
|
|
= storage
|
|
.call_once_and_store_result([]() {
|
|
return py::int_(PyInterpreterState_GetID(PyInterpreterState_Get()));
|
|
})
|
|
.get_stored();
|
|
|
|
sub_cached_value = sub_value.cast<int64_t>();
|
|
|
|
// The cached value should be the SUBINTERPRETER's ID, not the main interpreter's.
|
|
// This would fail without per-interpreter storage.
|
|
REQUIRE(sub_cached_value == sub_interp_id);
|
|
REQUIRE(sub_cached_value != main_interp_id);
|
|
|
|
py::object sub_dict_type = get_dict_type_object();
|
|
py::object sub_ordered_dict_type = get_ordered_dict_type_object();
|
|
py::object sub_default_dict_type = get_default_dict_type_object();
|
|
|
|
// Verify that the subinterpreter has its own cached type objects.
|
|
// For static types, they should be the same object across interpreters.
|
|
// See also: https://docs.python.org/3/c-api/typeobj.html#static-types
|
|
REQUIRE(sub_dict_type.is(dict_type)); // dict is a static type
|
|
REQUIRE(sub_ordered_dict_type.is(ordered_dict_type)); // OrderedDict is a static type
|
|
// For heap types, they are dynamically created per-interpreter.
|
|
// See also: https://docs.python.org/3/c-api/typeobj.html#heap-types
|
|
REQUIRE_FALSE(sub_default_dict_type.is(default_dict_type)); // defaultdict is a heap type
|
|
|
|
// Set up a weakref callback to detect when the subinterpreter's cached default_dict_type
|
|
// is destroyed so the gil_safe_call_once_and_store storage is not leaked when the
|
|
// subinterpreter is shutdown.
|
|
(void) py::weakref(sub_default_dict_type,
|
|
py::cpp_function([&](py::handle weakref) -> void {
|
|
sub_default_dict_type_destroyed = true;
|
|
weakref.dec_ref();
|
|
}))
|
|
.release();
|
|
}
|
|
|
|
// Back in main interpreter, verify main's value is unchanged
|
|
auto &main_value_after = storage.get_stored();
|
|
REQUIRE(main_value_after.cast<int64_t>() == main_interp_id);
|
|
|
|
// Verify that the types cached in main are unchanged
|
|
py::object dict_type_after = get_dict_type_object();
|
|
py::object ordered_dict_type_after = get_ordered_dict_type_object();
|
|
py::object default_dict_type_after = get_default_dict_type_object();
|
|
REQUIRE(dict_type_after.is(dict_type));
|
|
REQUIRE(ordered_dict_type_after.is(ordered_dict_type));
|
|
REQUIRE(default_dict_type_after.is(default_dict_type));
|
|
|
|
// Verify that the subinterpreter's cached default_dict_type was destroyed
|
|
REQUIRE(sub_default_dict_type_destroyed);
|
|
|
|
unsafe_reset_internals_for_single_interpreter();
|
|
}
|
|
|
|
# ifdef Py_MOD_PER_INTERPRETER_GIL_SUPPORTED
|
|
TEST_CASE("Per-Subinterpreter GIL") {
|
|
auto main_int
|
|
= py::module_::import("external_module").attr("internals_at")().cast<uintptr_t>();
|
|
|
|
std::atomic<int> started, sync, failure;
|
|
started = 0;
|
|
sync = 0;
|
|
failure = 0;
|
|
|
|
// REQUIRE throws on failure, so we can't use it within the thread
|
|
# define T_REQUIRE(status) \
|
|
do { \
|
|
assert(status); \
|
|
if (!(status)) \
|
|
++failure; \
|
|
} while (0)
|
|
|
|
auto &&thread_main = [&](int num) {
|
|
while (started == 0)
|
|
std::this_thread::sleep_for(std::chrono::microseconds(1));
|
|
++started;
|
|
|
|
py::gil_scoped_acquire gil;
|
|
|
|
// we have the GIL, we can access the main interpreter
|
|
auto t_int
|
|
= py::module_::import("external_module").attr("internals_at")().cast<uintptr_t>();
|
|
T_REQUIRE(t_int == main_int);
|
|
py::module_::import("external_module").attr("multi_interp") = "1";
|
|
|
|
auto sub = py::subinterpreter::create();
|
|
|
|
{
|
|
py::subinterpreter_scoped_activate sguard{sub};
|
|
|
|
py::list(py::module_::import("sys").attr("path")).append(py::str("."));
|
|
|
|
// we have switched to the new interpreter and released the main gil
|
|
|
|
// trampoline_module did not provide the per_interpreter_gil tag, so it cannot be
|
|
// imported
|
|
bool caught = false;
|
|
try {
|
|
py::module_::import("trampoline_module");
|
|
} catch (pybind11::error_already_set &pe) {
|
|
T_REQUIRE(pe.matches(PyExc_ImportError));
|
|
std::string msg(pe.what());
|
|
T_REQUIRE(msg.find("does not support loading in subinterpreters")
|
|
!= std::string::npos);
|
|
caught = true;
|
|
}
|
|
T_REQUIRE(caught);
|
|
|
|
// widget_module did provide the per_interpreter_gil tag, so it this does not throw
|
|
try {
|
|
py::module_::import("widget_module");
|
|
caught = false;
|
|
} catch (pybind11::error_already_set &) {
|
|
caught = true;
|
|
}
|
|
T_REQUIRE(!caught);
|
|
|
|
// widget_module did provide the per_interpreter_gil tag, so it this does not throw
|
|
py::module_::import("widget_module");
|
|
|
|
T_REQUIRE(!py::hasattr(py::module_::import("external_module"), "multi_interp"));
|
|
py::module_::import("external_module").attr("multi_interp") = std::to_string(num);
|
|
|
|
// wait for something to set sync to our thread number
|
|
// we are holding our subinterpreter's GIL
|
|
while (sync != num)
|
|
std::this_thread::sleep_for(std::chrono::microseconds(1));
|
|
|
|
// now change it so the next thread can move on
|
|
++sync;
|
|
|
|
// but keep holding the GIL until after the next thread moves on as well
|
|
while (sync == num + 1)
|
|
std::this_thread::sleep_for(std::chrono::microseconds(1));
|
|
|
|
// one last check before quitting the thread, the internals should be different
|
|
auto sub_int
|
|
= py::module_::import("external_module").attr("internals_at")().cast<uintptr_t>();
|
|
T_REQUIRE(sub_int != main_int);
|
|
}
|
|
};
|
|
# undef T_REQUIRE
|
|
|
|
std::thread t1(thread_main, 1);
|
|
std::thread t2(thread_main, 2);
|
|
|
|
// we spawned two threads, at this point they are both waiting for started to increase
|
|
++started;
|
|
|
|
// ok now wait for the threads to start
|
|
while (started != 3)
|
|
std::this_thread::sleep_for(std::chrono::microseconds(1));
|
|
|
|
// we still hold the main GIL, at this point both threads are waiting on the main GIL
|
|
// IN THE CASE of free threading, the threads are waiting on sync (because there is no GIL)
|
|
|
|
// IF the below code hangs in one of the wait loops, then the child thread GIL behavior did not
|
|
// function as expected.
|
|
{
|
|
// release the GIL and allow the threads to run
|
|
py::gil_scoped_release nogil;
|
|
|
|
// the threads are now waiting on the sync
|
|
REQUIRE(sync == 0);
|
|
|
|
// this will trigger thread 1 and then advance and trigger 2 and then advance
|
|
sync = 1;
|
|
|
|
// wait for thread 2 to advance
|
|
while (sync != 3)
|
|
std::this_thread::sleep_for(std::chrono::microseconds(1));
|
|
|
|
// we know now that thread 1 has run and may be finishing
|
|
// and thread 2 is waiting for permission to advance
|
|
|
|
// so we move sync so that thread 2 can finish executing
|
|
++sync;
|
|
|
|
// now wait for both threads to complete
|
|
t1.join();
|
|
t2.join();
|
|
}
|
|
|
|
// now we have the gil again, sanity check
|
|
REQUIRE(py::cast<std::string>(py::module_::import("external_module").attr("multi_interp"))
|
|
== "1");
|
|
|
|
unsafe_reset_internals_for_single_interpreter();
|
|
|
|
// make sure nothing unexpected happened inside the threads, now that they are completed
|
|
REQUIRE(failure == 0);
|
|
}
|
|
# endif // Py_MOD_PER_INTERPRETER_GIL_SUPPORTED
|
|
|
|
#endif // PYBIND11_HAS_SUBINTERPRETER_SUPPORT
|