Repository navigation
Description
After the Rust toolchain got updated from 1.89.0 to 1.94.1 on Haiku OS, I noticed that my project now fails to build with error:
fatal runtime error: out of TLS keys, aborting
I suppose here "TLS" refers to thread-local storage rather than transport layer security 🤔
So, it would seem that the new version of rustc attempts to use more TLS slots (keys) than the operating system can provide.
From what I understand, the maximum number of TLS (thread-local storage) keys is equal to PTHREAD_KEYS_MAX, i.e., the "Maximum number of data keys that can be created by a process". It appears that this constant is platform-specific, but the POSIX standard defines the "Minimum Acceptable Value" as another constant called _POSIX_THREAD_KEYS_MAX – which is defined as 128.
See here:
https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/limits.h.html
So, this means that rustc probably should be very conservative with the number of TLS keys that it uses, because one must not assume that more than 128 TLS keys are available on any operating system. Nonetheless, it appears that Rust 1.94.1 violates the limit, whereas older Rust versions, such as 1.89.0, have been working flawlessly 😕
To make it clear, the exactly same code used to compile just fine with Rust 1.89.0 on Haiku OS. Also, my code does not even use thread_local! at all! So, I think, this must be a problem with the excessive use of thread_local! in rustc itself, not in my own code. Furthermore, the problem only started after Haiku OS updated their Rust toolchain from 1.89.0 to 1.94.1, so the "problematic" change must be anywhere in between these versions. I cannot say which version exactly broke the build, since rustup does not support Haiku OS yet, so I can only use the Rust version that Haiko OS itself provides (via pkgman).
Best regards.
Might be related:
#147590

