]> Cypherpunks repositories - gostls13.git/commitdiff
net: return io.ErrClosedPipe when possible from net.Pipe
authorJoe Tsai <joetsai@digital-static.net>
Mon, 4 Dec 2017 19:16:51 +0000 (11:16 -0800)
committerJoe Tsai <thebrokentoaster@gmail.com>
Tue, 5 Dec 2017 18:44:28 +0000 (18:44 +0000)
The previous implementation of net.Pipe was just a thin wrapper around
io.Pipe and did not wrap any of the io.Pipe errors as net.Errors.
As a result of Hyrum's law, users have come to depend on the fact that
net.Pipe returns io.ErrClosedPipe when the pipe is closed.
Thus, we preserve this behavior to avoid regressing such use cases.

Change-Id: I06b387877b944c1c08527601f58983872b7557b4
Reviewed-on: https://go-review.googlesource.com/81777
Run-TryBot: Joe Tsai <thebrokentoaster@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/net/pipe.go
src/net/pipe_test.go

index 38d0f42a7077d237e0951d8a220754665a6680e0..9177fc403643e3b9de936811a9c1873efa5a34cf 100644 (file)
@@ -78,19 +78,11 @@ func isClosedChan(c <-chan struct{}) bool {
        }
 }
 
-type pipeError struct {
-       errStr  string
-       timeout bool
-}
-
-func (pe pipeError) Error() string   { return pe.errStr }
-func (pe pipeError) Timeout() bool   { return pe.timeout }
-func (pe pipeError) Temporary() bool { return pe.timeout }
+type timeoutError struct{}
 
-var (
-       errDeadline = pipeError{"deadline exceeded", true}
-       errClosed   = pipeError{"closed connection", false}
-)
+func (timeoutError) Error() string   { return "deadline exceeded" }
+func (timeoutError) Timeout() bool   { return true }
+func (timeoutError) Temporary() bool { return true }
 
 type pipeAddr struct{}
 
@@ -153,7 +145,7 @@ func (*pipe) RemoteAddr() Addr { return pipeAddr{} }
 
 func (p *pipe) Read(b []byte) (int, error) {
        n, err := p.read(b)
-       if err != nil && err != io.EOF {
+       if err != nil && err != io.EOF && err != io.ErrClosedPipe {
                err = &OpError{Op: "read", Net: "pipe", Err: err}
        }
        return n, err
@@ -162,11 +154,11 @@ func (p *pipe) Read(b []byte) (int, error) {
 func (p *pipe) read(b []byte) (n int, err error) {
        switch {
        case isClosedChan(p.localDone):
-               return 0, errClosed
+               return 0, io.ErrClosedPipe
        case isClosedChan(p.remoteDone):
                return 0, io.EOF
        case isClosedChan(p.readDeadline.wait()):
-               return 0, errDeadline
+               return 0, timeoutError{}
        }
 
        select {
@@ -175,17 +167,17 @@ func (p *pipe) read(b []byte) (n int, err error) {
                p.rdTx <- nr
                return nr, nil
        case <-p.localDone:
-               return 0, errClosed
+               return 0, io.ErrClosedPipe
        case <-p.remoteDone:
                return 0, io.EOF
        case <-p.readDeadline.wait():
-               return 0, errDeadline
+               return 0, timeoutError{}
        }
 }
 
 func (p *pipe) Write(b []byte) (int, error) {
        n, err := p.write(b)
-       if err != nil {
+       if err != nil && err != io.ErrClosedPipe {
                err = &OpError{Op: "write", Net: "pipe", Err: err}
        }
        return n, err
@@ -194,11 +186,11 @@ func (p *pipe) Write(b []byte) (int, error) {
 func (p *pipe) write(b []byte) (n int, err error) {
        switch {
        case isClosedChan(p.localDone):
-               return 0, errClosed
+               return 0, io.ErrClosedPipe
        case isClosedChan(p.remoteDone):
-               return 0, errClosed
+               return 0, io.ErrClosedPipe
        case isClosedChan(p.writeDeadline.wait()):
-               return 0, errDeadline
+               return 0, timeoutError{}
        }
 
        p.wrMu.Lock() // Ensure entirety of b is written together
@@ -210,11 +202,11 @@ func (p *pipe) write(b []byte) (n int, err error) {
                        b = b[nw:]
                        n += nw
                case <-p.localDone:
-                       return n, errClosed
+                       return n, io.ErrClosedPipe
                case <-p.remoteDone:
-                       return n, errClosed
+                       return n, io.ErrClosedPipe
                case <-p.writeDeadline.wait():
-                       return n, errDeadline
+                       return n, timeoutError{}
                }
        }
        return n, nil
@@ -222,7 +214,7 @@ func (p *pipe) write(b []byte) (n int, err error) {
 
 func (p *pipe) SetDeadline(t time.Time) error {
        if isClosedChan(p.localDone) || isClosedChan(p.remoteDone) {
-               return &OpError{Op: "set", Net: "pipe", Err: errClosed}
+               return io.ErrClosedPipe
        }
        p.readDeadline.set(t)
        p.writeDeadline.set(t)
@@ -231,7 +223,7 @@ func (p *pipe) SetDeadline(t time.Time) error {
 
 func (p *pipe) SetReadDeadline(t time.Time) error {
        if isClosedChan(p.localDone) || isClosedChan(p.remoteDone) {
-               return &OpError{Op: "set", Net: "pipe", Err: errClosed}
+               return io.ErrClosedPipe
        }
        p.readDeadline.set(t)
        return nil
@@ -239,7 +231,7 @@ func (p *pipe) SetReadDeadline(t time.Time) error {
 
 func (p *pipe) SetWriteDeadline(t time.Time) error {
        if isClosedChan(p.localDone) || isClosedChan(p.remoteDone) {
-               return &OpError{Op: "set", Net: "pipe", Err: errClosed}
+               return io.ErrClosedPipe
        }
        p.writeDeadline.set(t)
        return nil
index 382eca75203b59fb366a5979579bfee25d76c682..84a71b756bc044eefcd48c6e130fc9bf9e34b342 100644 (file)
@@ -5,8 +5,10 @@
 package net_test
 
 import (
+       "io"
        "net"
        "testing"
+       "time"
 
        "golang_org/x/net/nettest"
 )
@@ -21,3 +23,27 @@ func TestPipe(t *testing.T) {
                return
        })
 }
+
+func TestPipeCloseError(t *testing.T) {
+       c1, c2 := net.Pipe()
+       c1.Close()
+
+       if _, err := c1.Read(nil); err != io.ErrClosedPipe {
+               t.Errorf("c1.Read() = %v, want io.ErrClosedPipe", err)
+       }
+       if _, err := c1.Write(nil); err != io.ErrClosedPipe {
+               t.Errorf("c1.Write() = %v, want io.ErrClosedPipe", err)
+       }
+       if err := c1.SetDeadline(time.Time{}); err != io.ErrClosedPipe {
+               t.Errorf("c1.SetDeadline() = %v, want io.ErrClosedPipe", err)
+       }
+       if _, err := c2.Read(nil); err != io.EOF {
+               t.Errorf("c2.Read() = %v, want io.EOF", err)
+       }
+       if _, err := c2.Write(nil); err != io.ErrClosedPipe {
+               t.Errorf("c2.Write() = %v, want io.ErrClosedPipe", err)
+       }
+       if err := c2.SetDeadline(time.Time{}); err != io.ErrClosedPipe {
+               t.Errorf("c2.SetDeadline() = %v, want io.ErrClosedPipe", err)
+       }
+}