-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
io: clarify Writer bytes written semantics #56533
Comments
If the system call doesn't tell us what happened, then the I think the intent is clear: |
Fair; if we don't know, then what do we report? Should the count returned by Write include all of the bytes that were attempted, or none? That's what I'm trying to document, I guess. |
I see. I think none. While we don't know whether that is accurate, it seems more likely than all or some, I think. |
Much of Go's writer implementations or wrappers expect reliable byte stream: errors are often assumed to imply that the writer is unusable past that point. The returned count from a Read call is particularly meaningful since Read can return early without error. In contrast, a Write call is expected to block until the full buffer has been written. For writer-as-stream cases, it's not clear how valuable the returned Since we should consistently return some value for n, due to the previously discussed ambiguity, perhaps we should just return the number of bytes affirmatively reported as being successfully written (which may be less than the number of bytes actually received, such as in the case of a broken network connection). In the original scenario listed, this may mean that we report 0 bytes written, since the underlying transport may be unable to distinguish the number of bytes received on the other side and because we're sending all 17 bytes at once (we certainly wouldn't be writing 1 byte at a time just to have byte-level failure precision). If, internally, we sent a batch of 16 bytes (e.g. due to framing), which succeeded, and then sent 1 byte, which failed, we would certainly return 16, since that number was affirmatively successful. |
I think it would be more useful to document the semantics of specific errors than to try document the semantics of If there are specific errors that always indicate an exact count of bytes written — or specific errors that always indicate an inexact count of bytes written — then it seems useful for callers who observe those specific errors to know those implications, but given the ambiguity in the |
The documentation for
io.Writer
states:And for example,
*os.File.Write()
says this:With some write API's, to sockets or files, you may get an error and not know whether the write actually succeeded or not (see https://danluu.com/file-consistency/ for examples).
If, say, 16 bytes are written and there is an error writing byte 17. The interface description isn't clear about what should happen:
Under the hood,
*os.File.Write
for example eventually invokes thewrite
syscall, which also does not give any clarity about what happens, at least on BSD.My request is to clarify what should happen, and then document it for consistency.
The text was updated successfully, but these errors were encountered: