avoid crashes and lockups from daemon threads during interpreter shutdown (#1856)
diff --git a/Python/ceval.c b/Python/ceval.c
index 18bc66b..b1f5e8e 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -355,6 +355,12 @@
     if (interpreter_lock) {
         int err = errno;
         PyThread_acquire_lock(interpreter_lock, 1);
+        /* _Py_Finalizing is protected by the GIL */
+        if (_Py_Finalizing && tstate != _Py_Finalizing) {
+            PyThread_release_lock(interpreter_lock);
+            PyThread_exit_thread();
+            assert(0);  /* unreachable */
+        }
         errno = err;
     }
 #endif