@@ -37,6 +37,16 @@ extern "C" {
3737 _Py_atomic_store_relaxed(&(gilstate)->tstate_current, \
3838 (uintptr_t)(value))
3939
40+ static void
41+ ensure_tstate_not_null (const char * func , PyThreadState * tstate )
42+ {
43+ if (tstate == NULL ) {
44+ _Py_FatalErrorFunc (func ,
45+ "current thread state is NULL (released GIL?)" );
46+ }
47+ }
48+
49+
4050/* Forward declarations */
4151static PyThreadState * _PyGILState_GetThisThreadState (struct _gilstate_runtime_state * gilstate );
4252static void _PyThreadState_Delete (PyThreadState * tstate , int check_current );
@@ -400,9 +410,7 @@ PyInterpreterState *
400410PyInterpreterState_Get (void )
401411{
402412 PyThreadState * tstate = _PyThreadState_GET ();
403- if (tstate == NULL ) {
404- Py_FatalError ("no current thread state" );
405- }
413+ ensure_tstate_not_null (__func__ , tstate );
406414 PyInterpreterState * interp = tstate -> interp ;
407415 if (interp == NULL ) {
408416 Py_FatalError ("no current interpreter" );
@@ -819,9 +827,7 @@ tstate_delete_common(PyThreadState *tstate,
819827 struct _gilstate_runtime_state * gilstate )
820828{
821829 _PyRuntimeState * runtime = tstate -> interp -> runtime ;
822- if (tstate == NULL ) {
823- Py_FatalError ("NULL tstate" );
824- }
830+ ensure_tstate_not_null (__func__ , tstate );
825831 PyInterpreterState * interp = tstate -> interp ;
826832 if (interp == NULL ) {
827833 Py_FatalError ("NULL interp" );
@@ -835,8 +841,6 @@ tstate_delete_common(PyThreadState *tstate,
835841 tstate -> next -> prev = tstate -> prev ;
836842 HEAD_UNLOCK (runtime );
837843
838- PyMem_RawFree (tstate );
839-
840844 if (gilstate -> autoInterpreterState &&
841845 PyThread_tss_get (& gilstate -> autoTSSkey ) == tstate )
842846 {
@@ -855,6 +859,7 @@ _PyThreadState_Delete(PyThreadState *tstate, int check_current)
855859 }
856860 }
857861 tstate_delete_common (tstate , gilstate );
862+ PyMem_RawFree (tstate );
858863}
859864
860865
@@ -866,22 +871,22 @@ PyThreadState_Delete(PyThreadState *tstate)
866871
867872
868873void
869- _PyThreadState_DeleteCurrent (_PyRuntimeState * runtime )
874+ _PyThreadState_DeleteCurrent (PyThreadState * tstate )
870875{
871- struct _gilstate_runtime_state * gilstate = & runtime -> gilstate ;
872- PyThreadState * tstate = _PyRuntimeGILState_GetThreadState (gilstate );
873- if (tstate == NULL ) {
874- Py_FatalError ("no current tstate" );
875- }
876+ ensure_tstate_not_null (__func__ , tstate );
877+ struct _gilstate_runtime_state * gilstate = & tstate -> interp -> runtime -> gilstate ;
876878 tstate_delete_common (tstate , gilstate );
877879 _PyRuntimeGILState_SetThreadState (gilstate , NULL );
878- PyEval_ReleaseLock ();
880+ _PyEval_ReleaseLock (tstate );
881+ PyMem_RawFree (tstate );
879882}
880883
881884void
882885PyThreadState_DeleteCurrent (void )
883886{
884- _PyThreadState_DeleteCurrent (& _PyRuntime );
887+ struct _gilstate_runtime_state * gilstate = & _PyRuntime .gilstate ;
888+ PyThreadState * tstate = _PyRuntimeGILState_GetThreadState (gilstate );
889+ _PyThreadState_DeleteCurrent (tstate );
885890}
886891
887892
@@ -938,9 +943,7 @@ PyThreadState *
938943PyThreadState_Get (void )
939944{
940945 PyThreadState * tstate = _PyThreadState_GET ();
941- if (tstate == NULL ) {
942- Py_FatalError ("no current thread" );
943- }
946+ ensure_tstate_not_null (__func__ , tstate );
944947 return tstate ;
945948}
946949
@@ -1342,8 +1345,8 @@ void
13421345PyGILState_Release (PyGILState_STATE oldstate )
13431346{
13441347 _PyRuntimeState * runtime = & _PyRuntime ;
1345- PyThreadState * tcur = PyThread_tss_get (& runtime -> gilstate .autoTSSkey );
1346- if (tcur == NULL ) {
1348+ PyThreadState * tstate = PyThread_tss_get (& runtime -> gilstate .autoTSSkey );
1349+ if (tstate == NULL ) {
13471350 Py_FatalError ("auto-releasing thread-state, "
13481351 "but no thread-state for this thread" );
13491352 }
@@ -1353,26 +1356,27 @@ PyGILState_Release(PyGILState_STATE oldstate)
13531356 but while this is very new (April 2003), the extra check
13541357 by release-only users can't hurt.
13551358 */
1356- if (!PyThreadState_IsCurrent (tcur )) {
1359+ if (!PyThreadState_IsCurrent (tstate )) {
13571360 Py_FatalError ("This thread state must be current when releasing" );
13581361 }
1359- assert (PyThreadState_IsCurrent (tcur ));
1360- -- tcur -> gilstate_counter ;
1361- assert (tcur -> gilstate_counter >= 0 ); /* illegal counter value */
1362+ assert (PyThreadState_IsCurrent (tstate ));
1363+ -- tstate -> gilstate_counter ;
1364+ assert (tstate -> gilstate_counter >= 0 ); /* illegal counter value */
13621365
13631366 /* If we're going to destroy this thread-state, we must
13641367 * clear it while the GIL is held, as destructors may run.
13651368 */
1366- if (tcur -> gilstate_counter == 0 ) {
1369+ if (tstate -> gilstate_counter == 0 ) {
13671370 /* can't have been locked when we created it */
13681371 assert (oldstate == PyGILState_UNLOCKED );
1369- PyThreadState_Clear (tcur );
1372+ PyThreadState_Clear (tstate );
13701373 /* Delete the thread-state. Note this releases the GIL too!
13711374 * It's vital that the GIL be held here, to avoid shutdown
13721375 * races; see bugs 225673 and 1061968 (that nasty bug has a
13731376 * habit of coming back).
13741377 */
1375- _PyThreadState_DeleteCurrent (runtime );
1378+ assert (_PyRuntimeGILState_GetThreadState (& runtime -> gilstate ) == tstate );
1379+ _PyThreadState_DeleteCurrent (tstate );
13761380 }
13771381 /* Release the lock if necessary */
13781382 else if (oldstate == PyGILState_UNLOCKED )
0 commit comments