From cbfd64018c2194af29694547bfcad1972cb24512 Mon Sep 17 00:00:00 2001 From: disksing Date: Thu, 7 Apr 2022 11:58:49 +0800 Subject: [PATCH] client: fix the race between connArray.Close() and connArray.Get() (#466) Signed-off-by: disksing --- .github/workflows/test.yml | 4 +--- internal/client/client.go | 11 ++++------- internal/client/client_test.go | 13 +++++++++++++ 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ad02c3d38..949eb9db3 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -45,6 +45,4 @@ jobs: go-version: 1.16 - name: Lint - uses: golangci/golangci-lint-action@v2 - with: - version: v1.29 + uses: golangci/golangci-lint-action@v2.5.2 diff --git a/internal/client/client.go b/internal/client/client.go index 78b6e0562..8977a72f1 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -134,7 +134,7 @@ func newConnArray(maxSize uint, addr string, security config.Security, idleNotif func (a *connArray) Init(addr string, security config.Security, idleNotify *uint32, enableBatch bool) error { a.target = addr - opt := grpc.WithInsecure() //nolint + opt := grpc.WithInsecure() if len(security.ClusterSSLCA) != 0 { tlsConfig, err := security.ToTLSConfig() if err != nil { @@ -234,12 +234,9 @@ func (a *connArray) Close() { a.batchConn.Close() } - for i, c := range a.v { - if c != nil { - err := c.Close() - tikverr.Log(err) - a.v[i] = nil - } + for _, c := range a.v { + err := c.Close() + tikverr.Log(err) } close(a.done) diff --git a/internal/client/client_test.go b/internal/client/client_test.go index 8119fb32f..142f65ab5 100644 --- a/internal/client/client_test.go +++ b/internal/client/client_test.go @@ -51,6 +51,7 @@ import ( "github.com/stretchr/testify/require" "github.com/tikv/client-go/v2/config" "github.com/tikv/client-go/v2/tikvrpc" + "google.golang.org/grpc/connectivity" "google.golang.org/grpc/metadata" ) @@ -75,6 +76,18 @@ func TestConn(t *testing.T) { assert.Nil(t, conn3) } +func TestGetConnAfterClose(t *testing.T) { + client := NewRPCClient() + + addr := "127.0.0.1:6379" + connArray, err := client.getConnArray(addr, true) + assert.Nil(t, err) + connArray.Close() + conn := connArray.Get() + state := conn.GetState() + assert.True(t, state == connectivity.Shutdown) +} + func TestCancelTimeoutRetErr(t *testing.T) { req := new(tikvpb.BatchCommandsRequest_Request) a := newBatchConn(1, 1, nil)