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

Is StdoutLock guranteed to be reentrant? #134698

Open
FeldrinH opened this issue Dec 23, 2024 · 1 comment
Open

Is StdoutLock guranteed to be reentrant? #134698

FeldrinH opened this issue Dec 23, 2024 · 1 comment
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@FeldrinH
Copy link

FeldrinH commented Dec 23, 2024

Location

https://doc.rust-lang.org/std/io/struct.Stdout.html#method.lock
https://doc.rust-lang.org/std/io/struct.Stderr.html#method.lock
https://doc.rust-lang.org/std/io/struct.Stdin.html#method.lock

Summary

Currently code like this is fine and a convenient way to lock stdout to prevent other threads from interrupting while printing is happening (this is especially useful when the printing happens deep inside some library/component, where StdoutLock can't be easily passed as an argument):

let stdout = io::stdout().lock();
println!("a");
println!("b");
println!("c");
drop(stdout);

As far as I understand, this works because Stdout::lock() is reentrant. However, the documentation does not mention that Stdout::lock() is reentrant.

Is Stdout::lock() guaranteed to be reentrant in all future Rust versions, or is this an implementation detail that should not be relied on?
Either way it would be nice if the documentation of Stdout::lock() made it clear what the expected behavior is and how much of it is an implementation detail.

The same question also applies to Stderr::lock() and Stdin::lock().

@FeldrinH FeldrinH added the A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools label Dec 23, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 23, 2024
@workingjubilee
Copy link
Member

As far as I understand, this works because Stdout::lock() is reentrant

Yes, it is a thin wrapper on a ReentrantLockGuard.

It seems reasonable to document this guarantee given that we broke a couple crates to maintain this:

@workingjubilee workingjubilee added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Dec 23, 2024
@jieyouxu jieyouxu removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants