Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 068f81e

Browse files
committed
avoid a deadlock with the interpreter head lock and the GIL during finalization
1 parent 17548dd commit 068f81e

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

Python/pystate.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,14 @@ PyThreadState_DeleteCurrent()
423423
Py_FatalError(
424424
"PyThreadState_DeleteCurrent: no current tstate");
425425
_Py_atomic_store_relaxed(&_PyThreadState_Current, NULL);
426+
/*
427+
Only call tstate_delete_common to have the tstate if we're not finalizing
428+
or we're the main thread. The main thread will do this for us. Not calling
429+
tstate_delete_common means we won't lock the interpreter head lock,
430+
avoiding a possible deadlock with the GIL.
431+
*/
432+
if (!_Py_Finalizing || _Py_Finalizing == tstate)
433+
tstate_delete_common(tstate);
426434
if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate)
427435
PyThread_delete_key_value(autoTLSkey);
428436
tstate_delete_common(tstate);

0 commit comments

Comments
 (0)