Skip to content

Rustc version 1.94.1 fails with error "fatal runtime error: out of TLS keys, aborting" #155758

Description

@lordmulder

View all comments

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

out of TLS keys, aborting (screenshot) Image

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

  1. added
    C-bug
    on Apr 24, 2026
  2. added
    needs-triage
    on Apr 24, 2026
  3. added
    A-thread-locals
    T-compiler
    O-haiku
    I-crash
    regression-from-stable-to-stable
    on Apr 25, 2026
  4. added
    I-prioritize
    on Apr 25, 2026
  5. chenyukang commented on Apr 25, 2026

    @chenyukang
    Member

    The error comes from

    #[inline]
    pub fn create(dtor: Option<unsafe extern "C" fn(*mut u8)>) -> Key {
    let mut key = 0;
    if unsafe { libc::pthread_key_create(&mut key, mem::transmute(dtor)) } != 0 {
    rtabort!("out of TLS keys");
    }
    key
    }

    On Haiku, target_thread_local is not enabled, so thread_local! falls back to an OS/pthread-key based implementation. When pthread_key_create fails, std aborts with fatal runtime error: out of TLS keys.

    In 1.94.x, parse_attribute_list finalizes all parsers unconditionally, which touches each parser's thread_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.

  6. chenyukang commented on Apr 25, 2026

    @chenyukang
    Member

    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.

  7. teor2345 commented on Apr 25, 2026

    @teor2345
    Member

    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.

  8. lordmulder commented on Apr 25, 2026

    @lordmulder
    Author

    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.

    Update to version 1.95.0 has been requested:
    haikuports/haikuports#14042

  9. removed
    I-prioritize
    needs-triage
    on Apr 27, 2026
  10. chitao1234 commented on Apr 29, 2026

    @chitao1234
    Contributor

    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, aborting failure, and in my own testing the update from 1.94.1 to 1.95.0 did not fix it.

  11. 4 remaining items

  12. waddlesplash commented on May 14, 2026

    @waddlesplash

    Note that OpenBSD increased their limit specifically due to Rust: openbsd/src@f90c654

    the rust libstd emutls version is using one pthread_key per thread-variable. it
    means the maximum number of thread-variable at a time is PTHREAD_KEYS_MAX.

    with recent rustc 1.91.0, we start hiting the limit while compiling some
    programs (like sysutils/rustic).

    ok robert@
    "Let's go 512 and hope someone eventually does proper TLS" deraadt@

  13. markmi commented on May 14, 2026

    @markmi

    You may still want to fix this on Rust side if you aim for strict POSIX conformance. I don't know if
    FreeBSD and NetBSD have issues or if they can use other TLS storage methods to avoid this.

    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.*-RELEASE or 15.*-RELEASE, only main (later: 16.*-RELEASE once 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.*-RELEASE and main. 14.*-RELEASE still 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.)

  14. clausecker commented on May 15, 2026

    @clausecker

    You may still want to fix this on Rust side if you aim for strict POSIX conformance. I don't know if FreeBSD and NetBSD have issues or if they can use other TLS storage methods to avoid this.

    The FreeBSD source code still has just 128, not 256, nor 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). (My understanding is that 1.93.0 bootstrap had the same issue preventing building of 1.94.0 . I' unsure for 1.92.0 bootstrap used for build 1.93.0 .) (The FreeBSD ports infrastructure does not cross build port-packages.)

    It seems like we have 256 keys, but at least one constant is still at 128. The system headers themselves are inconsistent here.

  15. markmi commented on May 15, 2026

    @markmi

    You may still want to fix this on Rust side if you aim for strict POSIX conformance. I don't know if FreeBSD and NetBSD have issues or if they can use other TLS storage methods to avoid this.

    The FreeBSD source code still has just 128, not 256, nor 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). (My understanding is that 1.93.0 bootstrap had the same issue preventing building of 1.94.0 . I' unsure for 1.92.0 bootstrap used for build 1.93.0 .) (The FreeBSD ports infrastructure does not cross build port-packages.)

    It seems like we have 256 keys, but at least one constant is still at 128. The system headers themselves are inconsistent here.

    Ahh:

    # grep -r "define.*THREAD_KEYS_MAX" /usr/include/ /usr/src/
    /usr/include/limits.h:#define	_POSIX_THREAD_KEYS_MAX	128
    /usr/include/pthread.h:#define	PTHREAD_KEYS_MAX			256
    /usr/include/unistd.h:#define	_SC_THREAD_KEYS_MAX	86 /* user */
    /usr/src/include/unistd.h:#define	_SC_THREAD_KEYS_MAX	86 /* user */
    /usr/src/include/limits.h:#define	_POSIX_THREAD_KEYS_MAX	128
    /usr/src/include/pthread.h:#define	PTHREAD_KEYS_MAX			256
    
    With:
            case _SC_THREAD_KEYS_MAX:
                    return (PTHREAD_KEYS_MAX);
    
  16. pulkomandy commented on May 15, 2026

    @pulkomandy
  17. markmi commented on May 15, 2026

    @markmi

    This looks correct to me, _POSIX_THREAD_KEYS_MAX is only the minimal value that POSIX requires to support. It should be unused by FreeBSD itself, but available to software. See the opengroup specification for limits.h which explains this.

    As long as FreeBSD uses PTHREAD_KEYS_MAX as the actual limit, you are fine.

    # grep -r _POSIX_THREAD_KEYS_MAX /usr/include/ /usr/src/
    /usr/include/limits.h:#define	_POSIX_THREAD_KEYS_MAX	128
    /usr/src/contrib/netbsd-tests/include/t_limits.c:#ifdef	_POSIX_THREAD_KEYS_MAX
    /usr/src/contrib/netbsd-tests/include/t_limits.c:	{ "_POSIX_THREAD_KEYS_MAX",	_POSIX_THREAD_KEYS_MAX,		128  },
    /usr/src/usr.bin/getconf/limits.gperf:_POSIX_THREAD_KEYS_MAX, _POSIX_THREAD_KEYS_MAX
    /usr/src/include/limits.h:#define	_POSIX_THREAD_KEYS_MAX	128
    
    # grep THREAD_KEYS_MAX /usr/src/usr.bin/getconf/limits.gperf
    _POSIX_THREAD_KEYS_MAX, _POSIX_THREAD_KEYS_MAX
    

    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.

  18. robertkirkman commented on Jul 2, 2026

    @robertkirkman

    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:

    • If exactly the same reproduction steps that trigger this issue on a different operating system also trigger it on Android, that would help me to understand that this issue in particular does also affect Android
    • If the Rust installation for Android used is derived from Termux's, there is currently a patch in it that must be manually removed and the entire Rust installation recompiled in order to reset Termux's Rust back closer to the upstream behavior regarding this issue, in order to troubleshoot it or collect logs https://github.com/termux/termux-packages/blob/35c0033a8c4b70d8ef2c9091af8c048f7f8419d8/packages/rust/0007-use-emulated-tls-on-android.patch I have done that locally in my attempts to reproduce this issue, but the steps are tedious and not straightforward for people who are not already focused on Android.

    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 --release

    could 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.

  19. clausecker commented on Jul 2, 2026

    @clausecker

    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.

  20. workingjubilee commented on Jul 2, 2026

    @workingjubilee
    Member

    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.

  21. waddlesplash commented on Jul 2, 2026

    @waddlesplash

    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.

  22. workingjubilee commented on Jul 2, 2026

    @workingjubilee
    Member

    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.

  23. chitao1234 commented on Jul 8, 2026

    @chitao1234
    Contributor

    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.

  24. robertkirkman commented on Jul 25, 2026

    @robertkirkman

    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.

    ~/haiku-tls-key-trigger $ cargo build
       Compiling tls_bomb_macro v0.1.0 (/data/data/com.termux/files/home/haiku-tls-key-trigger/macro)
       Compiling trigger v0.1.0 (/data/data/com.termux/files/home/haiku-tls-key-trigger/trigger)
    fatal runtime error: out of TLS keys, aborting
    error: could not compile `trigger` (bin "trigger")
    
    Caused by:
      process didn't exit successfully: `rustc --crate-name trigger --edition=2021 trigger/src/main.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=106 --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 --check-cfg 'cfg(docsrs,test)' --check-cfg 'cfg(feature, values())' -C metadata=b9f369ce6bcf6bc2 -C extra-filename=-4dbc4bc8489c11ad --out-dir /data/data/com.termux/files/home/haiku-tls-key-trigger/target/debug/deps -C incremental=/data/data/com.termux/files/home/haiku-tls-key-trigger/target/debug/incremental -L dependency=/data/data/com.termux/files/home/haiku-tls-key-trigger/target/debug/deps --extern tls_bomb_macro=/data/data/com.termux/files/home/haiku-tls-key-trigger/target/debug/deps/libtls_bomb_macro-dd742cb795e1ce8f.so` (signal: 6, SIGABRT: process abort signal)
    ~/haiku-tls-key-trigger $ 
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-thread-localsC-bugI-crashO-haikuT-compilerregression-from-stable-to-stable

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions