Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

print! does not work after shutdown if no previous print occurred #101375

Closed
joboet opened this issue Sep 3, 2022 · 1 comment · Fixed by #101404
Closed

print! does not work after shutdown if no previous print occurred #101375

joboet opened this issue Sep 3, 2022 · 1 comment · Fixed by #101404
Labels
C-bug Category: This is a bug. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@joboet
Copy link
Member

joboet commented Sep 3, 2022

I tried this code:

fn main() {
    extern "C" fn bye() {
        print!("hello, world!");
    }
    unsafe { libc::atexit(bye) };
}

I expected to see this happen:

The code should print "hello, world!", which it does if any print statement occurred before it (see the test).

Instead, this happened:

Nothing is printed to standard output.

The bug is in the cleanup function that disables buffering during shutdown:

pub fn cleanup() {
if let Some(instance) = STDOUT.get() {
// 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) = Pin::static_ref(instance).try_lock() {
*lock.borrow_mut() = LineWriter::with_capacity(0, stdout_raw());
}
}
}

If the buffer has not been initialized yet, that will not occur, so the print statement will initialize the buffer in the normal fashion and it will not be flushed.

Meta

rustc --version --verbose:

rustc 1.63.0 (4b91a6ea7 2022-08-08)
binary: rustc
commit-hash: 4b91a6ea7258a947e59c6522cd5898e7c0a6a88f
commit-date: 2022-08-08
host: aarch64-apple-darwin
release: 1.63.0
LLVM version: 14.0.5
@joboet joboet added the C-bug Category: This is a bug. label Sep 3, 2022
@joboet

This comment was marked as outdated.

@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Sep 3, 2022
joboet added a commit to joboet/rust that referenced this issue Sep 5, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Sep 5, 2022
…shtriplett

Fix cleanup for uninitialized stdout

Fixes rust-lang#101375 by disabling buffering even if the buffer was not initialized yet.
JohnTitor added a commit to JohnTitor/rust that referenced this issue Sep 5, 2022
…shtriplett

Fix cleanup for uninitialized stdout

Fixes rust-lang#101375 by disabling buffering even if the buffer was not initialized yet.
@bors bors closed this as completed in ff124c6 Sep 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants