Skip to content

Commit d7bafe8

Browse files
committed
Test using PyGILState_Check before PyGILState_Ensure
1 parent 0945b42 commit d7bafe8

File tree

1 file changed

+17
-0
lines changed
  • graalpython/com.oracle.graal.python.test/src/tests/cpyext

1 file changed

+17
-0
lines changed

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_thread.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,33 @@ def test_register_new_thread(self):
8989
includes="#include <pthread.h>",
9090
code=r'''
9191
void* thread_entrypoint(void* arg) {
92+
// This check is important not just to check that the function works without the thread attached,
93+
// but also because the thread attaching logic in it can break the following PyGILState_Ensure call
94+
if (PyGILState_Check()) {
95+
PyErr_SetString(PyExc_RuntimeError, "Thread shouldn't be holding the GIL at this point");
96+
PyErr_WriteUnraisable(NULL);
97+
return NULL;
98+
}
9299
PyObject* callable = (PyObject*)arg;
93100
PyGILState_STATE gstate;
94101
gstate = PyGILState_Ensure();
102+
if (!PyGILState_Check()) {
103+
PyErr_SetString(PyExc_RuntimeError, "GIL not acquired");
104+
PyErr_WriteUnraisable(NULL);
105+
return NULL;
106+
}
95107
if (!PyObject_CallNoArgs(callable)) {
96108
PyErr_WriteUnraisable(callable);
97109
}
98110
if (PyThreadState_Get() == NULL || PyThreadState_Get() == NULL) {
99111
PyErr_WriteUnraisable(callable);
100112
}
101113
PyGILState_Release(gstate);
114+
if (PyGILState_Check()) {
115+
PyErr_SetString(PyExc_RuntimeError, "GIL not released");
116+
PyErr_WriteUnraisable(NULL);
117+
return NULL;
118+
}
102119
return NULL;
103120
}
104121
PyObject* run_in_thread(PyObject* self, PyObject* callable) {

0 commit comments

Comments
 (0)