From 96b239778766d32d5158aca805e08199b3c0a743 Mon Sep 17 00:00:00 2001
From: robot-contrib <robot-contrib@yandex-team.com>
Date: Tue, 2 Jul 2024 22:47:57 +0300
Subject: Update contrib/tools/python3 to 3.12.4
 6e8edffbef193b35b45ddccdc3beda6bb2627186

---
 contrib/tools/python3/Python/pystate.c | 6 ++++++
 1 file changed, 6 insertions(+)

(limited to 'contrib/tools/python3/Python/pystate.c')

diff --git a/contrib/tools/python3/Python/pystate.c b/contrib/tools/python3/Python/pystate.c
index 7acf4f9854..93fbbaaf01 100644
--- a/contrib/tools/python3/Python/pystate.c
+++ b/contrib/tools/python3/Python/pystate.c
@@ -2282,12 +2282,18 @@ PyGILState_Release(PyGILState_STATE oldstate)
         /* can't have been locked when we created it */
         assert(oldstate == PyGILState_UNLOCKED);
         // XXX Unbind tstate here.
+        // gh-119585: `PyThreadState_Clear()` may call destructors that
+        // themselves use PyGILState_Ensure and PyGILState_Release, so make
+        // sure that gilstate_counter is not zero when calling it.
+        ++tstate->gilstate_counter;
         PyThreadState_Clear(tstate);
+        --tstate->gilstate_counter;
         /* Delete the thread-state.  Note this releases the GIL too!
          * It's vital that the GIL be held here, to avoid shutdown
          * races; see bugs 225673 and 1061968 (that nasty bug has a
          * habit of coming back).
          */
+        assert(tstate->gilstate_counter == 0);
         assert(current_fast_get(runtime) == tstate);
         _PyThreadState_DeleteCurrent(tstate);
     }
-- 
cgit v1.2.3