From e4dc2883efea932f1da62ef35c3e88806aed9eea Mon Sep 17 00:00:00 2001 From: David Fifield Date: Tue, 3 Aug 2021 21:00:45 -0600 Subject: [PATCH] Use errors.Is to compare against ErrClosedPipe. I was still getting "io: read/write on closed pipe" errors in the logs, even after comparing errors against io.ErrClosedPipe to skip logging them. It turns out that kcp-go wraps many of its errors in another type. The actual type of the errors was *errors.withStack, where errors is https://github.com/pkg/errors. We can use the go1.13 errors interface (https://blog.golang.org/go1.13-errors) to get at the value inside. --- dnstt-client/main.go | 5 +++-- dnstt-server/main.go | 11 ++++------- go.mod | 2 +- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/dnstt-client/main.go b/dnstt-client/main.go index f73d97e..19b05f6 100644 --- a/dnstt-client/main.go +++ b/dnstt-client/main.go @@ -26,6 +26,7 @@ package main import ( + "errors" "flag" "fmt" "io" @@ -95,7 +96,7 @@ func handle(local *net.TCPConn, sess *smux.Session, conv uint32) error { // smux Stream.Write may return io.EOF. err = nil } - if err != nil && err != io.ErrClosedPipe { + if err != nil && !errors.Is(err, io.ErrClosedPipe) { log.Printf("stream %08x:%d copy stream←local: %v", conv, stream.ID(), err) } local.CloseRead() @@ -108,7 +109,7 @@ func handle(local *net.TCPConn, sess *smux.Session, conv uint32) error { // smux Stream.WriteTo may return io.EOF. err = nil } - if err != nil && err != io.ErrClosedPipe { + if err != nil && !errors.Is(err, io.ErrClosedPipe) { log.Printf("stream %08x:%d copy local←stream: %v", conv, stream.ID(), err) } local.CloseWrite() diff --git a/dnstt-server/main.go b/dnstt-server/main.go index 4bfa6cd..753fdff 100644 --- a/dnstt-server/main.go +++ b/dnstt-server/main.go @@ -38,6 +38,7 @@ import ( "bytes" "encoding/base32" "encoding/binary" + "errors" "flag" "fmt" "io" @@ -205,7 +206,7 @@ func handleStream(stream *smux.Stream, upstream string, conv uint32) error { // smux Stream.Write may return io.EOF. err = nil } - if err != nil && err != io.ErrClosedPipe { + if err != nil && !errors.Is(err, io.ErrClosedPipe) { log.Printf("stream %08x:%d copy stream←upstream: %v", conv, stream.ID(), err) } upstreamTCPConn.CloseRead() @@ -218,7 +219,7 @@ func handleStream(stream *smux.Stream, upstream string, conv uint32) error { // smux Stream.WriteTo may return io.EOF. err = nil } - if err != nil && err != io.ErrClosedPipe { + if err != nil && !errors.Is(err, io.ErrClosedPipe) { log.Printf("stream %08x:%d copy upstream←stream: %v", conv, stream.ID(), err) } upstreamTCPConn.CloseWrite() @@ -302,11 +303,7 @@ func acceptSessions(ln *kcp.Listener, privkey []byte, mtu int, upstream string) conn.Close() }() err := acceptStreams(conn, privkey, upstream) - if err == io.ErrClosedPipe { - // We don't want to report this error. - err = nil - } - if err != nil { + if err != nil && !errors.Is(err, io.ErrClosedPipe) { log.Printf("session %08x acceptStreams: %v", conn.GetConv(), err) } }() diff --git a/go.mod b/go.mod index b8750f3..b959c4e 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module www.bamsoftware.com/git/dnstt.git -go 1.11 +go 1.13 require ( github.com/flynn/noise v1.0.0