diff --git a/internal/client/client.go b/internal/client/client.go index 67e158c69..a62c82bb6 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -786,6 +786,10 @@ func (c *RPCClient) Close() error { // CloseAddr closes gRPC connections to the address. func (c *RPCClient) CloseAddr(addr string) error { c.Lock() + if c.isClosed { + c.Unlock() + return nil + } conn, ok := c.conns[addr] if ok { delete(c.conns, addr) diff --git a/internal/client/client_test.go b/internal/client/client_test.go index b58942120..8a3576d3c 100644 --- a/internal/client/client_test.go +++ b/internal/client/client_test.go @@ -640,3 +640,23 @@ func TestTraceExecDetails(t *testing.T) { }) } } + +func TestConcurrentCloseConnPanic(t *testing.T) { + client := NewRPCClient() + addr := "127.0.0.1:6379" + _, err := client.getConnArray(addr, true) + assert.Nil(t, err) + var wg sync.WaitGroup + wg.Add(2) + go func() { + defer wg.Done() + err := client.Close() + assert.Nil(t, err) + }() + go func() { + defer wg.Done() + err := client.CloseAddr(addr) + assert.Nil(t, err) + }() + wg.Wait() +}