Skip to content
This repository was archived by the owner on Jun 30, 2025. It is now read-only.
This repository was archived by the owner on Jun 30, 2025. It is now read-only.

stacktrace_libunwind-inl.h: reentrancy check should use a thread-local instead of a global variable #160

@mpercy

Description

@mpercy

At the time of writing, in the implementation of google::GetStackTrace() in src/stacktrace_libunwind-inl.h there is a global variable that enforces that only one thread may invoke libunwind at a time. However, libunwind is thread-safe. The comment above the variable declaration indicates that this is to protect against reentrancy issues:

// Sometimes, we can try to get a stack trace from within a stack
// trace, because libunwind can call mmap (maybe indirectly via an
// internal mmap based memory allocator), and that mmap gets trapped
// and causes a stack-trace request.  If were to try to honor that
// recursive request, we'd end up with infinite recursion or deadlock.
// Luckily, it's safe to ignore those subsequent traces.  In such
// cases, we return 0 to indicate the situation.
static bool g_now_entering = false;

Instead of using a global variable, it would be much better to use a thread-local variable to protect against these reentrancy issues. That should provide the needed reentrancy protection while allowing multiple threads to get stack traces at the same time. It would also allow for the removal of the atomic CAS operations on the g_now_entering variable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions