Skip to content

Commit dc5026c

Browse files
committed
fix weak memory bug in TLS on Windows
1 parent c8d19a9 commit dc5026c

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

library/std/src/sys/pal/windows/thread_local_key.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,15 @@ impl StaticKey {
141141
panic!("out of TLS indexes");
142142
}
143143

144-
self.key.store(key + 1, Release);
145144
register_dtor(self);
146145

146+
// Release-storing the key needs to be the last thing we do.
147+
// This is because in `fn key()`, other threads will do an acquire load of the key,
148+
// and if that sees this write then it will entirely bypass the `InitOnce`. We thus
149+
// need to establish synchronization through `key`. In particular that acquire load
150+
// must happen-after the register_dtor above, to ensure the dtor actually runs!
151+
self.key.store(key + 1, Release);
152+
147153
let r = c::InitOnceComplete(self.once.get(), 0, ptr::null_mut());
148154
debug_assert_eq!(r, c::TRUE);
149155

0 commit comments

Comments
 (0)