summaryrefslogtreecommitdiffstats
path: root/Modules/_testcapimodule.c
diff options
context:
space:
mode:
authorSam Gross <colesbury@gmail.com>2024-05-31 14:50:52 (GMT)
committerGitHub <noreply@github.com>2024-05-31 14:50:52 (GMT)
commitbcc1be39cb1d04ad9fc0bd1b9193d3972835a57c (patch)
tree74444fcc18034b396ec249de8f9735441f4338da /Modules/_testcapimodule.c
parent891c1e36f4e08da107443772a4eb50c72a83836d (diff)
downloadcpython-bcc1be39cb1d04ad9fc0bd1b9193d3972835a57c.zip
cpython-bcc1be39cb1d04ad9fc0bd1b9193d3972835a57c.tar.gz
cpython-bcc1be39cb1d04ad9fc0bd1b9193d3972835a57c.tar.bz2
gh-119585: Fix crash involving `PyGILState_Release()` and `PyThreadState_Clear()` (#119753)
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.
Diffstat (limited to 'Modules/_testcapimodule.c')
-rw-r--r--Modules/_testcapimodule.c9
1 files changed, 9 insertions, 0 deletions
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index f99ebf0..b58c172 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -764,6 +764,14 @@ test_thread_state(PyObject *self, PyObject *args)
Py_RETURN_NONE;
}
+static PyObject *
+gilstate_ensure_release(PyObject *module, PyObject *Py_UNUSED(ignored))
+{
+ PyGILState_STATE state = PyGILState_Ensure();
+ PyGILState_Release(state);
+ Py_RETURN_NONE;
+}
+
#ifndef MS_WINDOWS
static PyThread_type_lock wait_done = NULL;
@@ -3351,6 +3359,7 @@ static PyMethodDef TestMethods[] = {
{"test_get_type_dict", test_get_type_dict, METH_NOARGS},
{"test_reftracer", test_reftracer, METH_NOARGS},
{"_test_thread_state", test_thread_state, METH_VARARGS},
+ {"gilstate_ensure_release", gilstate_ensure_release, METH_NOARGS},
#ifndef MS_WINDOWS
{"_spawn_pthread_waiter", spawn_pthread_waiter, METH_NOARGS},
{"_end_spawned_pthread", end_spawned_pthread, METH_NOARGS},