Skip to content

Commit

Permalink
client: fix the race between connArray.Close() and connArray.Get() (#466
Browse files Browse the repository at this point in the history
)

Signed-off-by: disksing <[email protected]>
  • Loading branch information
disksing committed Apr 7, 2022
1 parent 46d6493 commit cbfd640
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,4 @@ jobs:
go-version: 1.16

- name: Lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.29
uses: golangci/[email protected]
11 changes: 4 additions & 7 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions internal/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
Expand Down

0 comments on commit cbfd640

Please sign in to comment.