From fd205f99d1c57ac0dbde38a790db0a31ef645ac0 Mon Sep 17 00:00:00 2001 From: guolei Date: Wed, 22 May 2024 19:28:28 +0800 Subject: [PATCH] fix(connection): move close msgBuffChan to write side to avoid send to closed channel panic --- znet/connection.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/znet/connection.go b/znet/connection.go index c651b186..25b04927 100644 --- a/znet/connection.go +++ b/znet/connection.go @@ -10,11 +10,12 @@ import ( "sync/atomic" "time" + "github.com/gorilla/websocket" + "github.com/aceld/zinx/zconf" "github.com/aceld/zinx/zinterceptor" "github.com/aceld/zinx/zlog" "github.com/aceld/zinx/zpack" - "github.com/gorilla/websocket" "github.com/aceld/zinx/ziface" ) @@ -388,6 +389,8 @@ func (c *Connection) SendToQueue(data []byte) error { // Send timeout select { case <-c.ctx.Done(): + // Close all channels associated with the connection + close(c.msgBuffChan) return errors.New("connection closed when send buff msg") case <-idleTimeout.C: return errors.New("send buff msg timeout") @@ -479,11 +482,6 @@ func (c *Connection) finalizer() { c.connManager.Remove(c) } - // Close all channels associated with the connection - if c.msgBuffChan != nil { - close(c.msgBuffChan) - } - go func() { defer func() { if err := recover(); err != nil {