diff options
| author | joboet <jonasboettiger@icloud.com> | 2022-09-05 09:08:07 +0200 |
|---|---|---|
| committer | joboet <jonasboettiger@icloud.com> | 2022-09-05 09:08:07 +0200 |
| commit | 774cadfbfa3a1b0ae3a0f8b142b556124fd552b5 (patch) | |
| tree | dbc88e0875b4ad42a55dd6affd02bab3a3269ad9 /library/std/src/io/stdio.rs | |
| parent | 9358d09a55307e47bde0276e2390c603c35d5fb6 (diff) | |
| download | rust-774cadfbfa3a1b0ae3a0f8b142b556124fd552b5.tar.gz rust-774cadfbfa3a1b0ae3a0f8b142b556124fd552b5.zip | |
std: fix cleanup for uninitialized stdout (#101375)
Diffstat (limited to 'library/std/src/io/stdio.rs')
| -rw-r--r-- | library/std/src/io/stdio.rs | 25 |
1 files changed, 17 insertions, 8 deletions
diff --git a/library/std/src/io/stdio.rs b/library/std/src/io/stdio.rs index dd4ff4952fd..91cff3217d2 100644 --- a/library/std/src/io/stdio.rs +++ b/library/std/src/io/stdio.rs @@ -607,15 +607,24 @@ pub fn stdout() -> Stdout { } } +// Flush the data and disable buffering during shutdown +// by replacing the line writer by one with zero +// buffering capacity. pub fn cleanup() { - // Flush the data and disable buffering during shutdown - // by replacing the line writer by one with zero - // buffering capacity. - // We use try_lock() instead of lock(), because someone - // might have leaked a StdoutLock, which would - // otherwise cause a deadlock here. - if let Some(lock) = STDOUT.get().and_then(ReentrantMutex::try_lock) { - *lock.borrow_mut() = LineWriter::with_capacity(0, stdout_raw()); + let mut initialized = false; + let stdout = STDOUT.get_or_init(|| { + initialized = true; + ReentrantMutex::new(RefCell::new(LineWriter::with_capacity(0, stdout_raw()))) + }); + + if !initialized { + // The buffer was previously initialized, overwrite it here. + // We use try_lock() instead of lock(), because someone + // might have leaked a StdoutLock, which would + // otherwise cause a deadlock here. + if let Some(lock) = stdout.try_lock() { + *lock.borrow_mut() = LineWriter::with_capacity(0, stdout_raw()); + } } } |
