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

Undefined behavior when getting a value twice, and from another thread in between #21

Closed
Kestrer opened this issue Nov 14, 2020 · 0 comments

Comments

@Kestrer
Copy link
Contributor

Kestrer commented Nov 14, 2020

The following program:

let tls = std::sync::Arc::new(ThreadLocal::new());

let s = tls.get_or(|| "Hello World".to_owned());

let thread_tls = tls.clone();
std::thread::spawn(move || {
    thread_tls.get_or(String::new);
})
.join()
.unwrap();

tls.get().unwrap();

s;

Produces undefined behavior when run through Miri:

error: Undefined Behavior: trying to reborrow for SharedReadOnly, but parent tag <234426> does not have an appropriate item in the borrow stack
   --> src/main.rs:17:5
   |
17 |     s;
   |     ^ trying to reborrow for SharedReadOnly, but parent tag <234426> does not have an appropriate item in the borrow stack
   |
   = help: this indicates a potential bug in the program: it performed an invalid operation, but the rules it violated are still experimental
   = help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information

I think this is caused by moving the data to the current hash map in the get_slow path. If I do not move it to the current hash map and instead just get it directly, this UB does not occur.

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

1 participant