From efc2078b150727bd05bead758cf90fe41b8c91c4 Mon Sep 17 00:00:00 2001 From: Sam Gross Date: Mon, 26 Jan 2026 13:17:57 -0500 Subject: [PATCH] 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. --- tests/test_with_catch/test_subinterpreter.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/tests/test_with_catch/test_subinterpreter.cpp b/tests/test_with_catch/test_subinterpreter.cpp index e322e0fe90..35b7f02334 100644 --- a/tests/test_with_catch/test_subinterpreter.cpp +++ b/tests/test_with_catch/test_subinterpreter.cpp @@ -501,15 +501,21 @@ TEST_CASE("Per-Subinterpreter GIL") { // 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)); + { + py::gil_scoped_release nogil; + 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)); + { + py::gil_scoped_release nogil; + 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