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.
This commit is contained in:
David Fifield
2021-08-03 21:00:45 -06:00
parent 1f73f6f5b6
commit e4dc2883ef
3 changed files with 8 additions and 10 deletions
+3 -2
View File
@@ -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()
+4 -7
View File
@@ -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)
}
}()
+1 -1
View File
@@ -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