avoid a deadlock with the interpreter head lock and the GIL during finalization
diff --git a/Python/pystate.c b/Python/pystate.c
index 2ac2fd5..8c1fad2 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -423,6 +423,14 @@
         Py_FatalError(
             "PyThreadState_DeleteCurrent: no current tstate");
     _Py_atomic_store_relaxed(&_PyThreadState_Current, NULL);
+    /*
+      Only call tstate_delete_common to have the tstate if we're not finalizing
+      or we're the main thread. The main thread will do this for us. Not calling
+      tstate_delete_common means we won't lock the interpreter head lock,
+      avoiding a possible deadlock with the GIL.
+    */
+    if (!_Py_Finalizing || _Py_Finalizing == tstate)
+        tstate_delete_common(tstate);
     if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate)
         PyThread_delete_key_value(autoTLSkey);
     tstate_delete_common(tstate);