-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
fix weak memory bug in TLS on Windows #124281
Conversation
r? @ChrisDenton rustbot has assigned @ChrisDenton. Use |
Ah yes, this is clearly a bug.
There was: the destructor may be called as soon as it is registered so I wanted to make sure that the key was set at that point, as You can fix this by skipping the TLS variable in |
Ah, makes sense. And in the Miri testcase, we don't have a thread-exit racing with a key-init so we wouldn't hit this. I have updated the code. |
I extended the Miri test to also cover this, that triggered the bug immediately: //! Regression test for <https://github.com/rust-lang/rust/issues/123583>.
use std::thread;
fn with_thread_local1() {
thread_local! { static X: Box<u8> = Box::new(0); }
X.with(|_x| {})
}
fn with_thread_local2() {
thread_local! { static Y: Box<u8> = Box::new(0); }
Y.with(|_y| {})
}
fn main() {
// Here we have two threads racing on initializing the thread-local
// and (on Windows without target_thread_local) adding it to the global dtor list.
let t = thread::spawn(with_thread_local1);
with_thread_local1();
t.join().unwrap();
// Here we have one thread running the destructors racing with another thread initializing a thread-local.
// The second thread adds a destructor that could be picked up by the first thread.
let t = thread::spawn(|| { /* immediately just run destructors */});
with_thread_local2(); // initialize thread-local
t.join().unwrap();
} |
Thank you very much, especially for the detailed comments! |
The regression tests are being added on the Miri side in rust-lang/miri#3501. |
Rollup of 6 pull requests Successful merges: - rust-lang#123316 (Test `#[unix_sigpipe = "inherit"]` with both `SIG_DFL` and `SIG_IGN`) - rust-lang#123794 (More DefineOpaqueTypes::Yes) - rust-lang#123881 (Bump Fuchsia versions) - rust-lang#124281 (fix weak memory bug in TLS on Windows) - rust-lang#124282 (windows fill_utf16_buf: explain the expected return value) - rust-lang#124308 (Add diagnostic item for `std::iter::Enumerate`) r? `@ghost` `@rustbot` modify labels: rollup
Rollup of 6 pull requests Successful merges: - rust-lang#123316 (Test `#[unix_sigpipe = "inherit"]` with both `SIG_DFL` and `SIG_IGN`) - rust-lang#123794 (More DefineOpaqueTypes::Yes) - rust-lang#123881 (Bump Fuchsia versions) - rust-lang#124281 (fix weak memory bug in TLS on Windows) - rust-lang#124282 (windows fill_utf16_buf: explain the expected return value) - rust-lang#124308 (Add diagnostic item for `std::iter::Enumerate`) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of rust-lang#124281 - RalfJung:win-tls, r=joboet fix weak memory bug in TLS on Windows We need to store the `key` *after* we register the dtor. Now I hope there isn't also some other reason why we have to actually register the dtor last... `@joboet` is there a reason you picked this particular order in rust-lang#102655? Fixes rust-lang#123583
We need to store the
key
after we register the dtor.Now I hope there isn't also some other reason why we have to actually register the dtor last... @joboet is there a reason you picked this particular order in #102655?
Fixes #123583