diff options
| author | Yuki Okushi <jtitor@2k36.org> | 2022-09-06 08:36:05 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-09-06 08:36:05 +0900 |
| commit | ff124c6ae8bc34f73431ff938656f8dae9ed5e0d (patch) | |
| tree | 4dcc3b0b9ba69a5276ea29043b5b3cfc3efa650c | |
| parent | 957b44a13c0a2eccacdbf917bff78eb625b0bd54 (diff) | |
| parent | 774cadfbfa3a1b0ae3a0f8b142b556124fd552b5 (diff) | |
| download | rust-ff124c6ae8bc34f73431ff938656f8dae9ed5e0d.tar.gz rust-ff124c6ae8bc34f73431ff938656f8dae9ed5e0d.zip | |
Rollup merge of #101404 - joboet:always_cleanup_stdout, r=joshtriplett
Fix cleanup for uninitialized stdout Fixes #101375 by disabling buffering even if the buffer was not initialized yet.
| -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()); + } } } |
