From ee1f04ae18f169d13413f1ddf15d2acfa352e9c7 Mon Sep 17 00:00:00 2001 From: youjiali1995 Date: Thu, 7 Apr 2022 16:49:10 +0800 Subject: [PATCH] store/client: fix data race in connArray Signed-off-by: youjiali1995 --- store/tikv/client/client.go | 3 +-- store/tikv/client/client_test.go | 13 +++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/store/tikv/client/client.go b/store/tikv/client/client.go index 856aac8454dc5..2d7bee7850a2b 100644 --- a/store/tikv/client/client.go +++ b/store/tikv/client/client.go @@ -208,11 +208,10 @@ func (a *connArray) Close() { a.batchConn.Close() } - for i, c := range a.v { + for _, c := range a.v { if c != nil { err := c.Close() terror.Log(errors.Trace(err)) - a.v[i] = nil } } diff --git a/store/tikv/client/client_test.go b/store/tikv/client/client_test.go index 7b4b652f55c85..1161aca311fee 100644 --- a/store/tikv/client/client_test.go +++ b/store/tikv/client/client_test.go @@ -30,6 +30,7 @@ import ( "github.com/pingcap/kvproto/pkg/tikvpb" "github.com/pingcap/tidb/store/tikv/config" "github.com/pingcap/tidb/store/tikv/tikvrpc" + "google.golang.org/grpc/connectivity" "google.golang.org/grpc/metadata" ) @@ -95,6 +96,18 @@ func (s *testClientSerialSuite) TestConn(c *C) { c.Assert(conn3, IsNil) } +func (s *testClientSerialSuite) TestGetConnAfterClose(c *C) { + client := NewRPCClient(config.Security{}) + + addr := "127.0.0.1:6379" + connArray, err := client.getConnArray(addr, true) + c.Assert(err, IsNil) + connArray.Close() + conn := connArray.Get() + state := conn.GetState() + c.Assert(state, Equals, connectivity.Shutdown) +} + func (s *testClientSuite) TestCancelTimeoutRetErr(c *C) { req := new(tikvpb.BatchCommandsRequest_Request) a := newBatchConn(1, 1, nil)