-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Writing to stdout
with write()
changed behavior between version 1.47 and >= 1.48
#83528
Comments
Thanks for your detailed report!
Agreed. Marking this as low priority. |
The basic issue at play here is that, in line buffering logic, the buffer will generally attempt to only forward whole lines to the underlying writer; the only exception is when the line length exceeds the size of the buffer. Once it forwards the line, it can't attempt any more fallible operations, because it must return an
However, there's an interesting issue at play here. In the sample code, the This is technically unrelated to #72808; that PR will only fix this issue when stdout is in buffered writing mode. However, it's fairly easy to add the logic, and it makes sense that it should be present, so I'm writing a separate PR for it. |
@Lucretiel #78515 should provide the necessary control to solve this issue by switching stdout to immediate mode, right? Edit: silly question, this already is mentioned in OP
|
We talked about this in the @rust-lang/libs meeting today. This is behavior we care about, and we think it's valuable to work on stdout buffering (including the ongoing work in issues like #78515 and similar). However:
Given this, we don't plan to track this as a regression. We'd encourage people to get involved with the stdout buffering rework to improve the behavior. |
I'm not sure this is worth reporting, but it has caused confusion at least once, so maybe it makes sense to be documented somewhere.
When writing to
stdout
withwrite()
, write will return smaller number bytes than directwrite()
call would do in some cases. Here is example code:Rustc 1.47 gives expected result:
This is also in accordance with similar programs written in C, Python and Rust using
libc::write()
.However rustc 1.48 and greater return this:
This is technically not a bug, but IMHO it is unexpected behavior. Rust docs says this:
I'm aware "wrapped object" here is some internal
std
library structure, but most people would expect it should behave similarly to underlying systemwrite()
.man 2 write
says:As I understand this, returning less than count should be affected only by OS state and not by content of data (newline in this case).
This behavior seems to be introduced with PR #72808, and pending PR #78515 seems to revert behavior to expected.
Just a disclaimer, I do not condone use of unchecked
write()
. Reason for reporting this is inconsistency betweenio::stdout().write()
and similar approaches.The text was updated successfully, but these errors were encountered: