# What is the current safety story for library-based stackful coroutines?

**URL:** <https://internals.rust-lang.org/t/what-is-the-current-safety-story-for-library-based-stackful-coroutines/14863>\
**Category:** Unsafe Code Guidelines\
**Created:** [June 10, 2021, 1:58pm UTC](https://internals.rust-lang.org/t/what-is-the-current-safety-story-for-library-based-stackful-coroutines/14863 "2021-06-10T13:58:26Z")\
**Posts on this page:** 3\
**Page:** 2

<div class="post-metadata">

**Author:** ![Matthias247](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/matthias247/32/5734_2.png) [@Matthias247](https://internals.rust-lang.org/u/Matthias247)\
**Post date:** [July 2, 2021, 5:12pm UTC](https://internals.rust-lang.org/t/what-is-the-current-safety-story-for-library-based-stackful-coroutines/14863/21 "2021-07-02T17:12:52Z")

</div>

Maybe? I think all the `std` use of TLS should be fine. It's either optimizations, or really identifies the current thread.

Libraries which use TLS to identify a logical flow context (e.g. a logging/tracing context) might however indeed break and would not be compatible with this approach. I guess it would be fair to say that's would be an issue with the coroutine library and not with the TLS usage of those libraries.

---

<div class="post-metadata">

**Author:** ![ibraheemdev](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ibraheemdev/32/13406_2.png) [@ibraheemdev](https://internals.rust-lang.org/u/ibraheemdev)\
**Post date:** [October 22, 2021, 10:58pm UTC](https://internals.rust-lang.org/t/what-is-the-current-safety-story-for-library-based-stackful-coroutines/14863/22 "2021-10-22T22:58:48Z")

</div>

May could make all of it's functions unsafe, and then have an internal module that provides safe versions, not meant to be used except through a macro that requires a safety assertion:

```rust
// SAFETY: we don't use TLS
#[may::safe]
unsafe mod may {}

// ===>
// mod may {
// pub use may::_internal_unsafe_dont_use::*;
// }

```

The program wide safety assertion is still a footgun, but it's better than a note in the docs.

This would be better with custom lints:

```rust
#![forbid(may::coroutine_tls_access)]

// SAFETY: we can't use TLS
// because we enabled the `no_tls` lint
#[may::safe]
unsafe mod may {}

```

Where `may::coroutine_tls_access` is a lint that warns on any use of `LocalKey`.

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [December 22, 2024, 5:04pm UTC](https://internals.rust-lang.org/t/what-is-the-current-safety-story-for-library-based-stackful-coroutines/14863/23 "2024-12-22T17:04:09Z")

</div>

Closed on December 22, 2024, 5:04pm UTC

This topic was automatically closed 540 days after the last reply. New replies are no longer allowed.

[Previous page](https://internals.rust-lang.org/t/what-is-the-current-safety-story-for-library-based-stackful-coroutines/14863.md?page=1)
