]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
mgr/Gil.cc: do not use PyGILState_Check()
authorSamuel Just <sjust@redhat.com>
Thu, 18 Dec 2025 18:59:45 +0000 (18:59 +0000)
committerSamuel Just <sjust@redhat.com>
Thu, 18 Dec 2025 19:59:42 +0000 (19:59 +0000)
See comment for explanation.

Fixes: https://tracker.ceph.com/issues/74220
Signed-off-by: Samuel Just <sjust@redhat.com>
src/mgr/Gil.cc

index b1285fc8ebd7c105fe6ad33f819b2e1370f3223a..159a93996dc7d83b94f9deaabe5881202562da37 100644 (file)
 
 #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_)
 {
@@ -80,7 +104,7 @@ Gil::~Gil()
 
 without_gil_t::without_gil_t()
 {
-  assert(PyGILState_Check());
+  assert_gil();
   release_gil();
 }