diff --git a/internal/client/client.go b/internal/client/client.go index 21dc34a9c..c08b3abde 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -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 197c66fa0..325fa1198 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) + assert.Nil(t, client.CloseAddr(addr)) + 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)