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

io: PipeWriter allows Write after Close #5330

Closed
gopherbot opened this issue Apr 22, 2013 · 5 comments
Closed

io: PipeWriter allows Write after Close #5330

gopherbot opened this issue Apr 22, 2013 · 5 comments
Milestone

Comments

@gopherbot
Copy link

by xuxinhua1984:

The version: all Go version

The example code:

http://play.golang.org/p/mOhFH_rIYT

The ouput:

http://studygolang.com
The data:[]byte{}
The error: close...
The number of bytes after writing:  22 . The error: io: read/write on closed pipe


Before calling Write method, I call CloseWithError, and The reader can read the data.

In io package, change the file of "pipe.go" Line 76:

        p.l.Lock()
    defer p.l.Unlock()
    if p.werr != nil {
        p.rwait.Signal()
        err = ErrClosedPipe
        return
    }
    p.data = b
    p.rwait.Signal()

The code:
        if p.werr != nil {
        p.rwait.Signal()
        err = ErrClosedPipe
        return
    }
is added。

After that, The output of the example:

The number of bytes after writing:  0 . The error: io: read/write on closed pipe
The data:[]byte{}
The error: close...
@gopherbot
Copy link
Author

Comment 1 by xuxinhua1984:

If the above is not bug, the following should be bug:(if the above fixed by my code,
the following code runs ok.)
http://play.golang.org/p/JCQV4uN8GJ
I write data 3 times, but I read 4 times.
This issue can fixed by above code, otherwise, this code is also can fix it:
io/pipe.go, line 42:
change the code 
        for {
        if p.rerr != nil {
            return 0, ErrClosedPipe
        }
        if p.data != nil {
            break
        }
        if p.werr != nil {
            return 0, p.werr
        }
        p.rwait.Wait()
    }
to 
       for {
        if p.rerr != nil {
            return 0, ErrClosedPipe
        }
                if p.werr != nil {
            return 0, p.werr
        }
        if p.data != nil {
            break
        }
        p.rwait.Wait()
    }

@davecheney
Copy link
Contributor

Comment 2:

I'm having trouble reproducing the problem you reported. I'm also having trouble
figuring out if it is an issue. Please consider this slightly simplified example
http://play.golang.org/p/Vhh0PoiEKq

Status changed to WaitingForReply.

@davecheney
Copy link
Contributor

Comment 3:

ping

@rsc
Copy link
Contributor

rsc commented Jul 25, 2013

Comment 4:

http://play.golang.org/p/bXlyS33kEE
package main
import (
    "fmt"
    "io"
)
func main() {
    r, w := io.Pipe()
    go func() {
        w.Write([]byte("hello"))
        w.Close()
        w.Write([]byte("world"))
    }()
    buf := make([]byte, 100)
    for i := 0; i < 3; i++ {
        n, err := r.Read(buf)
        fmt.Printf("%q %v\n", buf[:n], err)
    }
}
"hello" <nil>
"world" <nil>
"" EOF

Labels changed: added priority-later, go1.2, removed priority-triage.

Status changed to Accepted.

@bradfitz
Copy link
Contributor

Comment 5:

This issue was closed by revision 4be9385.

Status changed to Fixed.

@rsc rsc added this to the Go1.2 milestone Apr 14, 2015
@rsc rsc removed the go1.2 label Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
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