Issue #1856: Avoid crashes and lockups when daemon threads run while the
interpreter is shutting down; instead, these threads are now killed when
they try to take the GIL.
diff --git a/Python/ceval.c b/Python/ceval.c
index 43a5c90..705ed41 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -440,6 +440,12 @@
     if (gil_created()) {
         int err = errno;
         take_gil(tstate);
+        /* _Py_Finalizing is protected by the GIL */
+        if (_Py_Finalizing && tstate != _Py_Finalizing) {
+            drop_gil(tstate);
+            PyThread_exit_thread();
+            assert(0);  /* unreachable */
+        }
         errno = err;
     }
 #endif