[Python-checkins] bpo-39877: take_gil() checks tstate_must_exit() twice (GH-18890)
Victor Stinner
webhook-mailer at python.org
Mon Mar 9 18:10:57 EDT 2020
https://github.com/python/cpython/commit/9229eeee105f19705f72e553cf066751ac47c7b7
commit: 9229eeee105f19705f72e553cf066751ac47c7b7
branch: master
author: Victor Stinner <vstinner at python.org>
committer: GitHub <noreply at github.com>
date: 2020-03-09T23:10:53+01:00
summary:
bpo-39877: take_gil() checks tstate_must_exit() twice (GH-18890)
take_gil() now also checks tstate_must_exit() after acquiring
the GIL: exit the thread if Py_Finalize() has been called.
files:
M Python/ceval_gil.h
diff --git a/Python/ceval_gil.h b/Python/ceval_gil.h
index 03f04b93e0a32..9c051ae57b03b 100644
--- a/Python/ceval_gil.h
+++ b/Python/ceval_gil.h
@@ -213,8 +213,13 @@ take_gil(PyThreadState *tstate)
assert(tstate != NULL);
- /* Check if we should make a quick exit. */
if (tstate_must_exit(tstate)) {
+ /* bpo-39877: If Py_Finalize() has been called and tstate is not the
+ thread which called Py_Finalize(), exit immediately the thread.
+
+ This code path can be reached by a daemon thread after Py_Finalize()
+ completes. In this case, tstate is a dangling pointer: points to
+ PyThreadState freed memory. */
PyThread_exit_thread();
}
@@ -282,6 +287,18 @@ take_gil(PyThreadState *tstate)
MUTEX_UNLOCK(gil->mutex);
+ if (tstate_must_exit(tstate)) {
+ /* bpo-36475: If Py_Finalize() has been called and tstate is not
+ the thread which called Py_Finalize(), exit immediately the
+ thread.
+
+ This code path can be reached by a daemon thread which was waiting
+ in take_gil() while the main thread called
+ wait_for_thread_shutdown() from Py_Finalize(). */
+ drop_gil(ceval, tstate);
+ PyThread_exit_thread();
+ }
+
errno = err;
}
More information about the Python-checkins
mailing list