[Python-checkins] bpo-44449: faulthandler don't modify frame refcnt (GH-27850)

vstinner webhook-mailer at python.org
Mon Aug 30 09:24:47 EDT 2021


https://github.com/python/cpython/commit/fe997e1a67835a929705c8c305d41c4d7dd326e3
commit: fe997e1a67835a929705c8c305d41c4d7dd326e3
branch: 3.10
author: Victor Stinner <vstinner at python.org>
committer: vstinner <vstinner at python.org>
date: 2021-08-30T15:24:39+02:00
summary:

bpo-44449: faulthandler don't modify frame refcnt (GH-27850)

Fix a crash in the signal handler of the faulthandler module: no
longer modify the reference count of frame objects.

files:
A Misc/NEWS.d/next/Library/2021-08-20-11-30-52.bpo-44449.1r2-lS.rst
M Python/traceback.c

diff --git a/Misc/NEWS.d/next/Library/2021-08-20-11-30-52.bpo-44449.1r2-lS.rst b/Misc/NEWS.d/next/Library/2021-08-20-11-30-52.bpo-44449.1r2-lS.rst
new file mode 100644
index 00000000000000..52f01541fca6a7
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-08-20-11-30-52.bpo-44449.1r2-lS.rst
@@ -0,0 +1,2 @@
+Fix a crash in the signal handler of the :mod:`faulthandler` module: no
+longer modify the reference count of frame objects. Patch by Victor Stinner.
diff --git a/Python/traceback.c b/Python/traceback.c
index f7dc5ad6864762..88ffc01c6eca3a 100644
--- a/Python/traceback.c
+++ b/Python/traceback.c
@@ -796,7 +796,10 @@ dump_traceback(int fd, PyThreadState *tstate, int write_header)
         PUTS(fd, "Stack (most recent call first):\n");
     }
 
-    frame = PyThreadState_GetFrame(tstate);
+    // Use a borrowed reference. Avoid Py_INCREF/Py_DECREF, since this function
+    // can be called in a signal handler by the faulthandler module which must
+    // not modify Python objects.
+    frame = tstate->frame;
     if (frame == NULL) {
         PUTS(fd, "<no Python frame>\n");
         return;
@@ -805,17 +808,14 @@ dump_traceback(int fd, PyThreadState *tstate, int write_header)
     depth = 0;
     while (1) {
         if (MAX_FRAME_DEPTH <= depth) {
-            Py_DECREF(frame);
             PUTS(fd, "  ...\n");
             break;
         }
         if (!PyFrame_Check(frame)) {
-            Py_DECREF(frame);
             break;
         }
         dump_frame(fd, frame);
-        PyFrameObject *back = PyFrame_GetBack(frame);
-        Py_DECREF(frame);
+        PyFrameObject *back = frame->f_back;
 
         if (back == NULL) {
             break;



More information about the Python-checkins mailing list