diff options
author | Brett Simmers <swtaarrs@users.noreply.github.com> | 2024-05-23 20:59:35 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-23 20:59:35 (GMT) |
commit | be1dfccdf2c5c7671b8a549e969b8cf7d60d9936 (patch) | |
tree | 920d35829b0fb8d51399ff69760998165da704cb /Include/cpython/pystate.h | |
parent | b30d30c747df2bf9f1614df8e76db2ffdb24fcd8 (diff) | |
download | cpython-be1dfccdf2c5c7671b8a549e969b8cf7d60d9936.zip cpython-be1dfccdf2c5c7671b8a549e969b8cf7d60d9936.tar.gz cpython-be1dfccdf2c5c7671b8a549e969b8cf7d60d9936.tar.bz2 |
gh-118727: Don't drop the GIL in `drop_gil()` unless the current thread holds it (#118745)
`drop_gil()` assumes that its caller is attached, which means that the current
thread holds the GIL if and only if the GIL is enabled, and the enabled-state
of the GIL won't change. This isn't true, though, because `detach_thread()`
calls `_PyEval_ReleaseLock()` after detaching and
`_PyThreadState_DeleteCurrent()` calls it after removing the current thread
from consideration for stop-the-world requests (effectively detaching it).
Fix this by remembering whether or not a thread acquired the GIL when it last
attached, in `PyThreadState._status.holds_gil`, and check this in `drop_gil()`
instead of `gil->enabled`.
This fixes a crash in `test_multiprocessing_pool_circular_import()`, so I've
reenabled it.
Diffstat (limited to 'Include/cpython/pystate.h')
-rw-r--r-- | Include/cpython/pystate.h | 4 |
1 files changed, 3 insertions, 1 deletions
diff --git a/Include/cpython/pystate.h b/Include/cpython/pystate.h index 2df9ecd..ed3ee09 100644 --- a/Include/cpython/pystate.h +++ b/Include/cpython/pystate.h @@ -83,6 +83,8 @@ struct _ts { unsigned int bound_gilstate:1; /* Currently in use (maybe holds the GIL). */ unsigned int active:1; + /* Currently holds the GIL. */ + unsigned int holds_gil:1; /* various stages of finalization */ unsigned int finalizing:1; @@ -90,7 +92,7 @@ struct _ts { unsigned int finalized:1; /* padding to align to 4 bytes */ - unsigned int :24; + unsigned int :23; } _status; #ifdef Py_BUILD_CORE # define _PyThreadState_WHENCE_NOTSET -1 |