Skip to content
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

get_slow with THREAD_GUARD can trigger: cannot access a Thread Local Storage value during or after destruction: AccessError #74

Open
gerwin3 opened this issue Mar 1, 2024 · 1 comment

Comments

@gerwin3
Copy link

gerwin3 commented Mar 1, 2024

I'm trying to get to the bottom of a weird crash I'm getting. I have minimal reproducible here:

https://github.com/gerwin3/thread-local-accesserror-repro/blob/main/src/main.rs

System:

  • Arch Linux x86_64 6.7.6-arch1-1
  • AMD Ryzen 9 5900X

To trigger the crash: close the window with CTRL + W.

The app uses egui and tracing-appender and the combination somehow triggers an AccessError (cannot access a Thread Local Storage value during or after destruction: AccessError) that eventually is caused by a function get_slow in thread_local:

THREAD_GUARD.with(|guard| guard.id.set(new.id));

My guess is that calling with on a thread local may not always be correct if the caller is running a destructor (?) in any case the docs of LocalKey::with seem to suggest so: https://doc.rust-lang.org/std/thread/struct.LocalKey.html#method.with

Not sure if I'm on the right track here. But since the documentation on this crate does not seem to suggest that ThreadLocal::get can panic in certain situations, and it seems it can actually panic in this case it seems this is a bug.

@aviramha
Copy link

aviramha commented Aug 3, 2024

We get this too:

(panic stuff)

   7: std::thread::local::LocalKey<T>::try_with
             at /rustc/0d8b3346a3992ab11ea35ff0fb95a6864b91f797/library/std/src/thread/local.rs:286:12
   8: std::thread::local::LocalKey<T>::with
             at /rustc/0d8b3346a3992ab11ea35ff0fb95a6864b91f797/library/std/src/thread/local.rs:262:9
   9: thread_local::thread_id::get
             at /home/meowjesty/.cargo/registry/src/index.crates.io-6f17d22bba15001f/thread_local-1.1.8/src/thread_id.rs:155:13
  10: thread_local::ThreadLocal<T>::get
             at /home/meowjesty/.cargo/registry/src/index.crates.io-6f17d22bba15001f/thread_local-1.1.8/src/lib.rs:185:24

(some more tracing stuff)

  17: tracing::span::Span::new_with
             at /home/meowjesty/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tracing-0.1.40/src/span.rs:448:9
  18: tracing::span::Span::new::{{closure}}
             at /home/meowjesty/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tracing-0.1.40/src/span.rs:437:44
  19: tracing_core::dispatcher::get_default
             at /home/meowjesty/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tracing-core-0.1.32/src/dispatcher.rs:391:16
 20: tracing::span::Span::new
             at /home/meowjesty/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tracing-0.1.40/src/span.rs:437:9
  21: mirrord_layer::socket::ops::fcntl
             at /home/meowjesty/dev/metalbear/mirrord/mirrord/layer/src/socket/ops.rs:786:1
  22: mirrord_layer::socket::hooks::fcntl_detour
             at /home/meowjesty/dev/metalbear/mirrord/mirrord/layer/src/socket/hooks.rs:198:15
  23: <unknown>
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
fatal runtime error: failed to initiate panic, error 5

It's an edge case, but perhaps need to change .with to try_with?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants