Skip to content

Heavy usage of TLS causes stack overflow #136120

Open
@errangutan

Description

@errangutan

i wrote a dynamic library which uses tracing, which internally uses stds TLS implemention.
On unix, when I repeatedly load and then unload my library, eventually (after maybe 10 to 20 iterations) the program encounters a stack overflow.

After some research I found that this was due to the following flow:

pthread_key_create is called when TLS is initialized each time my library is loaded and my tracing logger is used.

pthread_key_delete is never called, there is no cleanup in this area.

Eventually pthread_key_create returns EAGAIN, which causes a panic.

Within the panic, the number of panics for the thread is tracked using TLS so when increase is called, we encounter a new panic (due to EAGAIN), which in turn will panic for the same reason.

We enter an endless loop of panics which eventually triggers a stack overflow.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-panicArea: Panicking machineryA-thread-localsArea: Thread local storage (TLS)C-bugCategory: This is a bug.T-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions