Skip to content

Commit fe997e1

Browse files
authored
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.
1 parent 52bdda5 commit fe997e1

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
@@ -796,7 +796,10 @@ dump_traceback(int fd, PyThreadState *tstate, int write_header)
796796
PUTS(fd, "Stack (most recent call first):\n");
797797
}
798798

799-
frame = PyThreadState_GetFrame(tstate);
799+
// Use a borrowed reference. Avoid Py_INCREF/Py_DECREF, since this function
800+
// can be called in a signal handler by the faulthandler module which must
801+
// not modify Python objects.
802+
frame = tstate->frame;
800803
if (frame == NULL) {
801804
PUTS(fd, "<no Python frame>\n");
802805
return;
@@ -805,17 +808,14 @@ dump_traceback(int fd, PyThreadState *tstate, int write_header)
805808
depth = 0;
806809
while (1) {
807810
if (MAX_FRAME_DEPTH <= depth) {
808-
Py_DECREF(frame);
809811
PUTS(fd, " ...\n");
810812
break;
811813
}
812814
if (!PyFrame_Check(frame)) {
813-
Py_DECREF(frame);
814815
break;
815816
}
816817
dump_frame(fd, frame);
817-
PyFrameObject *back = PyFrame_GetBack(frame);
818-
Py_DECREF(frame);
818+
PyFrameObject *back = frame->f_back;
819819

820820
if (back == NULL) {
821821
break;

0 commit comments

Comments
 (0)