io.Pipe中的竞态条件是什么?

huangapple go评论95阅读模式
英文:

Race conditions in io.Pipe?

问题

我有一个函数,它返回一个io.PipeReader端,并启动一个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.

huangapple
  • 本文由 发表于 2015年12月3日 18:19:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/34063291.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定