-
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: Copy implicilty requires WriterTo and ReaderFrom to not return io.EOF #44411
Comments
I think this is a documentation issue. I can't see any reason why |
@ianlancetaylor i make a PR, can you help view it ? thanks! |
Change https://golang.org/cl/294709 mentions this issue: |
You could do it by accident if you don't use |
Because io.Copy delegates to WriteTo and ReadFrom if possible, implementors need to be made aware that the EOF behaviour of WriteTo and ReadFrom need to match the io.Copy semantics (do not return io.EOF -- even if no bytes are read). In addition, the documentation of the two interfaces had diverged slightly, so re-unify the wording. Fixes: golang#44411 Signed-off-by: Aleksa Sarai <[email protected]>
Change https://golang.org/cl/294769 mentions this issue: |
Because io.Copy delegates to WriteTo and ReadFrom if possible, implementors need to be made aware that the EOF behaviour of WriteTo and ReadFrom need to match the io.Copy semantics (do not return io.EOF -- even if no bytes are read). In addition, the documentation of the two interfaces had diverged slightly, so re-unify the wording. Fixes golang#44411
1. WriteTo should not return EOF (see golang/go#44411). 2. In case of EOF, WriteTo should write the final payload before returning 3. In case of non EOF error, WriteTo should return n, not nw.
1. WriteTo should not return EOF (see golang/go#44411). 2. In case of EOF, WriteTo should write the final payload before returning 3. In case of non EOF error, WriteTo should return n, not nw.
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
https://play.golang.org/p/6dFx2mL7f5r
The primary issue is that if you have an
io.Reader
which implementsWriteTo
such that it returnsio.EOF
at the end of the stream,io.Copy
will blindly forward that error. klauspost/pgzip#38 is an example of a fairly popular project which made this mistake, and I hit this in opencontainers/umoci#360.The same thing happens if you return
io.EOF
fromReadFrom
(which is a little bit weirder to do, but if you don't useio.Copy
in the implementation ofio.ReaderFrom
you might do this by accident).It seems this is either:
io.WriterTo
andio.ReaderFrom
documentation doesn't mention that you shouldn't returnio.EOF
from those methods. The wording does kind of imply that you shouldn't returnio.EOF
but IMHO this should be much more explicit because anyWriteTo
implementation that doesn't useio.Copy
internally will have to handle this explicitly and should be warned that this is the case.io.Copy
(and possibly related stdlib users ofio.WriterTo
andio.ReaderFrom
), becauseio.EOF
is handled when it is returned byRead
.What did you expect to see?
or documentation which mentions that you shouldn't return
io.EOF
fromWriteTo
(orReadFrom
).What did you see instead?
The text was updated successfully, but these errors were encountered: