[Python-checkins] gh-96387: take_gil() resets drop request before exit (#96869) (#96941)
vstinner
webhook-mailer at python.org
Mon Sep 19 20:12:51 EDT 2022
https://github.com/python/cpython/commit/6ff54716f1073a4bcfed8a1ec0b518c489c1af0d
commit: 6ff54716f1073a4bcfed8a1ec0b518c489c1af0d
branch: 3.11
author: Victor Stinner <vstinner at python.org>
committer: vstinner <vstinner at python.org>
date: 2022年09月20日T02:12:09+02:00
summary:
gh-96387: take_gil() resets drop request before exit (#96869) (#96941)
At Python exit, sometimes a thread holding the GIL can wait forever
for a thread (usually a daemon thread) which requested to drop the
GIL, whereas the thread already exited. To fix the race condition,
the thread which requested the GIL drop now resets its request before
exiting.
take_gil() now calls RESET_GIL_DROP_REQUEST() before
PyThread_exit_thread() if it called SET_GIL_DROP_REQUEST to fix a
race condition with drop_gil().
Issue discovered and analyzed by Mingliang ZHAO.
(cherry picked from commit 04f4977f508583954ad7b9cb09076ee1e57461f8)
files:
A Misc/NEWS.d/next/Core and Builtins/2022-09-16-16-54-35.gh-issue-96387.GRzewg.rst
M Python/ceval_gil.h
diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-09-16-16-54-35.gh-issue-96387.GRzewg.rst b/Misc/NEWS.d/next/Core and Builtins/2022-09-16-16-54-35.gh-issue-96387.GRzewg.rst
new file mode 100644
index 000000000000..611ab94bc636
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2022-09-16-16-54-35.gh-issue-96387.GRzewg.rst
@@ -0,0 +1,5 @@
+At Python exit, sometimes a thread holding the GIL can wait forever for a
+thread (usually a daemon thread) which requested to drop the GIL, whereas
+the thread already exited. To fix the race condition, the thread which
+requested the GIL drop now resets its request before exiting. Issue
+discovered and analyzed by Mingliang ZHAO. Patch by Victor Stinner.
diff --git a/Python/ceval_gil.h b/Python/ceval_gil.h
index 1b2dc7f8e1dc..23f6fb26580e 100644
--- a/Python/ceval_gil.h
+++ b/Python/ceval_gil.h
@@ -239,6 +239,7 @@ take_gil(PyThreadState *tstate)
goto _ready;
}
+ int drop_requested = 0;
while (_Py_atomic_load_relaxed(&gil->locked)) {
unsigned long saved_switchnum = gil->switch_number;
@@ -254,11 +255,21 @@ take_gil(PyThreadState *tstate)
{
if (tstate_must_exit(tstate)) {
MUTEX_UNLOCK(gil->mutex);
+ // gh-96387: If the loop requested a drop request in a previous
+ // iteration, reset the request. Otherwise, drop_gil() can
+ // block forever waiting for the thread which exited. Drop
+ // requests made by other threads are also reset: these threads
+ // may have to request again a drop request (iterate one more
+ // time).
+ if (drop_requested) {
+ RESET_GIL_DROP_REQUEST(interp);
+ }
PyThread_exit_thread();
}
assert(is_tstate_valid(tstate));
SET_GIL_DROP_REQUEST(interp);
+ drop_requested = 1;
}
}
More information about the Python-checkins
mailing list