Activity
chenyukang commentedon Apr 25, 2026
The error comes from
rust/library/std/src/sys/thread_local/key/unix.rs
Lines 25 to 33 in 597d9e4
On Haiku,
target_thread_localis not enabled, sothread_local!falls back to an OS/pthread-key based implementation. Whenpthread_key_createfails, std aborts withfatal runtime error: out of TLS keys.In 1.94.x,
parse_attribute_listfinalizes all parsers unconditionally, which touches each parser'sthread_local!state even if that attribute was never used. Since there are early and late parser sets, sumed to more than size of 200, this can easily exceed platforms that only provide the POSIX-minimum 128 TLS keys.There is a relevant fix: #151382.
That fix is not in 1.94.1, but it is contained in 1.95.0. So it would be very useful to test whether Rust 1.95.0 still reproduces this on Haiku.
If 1.95.0 works, this issue is likely fixed by #151382, or could be considered for a backport.
chenyukang commentedon Apr 25, 2026
But seems #151382 changed rustc only runs the finalizers for parsers that actually accepted an attribute. This avoids eagerly touching all parser TLS states, and should significantly reduce the number of pthread TLS keys used in normal compilations.
seems it still a mitigation of the problem. Each attribute parser still has its own potential
thread_local!state. If a compilation actually uses enough different parsed attributes, rustc may still initialize many TLS states.teor2345 commentedon Apr 25, 2026
It might be useful to add a "Christmas tree" UI test which contains as many attributes as possible, so we don't go over the TLS limit on a tier 1 / CI platform.
lordmulder commentedon Apr 25, 2026
Update to version 1.95.0 has been requested:
haikuports/haikuports#14042
chitao1234 commentedon Apr 29, 2026
I can confirm that this still reproduces for me on Haiku with Rust 1.95.0. I’m affected by the same
fatal runtime error: out of TLS keys, abortingfailure, and in my own testing the update from 1.94.1 to 1.95.0 did not fix it.4 remaining items
waddlesplash commentedon May 14, 2026
Note that OpenBSD increased their limit specifically due to Rust: openbsd/src@f90c654
markmi commentedon May 14, 2026
The FreeBSD source code still has just [WRONG: 128, not 256 nor] 256, not 512. FreeBSD's 1.94.0 bootstrap fails for the "out of TLS keys" in an armv7 poudriere(-devel) jail on aarch64 that supports armv7 as well, preventing building of 1.95.0 (lang/rust port). [correction: 1.93.0's part of boostraping 1.94.0 worked, a later 1.94.0 stage got the failure]. (The FreeBSD ports infrastructure does not cross build port-packages.)
It seems one has to back up to the last working bootstrap, then update/improve all following after that to get to a working build of 1.95.0 for armv7. [It looks like 1.95.0 on/for armv7 can build based on target_thread_local use. That likely does not solve the general "out of TLS keys" problem based on what OpenBSD and Haiku found.]
I'm unsure if changing the [correction: 256] to 512 is technically an ABI change for the FreeBSD OS. [It is.] If it is, that would be a big deal to change that in the FreeBSD source code. If an ABI issue, it could not [normally] change for
14.*-RELEASEor15.*-RELEASE, only main (later:16.*-RELEASEonce it is branched). [But the 1.95.0 compiler may not need such.]I will note that for some reason, only armv7 has the "out of TLS keys" build issue (for main, which is the context I'm familiar with). It is the only 32-bit FreeBSD OS variant for
15.*-RELEASEand main.14.*-RELEASEstill supports i386. lang/rust for i386 has not had any "out of TLS keys" problems for its builds, including for building 1.95.0. (I looked at the logs from the official port-package builder for 14.3-RELEASE.)(At this time no port-package builder is active for armv7: resource limitations caused that to stop. So testing is via personal builds by various folks.)
Increase PTHREAD_KEYS_MAX from 256 to 512
clausecker commentedon May 15, 2026
It seems like we have 256 keys, but at least one constant is still at 128. The system headers themselves are inconsistent here.
markmi commentedon May 15, 2026
Ahh:
pulkomandy commentedon May 15, 2026
markmi commentedon May 15, 2026
So _POSIX_THREAD_KEYS_MAX basically does look unused.
However, OpenBSD had 256 and increased PTHREAD_KEYS_MAX to 512 because of the "out of TLS keys" failures it was getting and Haiku is still reporting more "out of TLS keys" failures than before. Just having 1.95.0 does not seem to be a full/general solution, leaving the FreeBSD potential increase to 512 still at issue to may more work.
Increase PTHREAD_KEYS_MAX from 256 to 512
robertkirkman commentedon Jul 2, 2026
According to all currently known theory, if this form of this issue is currently affecting OpenBSD, FreeBSD and Haiku, it should also be reproducible in Android,
however, while I was able to reproduce a different form of this issue in the past in Rust 1.90 in Android,
I am no longer able to reproduce it in Rust 1.96 on Android and struggle to reproduce it with any projects that I'm attempting to build.
If you'd like to help me by making reproduction steps, please test them in OpenBSD, FreeBSD or Haiku, not Android, unless you are experienced with Android app development. This is because:
These steps showing the issue occurring in Haiku during the original report of this form of this issue are, potentially unfortunately, not currently reproducing it for me on Android:
git clone https://github.com/lordmulder/sponge-hash-aes256.git cd sponge-hash-aes256 cargo build --releasecould someone confirm whether those steps still reproduce this issue for them on their operating system or not, in Rust 1.96?
To summarize clearly, I'm basically worried that the issue could still be affecting Android in unknown edge cases, but might have ceased to be reproducible with the majority of tests that could previously reproduce it.
clausecker commentedon Jul 2, 2026
We fixed the FreeBSD issue by switching back to ELF ABI TLS. Using pthread TLS was a temporary workaround for an rtld (ld.so) bug in processing certain relocations that is now no longer needed.
workingjubilee commentedon Jul 2, 2026
There is a PR to address this problem but it is having trouble motivating it because its current implementation also involves potential performance regressions for tier 1 platforms.
Any weight on the "but it makes things work" side of the scale would be useful, and would be provided by identifying specific packages that don't work when built by current 1.96.1 or next week's 1.97.0 rustc, on a specific OS version, despite working on another OS. Perhaps @chitao1234 knows one?
As a side note: We do not aim for "strict POSIX conformance" in libstd's implementation. We try to have a good implementation, and our shared "Unix" code does expect POSIX conformance as a minimum. But in practice, we rely on target maintainers to contribute their expertise in designing workarounds when platforms inevitably conform to POSIX in... creative ways.
waddlesplash commentedon Jul 2, 2026
Is there a reason the ELF ABI TLS is not used on Haiku? (We don't support the "static" ELF TLS model, only the "dynamic" one, is that the problem here? -- But if rustc is compiled as a shared object, which it would be on Haiku as everything's a shared object, then presumably that's not the issue?) If there's some noncompliance in our implementation, it would be good to know so we can fix.
workingjubilee commentedon Jul 2, 2026
For that, it is very possible (I am saying this without doing the history dive to verify for sure, but based on experience of previous similar situations) that there's no really good reason and our contributors and reviewers simply weren't sure at the time of the PRs adding the target or modifying how we do thread-local storage. There could also have been a transient issue in codegen or linking that is no longer an issue. In any such a case, emulated/key-based TLS was likely seen as a "safe" fallback which makes sure things work, and then no one checked for years.
If you know that your system supports a TLS model better than the one we use, then feel free to send a PR modifying our upstream target specifications!
As a reminder, we don't run tests for tier 2 targets ourselves, but we do verify we can build them. That usually happens in a cross-build situation from Linux hosts, however, so we won't necessarily know if your rustc works on e.g. self-bootstrap with your system as host or any other TLS-stressing situations, and you may wish to verify that with a local patch first.
chitao1234 commentedon Jul 8, 2026
Could not reproduce with ordinary crates any more, weird, maybe I accidentally used system rust 1.94.1 last time. Only thing that can make latest rustc crash was these two synthetic crate I created: haiku-macro-zoo and haiku-tls-key-trigger . The first one uses a lot of proc macro crates from crates.io, and the second one uses a bad behaving proc macro create that triggers the allocation of a lot of tls keys inside rustc.
robertkirkman commentedon Jul 25, 2026
Thank you very much, that is exactly what helps me,
I can reproduce the issue using that, and that confirms for me (and for anyone else) that this issue #155758 is the Haiku version of #147590 which is for Android, and that the issue is still reproducible in Rustc 1.97.1 when Rustc is running on Android and your test project is built.