Skip to content

Heavy usage of TLS causes stack overflow #136120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
errangutan opened this issue Jan 27, 2025 · 2 comments
Open

Heavy usage of TLS causes stack overflow #136120

errangutan opened this issue Jan 27, 2025 · 2 comments
Labels
A-panic Area: Panicking machinery A-thread-locals Area: Thread local storage (TLS) C-bug Category: This is a bug. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@errangutan
Copy link

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.

@errangutan errangutan added the C-bug Category: This is a bug. label Jan 27, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 27, 2025
@purplesyringa
Copy link
Contributor

dlclose is fundamentally broken for this reason among others. musl, for example, makes dlclose a no-op. A stack overflow instead of an abort is something we can fix, but I'd advise you to avoid unloading and reloading libraries in runtime.

@rustbot label +A-thread-locals +A-panic +T-libs -needs-triage

@rustbot rustbot added A-panic Area: Panicking machinery A-thread-locals Area: Thread local storage (TLS) T-libs Relevant to the library team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jan 27, 2025
@Noratrieb
Copy link
Member

On unix, when I repeatedly load and then unload my library,

Yeah, std does not really support being dlclosed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-panic Area: Panicking machinery A-thread-locals Area: Thread local storage (TLS) C-bug Category: This is a bug. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants