Skip to content

Commit efc2078

Browse files
committed
Fix deadlock in test with free threading
Importing "widget_module" re-enables the GIL. In current versions of CPython, this requires pausing all threads attached to all interpreters. The spinning on sync/num without a py::gil_scoped_release causes occasional deadlocks.
1 parent 6c83607 commit efc2078

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

tests/test_with_catch/test_subinterpreter.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -501,15 +501,21 @@ TEST_CASE("Per-Subinterpreter GIL") {
501501

502502
// wait for something to set sync to our thread number
503503
// we are holding our subinterpreter's GIL
504-
while (sync != num)
505-
std::this_thread::sleep_for(std::chrono::microseconds(1));
504+
{
505+
py::gil_scoped_release nogil;
506+
while (sync != num)
507+
std::this_thread::sleep_for(std::chrono::microseconds(1));
508+
}
506509

507510
// now change it so the next thread can move on
508511
++sync;
509512

510513
// but keep holding the GIL until after the next thread moves on as well
511-
while (sync == num + 1)
512-
std::this_thread::sleep_for(std::chrono::microseconds(1));
514+
{
515+
py::gil_scoped_release nogil;
516+
while (sync == num + 1)
517+
std::this_thread::sleep_for(std::chrono::microseconds(1));
518+
}
513519

514520
// one last check before quitting the thread, the internals should be different
515521
auto sub_int

0 commit comments

Comments
 (0)