From 872ed736b491e07a04f2cf060049b13cefcbdf5f Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Tue, 21 Sep 2021 11:49:48 +0200 Subject: [PATCH] Workaround lack of net.ErrClosed in WebSocket library --- conn.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/conn.go b/conn.go index 5624728..e25f7da 100644 --- a/conn.go +++ b/conn.go @@ -74,8 +74,18 @@ func (wic *websocketIRCConn) WriteMessage(msg *irc.Message) error { return wic.conn.Write(ctx, websocket.MessageText, b) } +func isErrWebSocketClosed(err error) bool { + return err != nil && strings.HasSuffix(err.Error(), "failed to close WebSocket: already wrote close") +} + func (wic *websocketIRCConn) Close() error { - return wic.conn.Close(websocket.StatusNormalClosure, "") + err := wic.conn.Close(websocket.StatusNormalClosure, "") + // TODO: remove once this PR is merged: + // https://github.com/nhooyr/websocket/pull/303 + if isErrWebSocketClosed(err) { + return nil + } + return err } func (wic *websocketIRCConn) SetReadDeadline(t time.Time) error {