From c8a54235d0361732cea04bd7a279e15d20a3deb0 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Thu, 20 May 2021 00:07:44 +0200 Subject: [PATCH] Silence net.ErrClosed errors --- conn.go | 6 ++++-- net_go113.go | 11 +++++++++++ net_go116.go | 12 ++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 net_go113.go create mode 100644 net_go116.go diff --git a/conn.go b/conn.go index d02cba2..5624728 100644 --- a/conn.go +++ b/conn.go @@ -194,7 +194,7 @@ func newConn(srv *Server, ic ircConn, options *connOptions) *conn { break } } - if err := c.conn.Close(); err != nil { + if err := c.conn.Close(); err != nil && !isErrClosed(err) { c.logger.Printf("failed to close connection: %v", err) } else { c.logger.Printf("connection closed") @@ -232,7 +232,9 @@ func (c *conn) Close() error { func (c *conn) ReadMessage() (*irc.Message, error) { msg, err := c.conn.ReadMessage() - if err != nil { + if isErrClosed(err) { + return nil, io.EOF + } else if err != nil { return nil, err } diff --git a/net_go113.go b/net_go113.go new file mode 100644 index 0000000..9e8186a --- /dev/null +++ b/net_go113.go @@ -0,0 +1,11 @@ +// +build !go1.16 + +package soju + +import ( + "strings" +) + +func isErrClosed(err error) bool { + return err != nil && strings.Contains(err.Error(), "use of closed network connection") +} diff --git a/net_go116.go b/net_go116.go new file mode 100644 index 0000000..620621a --- /dev/null +++ b/net_go116.go @@ -0,0 +1,12 @@ +// +build go1.16 + +package soju + +import ( + "errors" + "net" +) + +func isErrClosed(err error) bool { + return errors.Is(err, net.ErrClosed) +}