From 53f4bfe24222f1e0d57b13543b4aa17ce3da8f2f 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 49b0d9f69..94e442624 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -650,6 +650,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 b1bc3ec31..e72973f95 100644 --- a/internal/client/client_test.go +++ b/internal/client/client_test.go @@ -715,3 +715,23 @@ func TestBatchClientRecoverAfterServerRestart(t *testing.T) { require.NoError(t, err) } } + +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() +}