mirror of https://github.com/tikv/client-go.git
fix rpc client panic cause by concurrent close (#1359)
* fix rpc client panic cause by concurrent close Signed-off-by: crazycs520 <crazycs520@gmail.com> * address comment Signed-off-by: crazycs520 <crazycs520@gmail.com> * refine Signed-off-by: crazycs520 <crazycs520@gmail.com> --------- Signed-off-by: crazycs520 <crazycs520@gmail.com>
This commit is contained in:
parent
1f814b7a45
commit
cb580bc4ea
|
|
@ -860,6 +860,10 @@ func (c *RPCClient) CloseAddr(addr string) error {
|
|||
|
||||
func (c *RPCClient) CloseAddrVer(addr string, ver uint64) error {
|
||||
c.Lock()
|
||||
if c.isClosed {
|
||||
c.Unlock()
|
||||
return nil
|
||||
}
|
||||
conn, ok := c.conns[addr]
|
||||
if ok {
|
||||
if conn.ver <= ver {
|
||||
|
|
|
|||
|
|
@ -1047,3 +1047,23 @@ func TestFastFailWhenNoAvailableConn(t *testing.T) {
|
|||
require.Equal(t, "no available connections", err.Error())
|
||||
require.Less(t, time.Since(start), timeout)
|
||||
}
|
||||
|
||||
func TestConcurrentCloseConnPanic(t *testing.T) {
|
||||
client := NewRPCClient()
|
||||
addr := "127.0.0.1:6379"
|
||||
_, err := client.getConnArray(addr, true)
|
||||
assert.Nil(t, err)
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(2)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
err := client.Close()
|
||||
assert.Nil(t, err)
|
||||
}()
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
err := client.CloseAddr(addr)
|
||||
assert.Nil(t, err)
|
||||
}()
|
||||
wg.Wait()
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue