Skip to content

Commit

Permalink
fix rpc client panic cause by concurrent close (tikv#1359)
Browse files Browse the repository at this point in the history
close issue tikv#1357

---------

Signed-off-by: crazycs520 <[email protected]>
  • Loading branch information
crazycs520 committed Jul 24, 2024
1 parent be14c99 commit 1baa1dc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,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 {
Expand Down
20 changes: 20 additions & 0 deletions internal/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

0 comments on commit 1baa1dc

Please sign in to comment.