mirror of
https://github.com/python/cpython.git
synced 2025-09-09 18:32:22 +00:00
[3.9] bpo-40826: PyOS_InterruptOccurred() requires GIL (GH-20578) (GH-20618)
* bpo-40826: Add _Py_EnsureTstateNotNULL() macro (GH-20571) Add _Py_EnsureTstateNotNULL(tstate) macro: call Py_FatalError() if tstate is NULL, the error message contains the current function name. (cherry picked from commit3026cad59b
) * bpo-40826: PyOS_InterruptOccurred() requires GIL (GH-20578) PyOS_InterruptOccurred() now fails with a fatal error if it is called with the GIL released. (cherry picked from commitcbe1296922
)
This commit is contained in:
parent
5d2396c8cf
commit
6d62dc1ea4
8 changed files with 43 additions and 31 deletions
|
@ -74,6 +74,21 @@ _PyThreadState_GET(void)
|
||||||
#undef PyThreadState_GET
|
#undef PyThreadState_GET
|
||||||
#define PyThreadState_GET() _PyThreadState_GET()
|
#define PyThreadState_GET() _PyThreadState_GET()
|
||||||
|
|
||||||
|
PyAPI_FUNC(void) _Py_NO_RETURN _Py_FatalError_TstateNULL(const char *func);
|
||||||
|
|
||||||
|
static inline void
|
||||||
|
_Py_EnsureFuncTstateNotNULL(const char *func, PyThreadState *tstate)
|
||||||
|
{
|
||||||
|
if (tstate == NULL) {
|
||||||
|
_Py_FatalError_TstateNULL(func);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call Py_FatalError() if tstate is NULL
|
||||||
|
#define _Py_EnsureTstateNotNULL(tstate) \
|
||||||
|
_Py_EnsureFuncTstateNotNULL(__func__, tstate)
|
||||||
|
|
||||||
|
|
||||||
/* Get the current interpreter state.
|
/* Get the current interpreter state.
|
||||||
|
|
||||||
The macro is unsafe: it does not check for error and it can return NULL.
|
The macro is unsafe: it does not check for error and it can return NULL.
|
||||||
|
@ -84,7 +99,9 @@ _PyThreadState_GET(void)
|
||||||
and _PyGILState_GetInterpreterStateUnsafe(). */
|
and _PyGILState_GetInterpreterStateUnsafe(). */
|
||||||
static inline PyInterpreterState* _PyInterpreterState_GET(void) {
|
static inline PyInterpreterState* _PyInterpreterState_GET(void) {
|
||||||
PyThreadState *tstate = _PyThreadState_GET();
|
PyThreadState *tstate = _PyThreadState_GET();
|
||||||
assert(tstate != NULL);
|
#ifdef Py_DEBUG
|
||||||
|
_Py_EnsureTstateNotNULL(tstate);
|
||||||
|
#endif
|
||||||
return tstate->interp;
|
return tstate->interp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -67,7 +67,10 @@ class CAPITest(unittest.TestCase):
|
||||||
self.assertTrue(err.rstrip().startswith(
|
self.assertTrue(err.rstrip().startswith(
|
||||||
b'Fatal Python error: '
|
b'Fatal Python error: '
|
||||||
b'PyThreadState_Get: '
|
b'PyThreadState_Get: '
|
||||||
b'current thread state is NULL (released GIL?)'))
|
b'the function must be called with the GIL held, '
|
||||||
|
b'but the GIL is released '
|
||||||
|
b'(the current Python thread state is NULL)'),
|
||||||
|
err)
|
||||||
|
|
||||||
def test_memoryview_from_NULL_pointer(self):
|
def test_memoryview_from_NULL_pointer(self):
|
||||||
self.assertRaises(ValueError, _testcapi.make_memoryview_from_NULL_pointer)
|
self.assertRaises(ValueError, _testcapi.make_memoryview_from_NULL_pointer)
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
:c:func:`PyOS_InterruptOccurred` now fails with a fatal error if it is
|
||||||
|
called with the GIL released.
|
|
@ -1784,7 +1784,7 @@ PyOS_FiniInterrupts(void)
|
||||||
int
|
int
|
||||||
_PyOS_InterruptOccurred(PyThreadState *tstate)
|
_PyOS_InterruptOccurred(PyThreadState *tstate)
|
||||||
{
|
{
|
||||||
assert(tstate != NULL);
|
_Py_EnsureTstateNotNULL(tstate);
|
||||||
if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
|
if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -240,13 +240,13 @@ UNSIGNAL_ASYNC_EXC(PyInterpreterState *interp)
|
||||||
#endif
|
#endif
|
||||||
#include "ceval_gil.h"
|
#include "ceval_gil.h"
|
||||||
|
|
||||||
static void
|
void _Py_NO_RETURN
|
||||||
ensure_tstate_not_null(const char *func, PyThreadState *tstate)
|
_Py_FatalError_TstateNULL(const char *func)
|
||||||
{
|
{
|
||||||
if (tstate == NULL) {
|
|
||||||
_Py_FatalErrorFunc(func,
|
_Py_FatalErrorFunc(func,
|
||||||
"current thread state is NULL (released GIL?)");
|
"the function must be called with the GIL held, "
|
||||||
}
|
"but the GIL is released "
|
||||||
|
"(the current Python thread state is NULL)");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -346,7 +346,7 @@ PyEval_AcquireLock(void)
|
||||||
{
|
{
|
||||||
_PyRuntimeState *runtime = &_PyRuntime;
|
_PyRuntimeState *runtime = &_PyRuntime;
|
||||||
PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
|
PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
|
||||||
ensure_tstate_not_null(__func__, tstate);
|
_Py_EnsureTstateNotNULL(tstate);
|
||||||
|
|
||||||
take_gil(tstate);
|
take_gil(tstate);
|
||||||
}
|
}
|
||||||
|
@ -375,7 +375,7 @@ _PyEval_ReleaseLock(PyThreadState *tstate)
|
||||||
void
|
void
|
||||||
PyEval_AcquireThread(PyThreadState *tstate)
|
PyEval_AcquireThread(PyThreadState *tstate)
|
||||||
{
|
{
|
||||||
ensure_tstate_not_null(__func__, tstate);
|
_Py_EnsureTstateNotNULL(tstate);
|
||||||
|
|
||||||
take_gil(tstate);
|
take_gil(tstate);
|
||||||
|
|
||||||
|
@ -410,7 +410,7 @@ void
|
||||||
_PyEval_ReInitThreads(_PyRuntimeState *runtime)
|
_PyEval_ReInitThreads(_PyRuntimeState *runtime)
|
||||||
{
|
{
|
||||||
PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
|
PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
|
||||||
ensure_tstate_not_null(__func__, tstate);
|
_Py_EnsureTstateNotNULL(tstate);
|
||||||
|
|
||||||
struct _gil_runtime_state *gil = &runtime->ceval.gil;
|
struct _gil_runtime_state *gil = &runtime->ceval.gil;
|
||||||
if (!gil_created(gil)) {
|
if (!gil_created(gil)) {
|
||||||
|
@ -445,7 +445,7 @@ PyEval_SaveThread(void)
|
||||||
{
|
{
|
||||||
_PyRuntimeState *runtime = &_PyRuntime;
|
_PyRuntimeState *runtime = &_PyRuntime;
|
||||||
PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
|
PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
|
||||||
ensure_tstate_not_null(__func__, tstate);
|
_Py_EnsureTstateNotNULL(tstate);
|
||||||
|
|
||||||
struct _ceval_runtime_state *ceval = &runtime->ceval;
|
struct _ceval_runtime_state *ceval = &runtime->ceval;
|
||||||
struct _ceval_state *ceval2 = &tstate->interp->ceval;
|
struct _ceval_state *ceval2 = &tstate->interp->ceval;
|
||||||
|
@ -457,7 +457,7 @@ PyEval_SaveThread(void)
|
||||||
void
|
void
|
||||||
PyEval_RestoreThread(PyThreadState *tstate)
|
PyEval_RestoreThread(PyThreadState *tstate)
|
||||||
{
|
{
|
||||||
ensure_tstate_not_null(__func__, tstate);
|
_Py_EnsureTstateNotNULL(tstate);
|
||||||
|
|
||||||
take_gil(tstate);
|
take_gil(tstate);
|
||||||
|
|
||||||
|
@ -889,7 +889,7 @@ eval_frame_handle_pending(PyThreadState *tstate)
|
||||||
PyObject* _Py_HOT_FUNCTION
|
PyObject* _Py_HOT_FUNCTION
|
||||||
_PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
|
_PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
|
||||||
{
|
{
|
||||||
ensure_tstate_not_null(__func__, tstate);
|
_Py_EnsureTstateNotNULL(tstate);
|
||||||
|
|
||||||
#ifdef DXPAIRS
|
#ifdef DXPAIRS
|
||||||
int lastopcode = 0;
|
int lastopcode = 0;
|
||||||
|
|
|
@ -1426,7 +1426,7 @@ void
|
||||||
_PyErr_WriteUnraisableMsg(const char *err_msg_str, PyObject *obj)
|
_PyErr_WriteUnraisableMsg(const char *err_msg_str, PyObject *obj)
|
||||||
{
|
{
|
||||||
PyThreadState *tstate = _PyThreadState_GET();
|
PyThreadState *tstate = _PyThreadState_GET();
|
||||||
assert(tstate != NULL);
|
_Py_EnsureTstateNotNULL(tstate);
|
||||||
|
|
||||||
PyObject *err_msg = NULL;
|
PyObject *err_msg = NULL;
|
||||||
PyObject *exc_type, *exc_value, *exc_tb;
|
PyObject *exc_type, *exc_value, *exc_tb;
|
||||||
|
|
|
@ -39,16 +39,6 @@ extern "C" {
|
||||||
_Py_atomic_store_relaxed(&(gilstate)->tstate_current, \
|
_Py_atomic_store_relaxed(&(gilstate)->tstate_current, \
|
||||||
(uintptr_t)(value))
|
(uintptr_t)(value))
|
||||||
|
|
||||||
static void
|
|
||||||
ensure_tstate_not_null(const char *func, PyThreadState *tstate)
|
|
||||||
{
|
|
||||||
if (tstate == NULL) {
|
|
||||||
_Py_FatalErrorFunc(func,
|
|
||||||
"current thread state is NULL (released GIL?)");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* Forward declarations */
|
/* Forward declarations */
|
||||||
static PyThreadState *_PyGILState_GetThisThreadState(struct _gilstate_runtime_state *gilstate);
|
static PyThreadState *_PyGILState_GetThisThreadState(struct _gilstate_runtime_state *gilstate);
|
||||||
static void _PyThreadState_Delete(PyThreadState *tstate, int check_current);
|
static void _PyThreadState_Delete(PyThreadState *tstate, int check_current);
|
||||||
|
@ -431,7 +421,7 @@ PyInterpreterState *
|
||||||
PyInterpreterState_Get(void)
|
PyInterpreterState_Get(void)
|
||||||
{
|
{
|
||||||
PyThreadState *tstate = _PyThreadState_GET();
|
PyThreadState *tstate = _PyThreadState_GET();
|
||||||
ensure_tstate_not_null(__func__, tstate);
|
_Py_EnsureTstateNotNULL(tstate);
|
||||||
PyInterpreterState *interp = tstate->interp;
|
PyInterpreterState *interp = tstate->interp;
|
||||||
if (interp == NULL) {
|
if (interp == NULL) {
|
||||||
Py_FatalError("no current interpreter");
|
Py_FatalError("no current interpreter");
|
||||||
|
@ -846,7 +836,7 @@ static void
|
||||||
tstate_delete_common(PyThreadState *tstate,
|
tstate_delete_common(PyThreadState *tstate,
|
||||||
struct _gilstate_runtime_state *gilstate)
|
struct _gilstate_runtime_state *gilstate)
|
||||||
{
|
{
|
||||||
ensure_tstate_not_null(__func__, tstate);
|
_Py_EnsureTstateNotNULL(tstate);
|
||||||
PyInterpreterState *interp = tstate->interp;
|
PyInterpreterState *interp = tstate->interp;
|
||||||
if (interp == NULL) {
|
if (interp == NULL) {
|
||||||
Py_FatalError("NULL interpreter");
|
Py_FatalError("NULL interpreter");
|
||||||
|
@ -897,7 +887,7 @@ PyThreadState_Delete(PyThreadState *tstate)
|
||||||
void
|
void
|
||||||
_PyThreadState_DeleteCurrent(PyThreadState *tstate)
|
_PyThreadState_DeleteCurrent(PyThreadState *tstate)
|
||||||
{
|
{
|
||||||
ensure_tstate_not_null(__func__, tstate);
|
_Py_EnsureTstateNotNULL(tstate);
|
||||||
struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
|
struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
|
||||||
tstate_delete_common(tstate, gilstate);
|
tstate_delete_common(tstate, gilstate);
|
||||||
_PyRuntimeGILState_SetThreadState(gilstate, NULL);
|
_PyRuntimeGILState_SetThreadState(gilstate, NULL);
|
||||||
|
@ -967,7 +957,7 @@ PyThreadState *
|
||||||
PyThreadState_Get(void)
|
PyThreadState_Get(void)
|
||||||
{
|
{
|
||||||
PyThreadState *tstate = _PyThreadState_GET();
|
PyThreadState *tstate = _PyThreadState_GET();
|
||||||
ensure_tstate_not_null(__func__, tstate);
|
_Py_EnsureTstateNotNULL(tstate);
|
||||||
return tstate;
|
return tstate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -457,7 +457,7 @@ static PyObject *
|
||||||
sys_audit(PyObject *self, PyObject *const *args, Py_ssize_t argc)
|
sys_audit(PyObject *self, PyObject *const *args, Py_ssize_t argc)
|
||||||
{
|
{
|
||||||
PyThreadState *tstate = _PyThreadState_GET();
|
PyThreadState *tstate = _PyThreadState_GET();
|
||||||
assert(tstate != NULL);
|
_Py_EnsureTstateNotNULL(tstate);
|
||||||
|
|
||||||
if (argc == 0) {
|
if (argc == 0) {
|
||||||
_PyErr_SetString(tstate, PyExc_TypeError,
|
_PyErr_SetString(tstate, PyExc_TypeError,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue