From 3104796f16f3a6e25340fa9247e532d8863ff283 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Sat, 6 May 2023 14:17:07 -0600 Subject: [PATCH] Make sure the GIL is held by the current thread in _PyEval_InitGIL(). --- Python/ceval_gil.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/Python/ceval_gil.c b/Python/ceval_gil.c index 56122183c2bde8..8c47fb7f7a0014 100644 --- a/Python/ceval_gil.c +++ b/Python/ceval_gil.c @@ -499,6 +499,15 @@ PyEval_ThreadsInitialized(void) return _PyEval_ThreadsInitialized(); } +static inline int +current_thread_holds_gil(struct _gil_runtime_state *gil, PyThreadState *tstate) +{ + if (((PyThreadState*)_Py_atomic_load_relaxed(&gil->last_holder)) != tstate) { + return 0; + } + return _Py_atomic_load_relaxed(&gil->locked); +} + static void init_shared_gil(PyInterpreterState *interp, struct _gil_runtime_state *gil) { @@ -525,8 +534,9 @@ _PyEval_InitGIL(PyThreadState *tstate, int own_gil) if (!own_gil) { PyInterpreterState *main_interp = _PyInterpreterState_Main(); assert(tstate->interp != main_interp); - init_shared_gil(tstate->interp, main_interp->ceval.gil); - locked = _Py_atomic_load_relaxed(&main_interp->ceval.gil->locked); + struct _gil_runtime_state *gil = main_interp->ceval.gil; + init_shared_gil(tstate->interp, gil); + locked = current_thread_holds_gil(gil, tstate); } /* XXX per-interpreter GIL */ else if (!_Py_IsMainInterpreter(tstate->interp)) { @@ -537,7 +547,7 @@ _PyEval_InitGIL(PyThreadState *tstate, int own_gil) init_shared_gil(tstate->interp, main_gil); // XXX For now we lie. tstate->interp->ceval.own_gil = 1; - locked = _Py_atomic_load_relaxed(&main_gil->locked); + locked = current_thread_holds_gil(main_gil, tstate); } else { PyThread_init_thread();