英文:
Race conditions in io.Pipe?
问题
我有一个函数,它返回一个io.Pipe
的Reader
端,并启动一个go协程来向其Writer
端写入数据,然后关闭管道。
func GetPipeReader() io.ReadCloser {
r, w := io.Pipe()
go func() {
_, err := io.CopyN(w, SomeReaderOfSize(N), N)
w.CloseWithError(err)
}()
return r
}
func main() {
var buf bytes.Buffer
io.Copy(&buf, GetPipeReader())
println("got", buf.Len(), "bytes")
}
这在我的测试中似乎总是有效的,我可以得到我写入的所有数据。但是API文档让我有些担心:
**func Pipe() (PipeReader, PipeWriter)
Pipe创建一个同步的内存管道。[...] 一端的读取与另一端的写入相匹配,[...] 没有内部缓冲区。
*func (w PipeWriter) CloseWithError(err error) error
CloseWithError关闭写入器;从管道的读取端进行的后续读取将返回零字节和错误err,如果err为nil,则返回EOF。
我想知道的是,这里可能出现什么竞态条件?我的go协程是否有可能在我读取完所有数据之前写入大量数据并关闭管道?
我需要使用通道来进行一些关闭的信号传递吗?基本上会出现什么问题?
英文:
I have a function which returns the Reader
end of an io.Pipe
and kicks off a go-routine which writes data to the Writer
end of it, and then closes the pipe.
func GetPipeReader() io.ReadCloser {
r, w := io.Pipe()
go func() {
_, err := io.CopyN(w, SomeReaderOfSize(N), N)
w.CloseWithError(err)
}()
return r
}
func main() {
var buf bytes.Buffer
io.Copy(&buf, GetPipeReader())
println("got", buf.Len(), "bytes")
}
https://play.golang.org/p/OAijIwmtRr
This seems to always work in my testing, in that I get all the data I wrote. But the API docs are a bit worrying to me:
> **func Pipe() (PipeReader, PipeWriter)
>
> Pipe creates a synchronous in-memory pipe. [...] Reads on one end are
> matched with writes on the other, [...] there is no internal
> buffering.
>
> *func (w PipeWriter) CloseWithError(err error) error
>
> CloseWithError closes the writer; subsequent reads from the read half
> of the pipe will return no bytes and the error err, or EOF if err is
> nil.
What I want to know is, what are the possible race conditions here? Is is plausible that my go-routine will write a bunch of data and then close the pipe before I can read it all?
Do I need to use a channel for some signalling on when to close? What can go wrong, basically.
答案1
得分: 3
不,没有竞态条件。正如文档中提到的那样,一端的读取与另一端的写入是匹配的。因此,当达到CloseWithError()
时,意味着每个Write
都已成功完成,并与相应的Read
匹配 - 因此另一端必须已经读取了所有可读的内容。
英文:
No, there are no race conditions. As the documentation mentions, reads on one end are matched with writes on the other. So, when CloseWithError()
is reached, it means every Write
has successfully completed and been matched with a corresponding Read
- so the other end must have read everything there was to read.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论