diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2022-09-20 00:36:26 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-20 00:36:26 (GMT) |
commit | dcff50a3e9a38302009263594b4485c49f51e853 (patch) | |
tree | 89f9b9d353a7056e3ad90f17f05667921608fe03 /Python/ceval_gil.h | |
parent | 88a3f1873ec4e6d35b1736c480c7087e01fca40f (diff) | |
download | cpython-dcff50a3e9a38302009263594b4485c49f51e853.zip cpython-dcff50a3e9a38302009263594b4485c49f51e853.tar.gz cpython-dcff50a3e9a38302009263594b4485c49f51e853.tar.bz2 |
gh-96387: take_gil() resets drop request before exit (GH-96869) (GH-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)
(cherry picked from commit 6ff54716f1073a4bcfed8a1ec0b518c489c1af0d)
Co-authored-by: Victor Stinner <vstinner@python.org>
Diffstat (limited to 'Python/ceval_gil.h')
-rw-r--r-- | Python/ceval_gil.h | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/Python/ceval_gil.h b/Python/ceval_gil.h index 9b8b432..824897e 100644 --- a/Python/ceval_gil.h +++ b/Python/ceval_gil.h @@ -247,6 +247,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; @@ -262,11 +263,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; } } |