From: Samuel Just Date: Thu, 18 Dec 2025 18:59:45 +0000 (+0000) Subject: mgr/Gil.cc: do not use PyGILState_Check() X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=b9031339553763acc51f49265e9cb6b16d889567;p=ceph.git mgr/Gil.cc: do not use PyGILState_Check() See comment for explanation. Fixes: https://tracker.ceph.com/issues/74220 Signed-off-by: Samuel Just (cherry picked from commit 19a9981e8f64c7808b0f0d29d1397e97300174ed) --- diff --git a/src/mgr/Gil.cc b/src/mgr/Gil.cc index de27b9acd66..bc794996e2c 100644 --- a/src/mgr/Gil.cc +++ b/src/mgr/Gil.cc @@ -24,6 +24,30 @@ #include "Gil.h" +static void assert_gil() +{ + /* Using PyGILState_Check() isn't appropriate: + * + * https://docs.python.org/3/c-api/init.html#c.PyGILState_Check + * + * "Only if it has had its thread state initialized via PyGILState_Ensure() + * will it return 1." + * + * We got away with it for a while due to: + * + * "Note: If the current Python process has ever created a subinterpreter, + * this function will always return 1." + * + * Instead, use PyThreadState_Get() and use ts->thread_id to confirm that it's + * the right thread (ts->thread_id isn't necessarily stable, and may need to + * change in the future). Once we no longer need to support python versions + * prior to 3.13, this can be PyThreadState_GetUnchecked(). + */ + auto *ts = PyThreadState_Get(); + ceph_assert(ts != nullptr); + ceph_assert(ts->thread_id == PyThread_get_thread_ident()); +} + SafeThreadState::SafeThreadState(PyThreadState *ts_) : ts(ts_) { @@ -79,7 +103,7 @@ Gil::~Gil() without_gil_t::without_gil_t() { - assert(PyGILState_Check()); + assert_gil(); release_gil(); }