diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2024-05-31 15:29:50 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-31 15:29:50 (GMT) |
commit | ea48de4f4fadd7466c43ecaa88b431fc64b12acb (patch) | |
tree | 0ddf6162928fb41b4eb53ab9839e131ae66df0e3 /Lib | |
parent | d6faac6d1f825405398158272286aaed94eb51fc (diff) | |
download | cpython-ea48de4f4fadd7466c43ecaa88b431fc64b12acb.zip cpython-ea48de4f4fadd7466c43ecaa88b431fc64b12acb.tar.gz cpython-ea48de4f4fadd7466c43ecaa88b431fc64b12acb.tar.bz2 |
[3.13] gh-119585: Fix crash involving `PyGILState_Release()` and `PyThreadState_Clear()` (GH-119753) (#119859)
Make sure that `gilstate_counter` is not zero in when calling
`PyThreadState_Clear()`. A destructor called from `PyThreadState_Clear()` may
call back into `PyGILState_Ensure()` and `PyGILState_Release()`. If
`gilstate_counter` is zero, it will try to create a new thread state before
the current active thread state is destroyed, leading to an assertion failure
or crash.
(cherry picked from commit bcc1be39cb1d04ad9fc0bd1b9193d3972835a57c)
Co-authored-by: Sam Gross <colesbury@gmail.com>
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_capi/test_misc.py | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/Lib/test/test_capi/test_misc.py b/Lib/test/test_capi/test_misc.py index ed42d7b..f3d16e4 100644 --- a/Lib/test/test_capi/test_misc.py +++ b/Lib/test/test_capi/test_misc.py @@ -2890,6 +2890,22 @@ class TestThreadState(unittest.TestCase): @threading_helper.reap_threads @threading_helper.requires_working_threading() + def test_thread_gilstate_in_clear(self): + # See https://github.com/python/cpython/issues/119585 + class C: + def __del__(self): + _testcapi.gilstate_ensure_release() + + # Thread-local variables are destroyed in `PyThreadState_Clear()`. + local_var = threading.local() + + def callback(): + local_var.x = C() + + _testcapi._test_thread_state(callback) + + @threading_helper.reap_threads + @threading_helper.requires_working_threading() def test_gilstate_ensure_no_deadlock(self): # See https://github.com/python/cpython/issues/96071 code = textwrap.dedent(""" |