◐ Shell
clean mode source ↗

gh-118727: Don't drop the GIL in `drop_gil()` unless the current thread holds it by swtaarrs · Pull Request #118745 · python/cpython

@swtaarrs

@swtaarrs swtaarrs marked this pull request as ready for review

May 7, 2024 23:30

colesbury

@swtaarrs

@swtaarrs swtaarrs marked this pull request as ready for review

May 8, 2024 20:58

@swtaarrs swtaarrs changed the title gh-118727: Don't drop the GIL during thread deletion unless the current thread holds it gh-118727: Don't drop the GIL in drop_gil() unless the current thread holds it

May 8, 2024

colesbury

@swtaarrs

colesbury

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request

May 23, 2024
…t thread holds it (pythonGH-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.
(cherry picked from commit be1dfcc)

Co-authored-by: Brett Simmers <swtaarrs@users.noreply.github.com>

colesbury pushed a commit that referenced this pull request

May 23, 2024
…nt thread holds it (GH-118745) (#119474)

`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.
(cherry picked from commit be1dfcc)

Co-authored-by: Brett Simmers <swtaarrs@users.noreply.github.com>

estyxx pushed a commit to estyxx/cpython that referenced this pull request

Jul 17, 2024
…t thread holds it (python#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.