From d0ddd390fb0dbbd802630662756270464c3b5f81 Mon Sep 17 00:00:00 2001 From: crazycs Date: Fri, 31 May 2024 18:21:21 +0800 Subject: [PATCH] fix rpc client panic cause by concurrent close (#1359) close issue #1357 Signed-off-by: crazycs520 --------- Signed-off-by: crazycs520 --- internal/client/client.go | 4 ++++ internal/client/client_test.go | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) 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() +}