Skip to content

Commit

Permalink
fix weak memory bug in TLS on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Apr 23, 2024
1 parent c8d19a9 commit dc5026c
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion library/std/src/sys/pal/windows/thread_local_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,15 @@ impl StaticKey {
panic!("out of TLS indexes");
}

self.key.store(key + 1, Release);
register_dtor(self);

// Release-storing the key needs to be the last thing we do.
// This is because in `fn key()`, other threads will do an acquire load of the key,
// and if that sees this write then it will entirely bypass the `InitOnce`. We thus
// need to establish synchronization through `key`. In particular that acquire load
// must happen-after the register_dtor above, to ensure the dtor actually runs!
self.key.store(key + 1, Release);

let r = c::InitOnceComplete(self.once.get(), 0, ptr::null_mut());
debug_assert_eq!(r, c::TRUE);

Expand Down

0 comments on commit dc5026c

Please sign in to comment.