diff --git a/internal/client/client.go b/internal/client/client.go index e1bfda575..de4e80edb 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -288,7 +288,9 @@ func (c *RPCClient) getConnArray(addr string, enableBatch bool, opt ...func(cfg } array, ok := c.conns[addr] c.RUnlock() - if !ok { + // An idle connArray will not change to active again, this avoid the race condition + // that recycling idle connection close an active connection unexpectedly (idle -> active). + if !ok || array.isIdle() { var err error array, err = c.createConnArray(addr, enableBatch, opt...) if err != nil { diff --git a/internal/client/client_batch.go b/internal/client/client_batch.go index d4850a3d4..5b15f076f 100644 --- a/internal/client/client_batch.go +++ b/internal/client/client_batch.go @@ -816,8 +816,6 @@ func (c *RPCClient) recycleIdleConnArray() { } c.Unlock() - // NOTE, There is a risk that an idle connection become active again, and here is - // a race condition someone is using the connection and it's closed here. if conn != nil { conn.Close() }