Heavy usage of TLS causes stack overflow
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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