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

bufio: Reader.WriteTo documentation should clarify that multiple calls to the upstream Reader may be made #19092

Closed
SamWhited opened this issue Feb 14, 2017 · 4 comments

Comments

@SamWhited
Copy link
Member

SamWhited commented Feb 14, 2017

I am unsure if this is intended behavior (in which case this is a documentation problem), or a bug (in which case it may be a bug and a documentation problem :) ). Please advise (or perhaps it's neither, of course); I have patches for both I can submit.

What version of Go are you using (go version)?

go version devel +d2fea0447f Tue Feb 14 19:44:35 2017 +0000 linux/amd64

What did you do?

  • Create a net.Conn that receives some bytes on the first call to Read, and then holds the connection open (so that you do not receive an io.EOF from a call to Read).
  • Now wrap it in a bufio.Reader.
  • Call io.Copy on the new buffered reader and copy it somewhere else (into a bytes.Buffer, for example).

What did you expect to see?

I expected the bufio.Reader's WriteTo method to be called, proxy a single call to conn.Read, write that data, and then return.

What did you see instead?

Instead, it attempts to call conn.Read a second time (the behavior is to read until EOF or an error is returned).

This may not be an issue; the documentation for io.WriterTo says:

WriteTo writes data to w until there's no more data to write or when an error occurs.

However, it is unclear to me if "no more data to write" means "until EOF" in the case of the bufio.Reader implementation, or if it means "the buffer was flushed" or, if the buffer was empty, "a successful proxied read call had the read data written".

Personally I expected this to call Read at most once, much like the bufio.Reader's Read method.

I think that this behavior should either be changed to not use fill() (and I am happy to make this change), or the documentation should explicitly say that bufio.Reader's WriteTo method writes until EOF or an error is returned from an upstream Read call.

@ianlancetaylor
Copy link
Contributor

A WriteTo method is intended to write the entire contents of a value to the io.Writer argument. For a bufio.Reader, this means reading all the data available in the input io.Reader, buffering it, and writing it to the io.Writer, until there is no more data in the input io.Reader.

So, this is a documentation issue.

@ianlancetaylor ianlancetaylor added this to the Go1.9 milestone Feb 14, 2017
@SamWhited SamWhited changed the title bufio: Reader.WriteTo may result in multiple calls to upstream reader bufio: Reader.WriteTo documentation should clarify that multiple calls to the upstream Reader may be made Feb 14, 2017
@ALTree
Copy link
Member

ALTree commented Jun 3, 2017

Please advise [..] I have patches for both I can submit.

@SamWhited It looks like the final decision was that this is a documentation issue. Can you submit your doc patch for inclusion in go1.9?

@gopherbot
Copy link

CL https://golang.org/cl/44811 mentions this issue.

@SamWhited
Copy link
Member Author

@ALTree my apologies, this fell off my radar. Thanks for the prompt.

@golang golang locked and limited conversation to collaborators Jun 8, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants