diff Python/pythonrun.c @ 64857:38b0d5d0dd30

Issue #9901: Destroying the GIL in Py_Finalize() can fail if some other threads are still running. Instead, reinitialize the GIL on a second call to Py_Initialize().
author Antoine Pitrou <solipsis@pitrou.net>
date Mon, 20 Sep 2010 20:13:48 +0000
parents 557e2537c9ae
children 01b96f78fb70
line wrap: on
line diff
--- a/Python/pythonrun.c	Mon Sep 20 19:12:07 2010 +0000
+++ b/Python/pythonrun.c	Mon Sep 20 20:13:48 2010 +0000
@@ -217,8 +217,15 @@
         Py_FatalError("Py_Initialize: can't make first thread");
     (void) PyThreadState_Swap(tstate);
 
-    /* auto-thread-state API, if available */
 #ifdef WITH_THREAD
+    /* We can't call _PyEval_FiniThreads() in Py_Finalize because
+       destroying the GIL might fail when it is being referenced from
+       another running thread (see issue #9901).
+       Instead we destroy the previously created GIL here, which ensures
+       that we can call Py_Initialize / Py_Finalize multiple times. */
+    _PyEval_FiniThreads();
+
+    /* Auto-thread-state API */
     _PyGILState_Init(interp, tstate);
 #endif /* WITH_THREAD */
 
@@ -514,10 +521,6 @@
 
     PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
 
-#ifdef WITH_THREAD
-    _PyEval_FiniThreads();
-#endif
-
 #ifdef Py_TRACE_REFS
     /* Display addresses (& refcnts) of all objects still alive.
      * An address can be used to find the repr of the object, printed