rust-lang / rust-lang/rust

Heavy usage of TLS causes stack overflow

Open
#136,120 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-panic A-thread-locals C-bug T-libs
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by investigating the std TLS initialization path described in the report, including pthread_key_create and the absence of pthread_key_delete. Reproduce repeated dynamic-library load and unload cycles and trace the EAGAIN-to-recursive-panic path. Done means repeated cycles no longer exhaust pthread keys or end in a stack overflow.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
operating-systems
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.