@@ -2608,17 +2608,18 @@ test_thread_state_ensure_nested(PyObject *self, PyObject *unused)
26082608 PyInterpreterRef ref = get_strong_ref ();
26092609 PyThreadState * save_tstate = PyThreadState_Swap (NULL );
26102610 assert (PyGILState_GetThisThreadState () == save_tstate );
2611+ PyThreadRef refs [10 ];
26112612
26122613 for (int i = 0 ; i < 10 ; ++ i ) {
26132614 // Test reactivation of the detached tstate.
2614- if (PyThreadState_Ensure (ref ) < 0 ) {
2615+ if (PyThreadState_Ensure (ref , & refs [ i ] ) < 0 ) {
26152616 PyInterpreterRef_Close (ref );
26162617 return PyErr_NoMemory ();
26172618 }
26182619
26192620 // No new thread state should've been created.
26202621 assert (PyThreadState_Get () == save_tstate );
2621- PyThreadState_Release ();
2622+ PyThreadState_Release (refs [ i ] );
26222623 }
26232624
26242625 assert (PyThreadState_GetUnchecked () == NULL );
@@ -2627,7 +2628,7 @@ test_thread_state_ensure_nested(PyObject *self, PyObject *unused)
26272628 // If the (detached) gilstate matches the interpreter, then it shouldn't
26282629 // create a new thread state.
26292630 for (int i = 0 ; i < 10 ; ++ i ) {
2630- if (PyThreadState_Ensure (ref ) < 0 ) {
2631+ if (PyThreadState_Ensure (ref , & refs [ i ] ) < 0 ) {
26312632 // This will technically leak other thread states, but it doesn't
26322633 // matter because this is a test.
26332634 PyInterpreterRef_Close (ref );
@@ -2639,7 +2640,7 @@ test_thread_state_ensure_nested(PyObject *self, PyObject *unused)
26392640
26402641 for (int i = 0 ; i < 10 ; ++ i ) {
26412642 assert (PyThreadState_Get () == save_tstate );
2642- PyThreadState_Release ();
2643+ PyThreadState_Release (refs [ i ] );
26432644 }
26442645
26452646 assert (PyThreadState_GetUnchecked () == NULL );
@@ -2672,7 +2673,9 @@ test_thread_state_ensure_crossinterp(PyObject *self, PyObject *unused)
26722673 interp = interpreters.create()
26732674 interp.exec(some_func)
26742675 */
2675- if (PyThreadState_Ensure (ref ) < 0 ) {
2676+ PyThreadRef thread_ref ;
2677+ PyThreadRef other_thread_ref ;
2678+ if (PyThreadState_Ensure (ref , & thread_ref ) < 0 ) {
26762679 PyInterpreterRef_Close (ref );
26772680 return PyErr_NoMemory ();
26782681 }
@@ -2683,16 +2686,16 @@ test_thread_state_ensure_crossinterp(PyObject *self, PyObject *unused)
26832686 assert (PyGILState_GetThisThreadState () == ensured_tstate );
26842687
26852688 // Now though, we should reactivate the thread state
2686- if (PyThreadState_Ensure (ref ) < 0 ) {
2689+ if (PyThreadState_Ensure (ref , & other_thread_ref ) < 0 ) {
26872690 PyInterpreterRef_Close (ref );
26882691 return PyErr_NoMemory ();
26892692 }
26902693
26912694 assert (PyThreadState_Get () == ensured_tstate );
2692- PyThreadState_Release ();
2695+ PyThreadState_Release (other_thread_ref );
26932696
26942697 // Ensure that we're restoring the prior thread state
2695- PyThreadState_Release ();
2698+ PyThreadState_Release (thread_ref );
26962699 assert (PyThreadState_Get () == interp_tstate );
26972700 assert (PyGILState_GetThisThreadState () == interp_tstate );
26982701
0 commit comments