-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Make write(IO, Char) actually return the amount of printed bytes instead of the attempted written bytes. #56980
Merged
+8
−3
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This currently unconditionally advances the given character, but what happens in case the first
write
fails, and the second succeeds? Now there's suddenly a torn write involved here, and even though you can theoretically know that not all of the givenChar
has been written (e.g. getting a return value of3
when a 4-byteChar
is passed), you still wouldn't know which byte was dropped.I think it would be good to return after the first failing write, so that it's at least knowable that a valid prefix has been written (if the return value is nonzero).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any Julia IO type where writing a byte can fail, return zero, and then succeed, without some error being thrown?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For example, writing a byte with TranscodingStreams.jl will either return 1 or throw an error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, a non-blocking buffered IO whose buffer is temporarily full, for example. I don't know whether there currently is such a type in the ecosystem, but the point is that it could exist and would be a valid
IO
, as far as I can tell.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's a (slightly contrived) example:
It's a bit awkward to do this with an
IOBuffer
, but the principle is the same for someIO
type that has an actual read-end that's distinct from the write end. For arbitrary I/O, it's usually preferrable to drop data on the write end and retry later once the buffer is ready to send again. With the current behavior, the writer wouldn't know what to try to retransmit over the I/O, since it's impossible to know which byte(s) of theChar
was/were not transmitted correctly. Effectively, the number returned bywrite
becomes irrelevant, and only matters when it matchessizeof(Char)
- at which point we might as well only returntrue
/false
. If we instead abort as soon as any internalwrite
fails, we know that at least a correct prefix of theChar
(or any data, in the general case) was returned, and we can retry with only the data that we haven't attempted to transmit at all yet.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think write ever errors for us, given asyncio and other stuff?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does, e.g. trying to write to a read-only stream.
write
itself has a synchronous API, i.e. it is (task-)blocking.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is interesting historical data suggesting that some implementations of libc
write
were indeed able to return0
: https://stackoverflow.com/a/41970485For quite a bunch of kinds of files, the behavior is unspecified, so more or less anything goes either way 🤷
Right, and for a non-blocking buffered IO it would be incredibly awkward to throw actual errors just because it's full. That possibility would be incredibly detrimental in the common case of success. I admit having
0
signal that is quite a bad API though. I guess this is yet-another case something like aResult{Int, Err}
sum type would be nice, to distinguish success from errors 🤔Maybe let me put it another way - would this be a valid
IO
subtype (barring some other missing methods)?You could get very fancy and record which writes succeeded & which ones failed for introspection later on, or do some more complicated scheme for deciding when exactly it "fails" to write anything. This kind of type would be incredibly useful for fuzzing stuff that accidentally depends on writes to
IO
always succeeding (like the fallback method ofwrite
in Base does, for example).One issue I see with just throwing an error for partial writes/write failures of parts of larger types is that then the return value of
write
becomes meaningless - either we always get a full write, or we get an error. There would be no more room for partial writes, which can happen in a bunch of cases.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This conversation is worth continuing, but for the purposes of fixing this bug I think it's orthogonal.
Our
AbstractArray
write method can also suffer "torn" writes in the same way:This is probably worth splitting into a separate issue and fixing across-the-board. The only thing I think this needs to merge @gbaraldi is a test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Filed #57011 to continue discussion here