diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2020-03-12 11:39:30 -0700 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2020-03-20 07:34:56 -0700 |
| commit | 5edaa7eefd76d4996dcf85dfc1c1a3f737087257 (patch) | |
| tree | 23b94ea5de5fe2856d7f020621e0c0ea1f9734c9 /src/libstd/sys/windows/mutex.rs | |
| parent | 23de8275c9b5e5812dc54a12bdba6d80870d9dc8 (diff) | |
| download | rust-5edaa7eefd76d4996dcf85dfc1c1a3f737087257.tar.gz rust-5edaa7eefd76d4996dcf85dfc1c1a3f737087257.zip | |
Fix abort-on-eprintln during process shutdown
This commit fixes an issue where if `eprintln!` is used in a TLS destructor it can accidentally cause the process to abort. TLS destructors are executed after `main` returns on the main thread, and at this point we've also deinitialized global `Lazy` values like those which store the `Stderr` and `Stdout` internals. This means that despite handling TLS not being accessible in `eprintln!`, we will fail due to not being able to call `stderr()`. This means that we'll double-panic quickly because panicking also attempt to write to stderr. The fix here is to reimplement the global stderr handle to avoid the need for destruction. This avoids the need for `Lazy` as well as the hidden panic inside of the `stderr` function. Overall this should improve the robustness of printing errors and/or panics in weird situations, since the `stderr` accessor should be infallible in more situations.
Diffstat (limited to 'src/libstd/sys/windows/mutex.rs')
| -rw-r--r-- | src/libstd/sys/windows/mutex.rs | 6 |
1 files changed, 3 insertions, 3 deletions
diff --git a/src/libstd/sys/windows/mutex.rs b/src/libstd/sys/windows/mutex.rs index 281eb294c65..63dfc640908 100644 --- a/src/libstd/sys/windows/mutex.rs +++ b/src/libstd/sys/windows/mutex.rs @@ -109,7 +109,7 @@ impl Mutex { 0 => {} n => return n as *mut _, } - let mut re = box ReentrantMutex::uninitialized(); + let re = box ReentrantMutex::uninitialized(); re.init(); let re = Box::into_raw(re); match self.lock.compare_and_swap(0, re as usize, Ordering::SeqCst) { @@ -157,11 +157,11 @@ unsafe impl Send for ReentrantMutex {} unsafe impl Sync for ReentrantMutex {} impl ReentrantMutex { - pub fn uninitialized() -> ReentrantMutex { + pub const fn uninitialized() -> ReentrantMutex { ReentrantMutex { inner: UnsafeCell::new(MaybeUninit::uninit()) } } - pub unsafe fn init(&mut self) { + pub unsafe fn init(&self) { c::InitializeCriticalSection((&mut *self.inner.get()).as_mut_ptr()); } |
