Skip to content

[3.14] gh-137185: Fix _Py_DumpStack() async signal safety (gh-137187) #137206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 3.14
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Include/internal/pycore_traceback.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ extern int _Py_WriteIndentedMargin(int, const char*, PyObject *);
extern int _Py_WriteIndent(int, PyObject *);

// Export for the faulthandler module
PyAPI_FUNC(void) _Py_InitDumpStack(void);
PyAPI_FUNC(void) _Py_DumpStack(int fd);

#ifdef __cplusplus
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a potential async-signal-safety issue in :mod:`faulthandler` when
printing C stack traces.
5 changes: 5 additions & 0 deletions Modules/faulthandler.c
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,11 @@ faulthandler_enable(void)
}
#endif

// gh-137185: Initialize C stack trace dumping outside of the signal
// handler. Specifically, we call backtrace() to ensure that libgcc is
// dynamically loaded outside of the signal handler.
_Py_InitDumpStack();

for (size_t i=0; i < faulthandler_nsignals; i++) {
fault_handler_t *handler;
int err;
Expand Down
10 changes: 10 additions & 0 deletions Python/traceback.c
Original file line number Diff line number Diff line change
Expand Up @@ -1327,3 +1327,13 @@ _Py_DumpStack(int fd)
PUTS(fd, " <cannot get C stack on this system>\n");
}
#endif

void
_Py_InitDumpStack(void)
{
#ifdef CAN_C_BACKTRACE
// gh-137185: Call backtrace() once to force libgcc to be loaded early.
void *callstack[1];
(void)backtrace(callstack, 1);
#endif
}
Loading