Skip to content

Commit 720aef4

Browse files
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. (cherry picked from commit fe997e1) Co-authored-by: Victor Stinner <[email protected]>
1 parent 0c5e0aa commit 720aef4

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a crash in the signal handler of the :mod:`faulthandler` module: no
2+
longer modify the reference count of frame objects. Patch by Victor Stinner.

Python/traceback.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,10 @@ dump_traceback(int fd, PyThreadState *tstate, int write_header)
799799
PUTS(fd, "Stack (most recent call first):\n");
800800
}
801801

802-
frame = PyThreadState_GetFrame(tstate);
802+
// Use a borrowed reference. Avoid Py_INCREF/Py_DECREF, since this function
803+
// can be called in a signal handler by the faulthandler module which must
804+
// not modify Python objects.
805+
frame = tstate->frame;
803806
if (frame == NULL) {
804807
PUTS(fd, "<no Python frame>\n");
805808
return;
@@ -808,17 +811,14 @@ dump_traceback(int fd, PyThreadState *tstate, int write_header)
808811
depth = 0;
809812
while (1) {
810813
if (MAX_FRAME_DEPTH <= depth) {
811-
Py_DECREF(frame);
812814
PUTS(fd, " ...\n");
813815
break;
814816
}
815817
if (!PyFrame_Check(frame)) {
816-
Py_DECREF(frame);
817818
break;
818819
}
819820
dump_frame(fd, frame);
820-
PyFrameObject *back = PyFrame_GetBack(frame);
821-
Py_DECREF(frame);
821+
PyFrameObject *back = frame->f_back;
822822

823823
if (back == NULL) {
824824
break;

0 commit comments

Comments
 (0)