Skip to content

_Py_DumpStack() is not signal-safe the first time it's called #137185

@colesbury

Description

@colesbury

Bug report

The _Py_DumpStack() should be signal-safe, because we typically call it from signal handlers.

This shows up as an error "signal-unsafe call inside of a signal" if you run ./python -m test test_faulthandler -m test_enable_fd -v with TSan enabled.

From the man page:

backtrace() and backtrace_symbols_fd() don't call malloc()
explicitly, but they are part of libgcc, which gets loaded
dynamically when first used
. Dynamic loading usually triggers
a call to malloc(3). If you need certain calls to these two
functions to not allocate memory (in signal handlers, for
example), you need to make sure libgcc is loaded beforehand.

(I think backtrace() is in libc, but it calls __libc_unwind_link_get(), which is in libgcc)

If we are going to call backtrace() from a signal handler, we should ensure that libgcc is loaded when we install the signal handler. We can do this by calling backtrace() early, at signal handler installation time, to force libgcc to be loaded.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions