1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
|
#include <pybind11/embed.h>
#ifdef PYBIND11_HAS_SUBINTERPRETER_SUPPORT
# 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.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();
}
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");
}
std::thread([&]() {
// Use it again
{
py::subinterpreter_scoped_activate activate(*sub);
py::module_::import("external_module");
}
sub.reset();
}).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();
}
# 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
|