]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.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)
committerPatrick Donnelly <pdonnell@ibm.com>
Fri, 8 May 2026 19:38:37 +0000 (15:38 -0400)
See comment for explanation.

Fixes: https://tracker.ceph.com/issues/74220
Signed-off-by: Samuel Just <sjust@redhat.com>
(cherry picked from commit 19a9981e8f64c7808b0f0d29d1397e97300174ed)

src/mgr/Gil.cc

index de27b9acd666b40497118b014ffd67f1eab8c9d7..bc794996e2c5ef92cc4a47585d851af624870f15 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_)
 {
@@ -79,7 +103,7 @@ Gil::~Gil()
 
 without_gil_t::without_gil_t()
 {
-  assert(PyGILState_Check());
+  assert_gil();
   release_gil();
 }