client: fix the race between connArray.Close() and connArray.Get() (#465)

Signed-off-by: youjiali1995 <zlwgx1023@gmail.com>
This commit is contained in:
Lei Zhao 2022-04-06 17:12:03 +08:00 committed by GitHub
parent c7fbfc2641
commit f73ec0e675
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 8 deletions

View File

@ -137,7 +137,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 { func (a *connArray) Init(addr string, security config.Security, idleNotify *uint32, enableBatch bool) error {
a.target = addr a.target = addr
opt := grpc.WithInsecure() //nolint opt := grpc.WithInsecure()
if len(security.ClusterSSLCA) != 0 { if len(security.ClusterSSLCA) != 0 {
tlsConfig, err := security.ToTLSConfig() tlsConfig, err := security.ToTLSConfig()
if err != nil { if err != nil {
@ -237,12 +237,9 @@ func (a *connArray) Close() {
a.batchConn.Close() a.batchConn.Close()
} }
for i, c := range a.v { for _, c := range a.v {
if c != nil { err := c.Close()
err := c.Close() tikverr.Log(err)
tikverr.Log(err)
a.v[i] = nil
}
} }
close(a.done) close(a.done)

View File

@ -51,6 +51,7 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/tikv/client-go/v2/config" "github.com/tikv/client-go/v2/config"
"github.com/tikv/client-go/v2/tikvrpc" "github.com/tikv/client-go/v2/tikvrpc"
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/metadata" "google.golang.org/grpc/metadata"
) )
@ -82,6 +83,18 @@ func TestConn(t *testing.T) {
assert.Nil(t, conn4) assert.Nil(t, conn4)
} }
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) { func TestCancelTimeoutRetErr(t *testing.T) {
req := new(tikvpb.BatchCommandsRequest_Request) req := new(tikvpb.BatchCommandsRequest_Request)
a := newBatchConn(1, 1, nil) a := newBatchConn(1, 1, nil)

View File

@ -2347,7 +2347,7 @@ func createKVHealthClient(ctx context.Context, addr string) (*grpc.ClientConn, h
cfg := config.GetGlobalConfig() cfg := config.GetGlobalConfig()
opt := grpc.WithInsecure() //nolint opt := grpc.WithInsecure()
if len(cfg.Security.ClusterSSLCA) != 0 { if len(cfg.Security.ClusterSSLCA) != 0 {
tlsConfig, err := cfg.Security.ToTLSConfig() tlsConfig, err := cfg.Security.ToTLSConfig()
if err != nil { if err != nil {