Skip to content

Commit b0b384b

Browse files
committed
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().
1 parent 0646b4b commit b0b384b

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ What's New in Python 3.2 Alpha 3?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #9901: Destroying the GIL in Py_Finalize() can fail if some other
14+
threads are still running. Instead, reinitialize the GIL on a second
15+
call to Py_Initialize().
16+
1317
- Issue #9252: PyImport_Import no longer uses a fromlist hack to return the
1418
module that was imported, but instead gets the module from sys.modules.
1519

Python/pythonrun.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,15 @@ Py_InitializeEx(int install_sigs)
217217
Py_FatalError("Py_Initialize: can't make first thread");
218218
(void) PyThreadState_Swap(tstate);
219219

220-
/* auto-thread-state API, if available */
221220
#ifdef WITH_THREAD
221+
/* We can't call _PyEval_FiniThreads() in Py_Finalize because
222+
destroying the GIL might fail when it is being referenced from
223+
another running thread (see issue #9901).
224+
Instead we destroy the previously created GIL here, which ensures
225+
that we can call Py_Initialize / Py_Finalize multiple times. */
226+
_PyEval_FiniThreads();
227+
228+
/* Auto-thread-state API */
222229
_PyGILState_Init(interp, tstate);
223230
#endif /* WITH_THREAD */
224231

@@ -514,10 +521,6 @@ Py_Finalize(void)
514521

515522
PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
516523

517-
#ifdef WITH_THREAD
518-
_PyEval_FiniThreads();
519-
#endif
520-
521524
#ifdef Py_TRACE_REFS
522525
/* Display addresses (& refcnts) of all objects still alive.
523526
* An address can be used to find the repr of the object, printed

0 commit comments

Comments
 (0)