File bsc1188607-pythreadstate_clear-decref.patch of Package python3.36158

From d7c7ce2d7496704aa81367fd23191973b18c39dc Mon Sep 17 00:00:00 2001
From: Victor Stinner <vstinner@python.org>
Date: Tue, 24 Mar 2020 17:12:19 +0100
Subject: [PATCH] fix PyThreadState_Clear(): don't decref frame

PyThreadState.frame is a borrowed reference, not a strong reference:
PyThreadState_Clear() must not call Py_CLEAR(tstate->frame).

Remove test_threading.test_warnings_at_exit(): we cannot warranty
that the Python thread state of daemon threads is cleared in a
reliable way during Python shutdown.

Fix is from gh#python/cpython!19137, released upstream in 3.7.8

Fixes: bsc#1188607
Patch: bsc1188607-pythreadstate_clear-decref.patch
---
 Include/pystate.h |  1 +
 Python/pystate.c  | 12 ++++++++++--
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/Include/pystate.h b/Include/pystate.h
index 1838fa40480..56e16336b46 100644
--- a/Include/pystate.h
+++ b/Include/pystate.h
@@ -88,6 +88,7 @@ typedef struct _ts {
     struct _ts *next;
     PyInterpreterState *interp;
 
+    /* Borrowed reference to the current frame (it can be NULL) */
     struct _frame *frame;
     int recursion_depth;
     char overflowed; /* The stack has overflowed. Allow 50 more calls
diff --git a/Python/pystate.c b/Python/pystate.c
index 71494daa471..cd3039e7666 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -409,11 +409,19 @@ _PyState_ClearModules(void)
 void
 PyThreadState_Clear(PyThreadState *tstate)
 {
-    if (Py_VerboseFlag && tstate->frame != NULL)
+    if (Py_VerboseFlag && tstate->frame != NULL) {
+        /* bpo-20526: After the main thread calls
+           _PyRuntimeState_SetFinalizing() in Py_FinalizeEx(), threads must
+           exit when trying to take the GIL. If a thread exit in the middle of
+           _PyEval_EvalFrameDefault(), tstate->frame is not reset to its
+           previous value. It is more likely with daemon threads, but it can
+           happen with regular threads if threading._shutdown() fails
+           (ex: interrupted by CTRL+C). */
         fprintf(stderr,
           "PyThreadState_Clear: warning: thread still has a frame\n");
+    }
 
-    Py_CLEAR(tstate->frame);
+    /* Don't clear tstate->frame: it is a borrowed reference */
 
     Py_CLEAR(tstate->dict);
     Py_CLEAR(tstate->async_exc);
-- 
2.45.0
openSUSE Build Service is sponsored by