Skip to content

Commit

Permalink
rpcclient: fix race condition in doDisconnect
Browse files Browse the repository at this point in the history
In this commit, we fix the following race condition:
```
==================
WARNING: DATA RACE
Write at 0x00c000216018 by goroutine 31:
  github.com/btcsuite/btcd/rpcclient.(*Client).wsReconnectHandler()
      /home/runner/work/btcd/btcd/rpcclient/infrastructure.go:736 +0x2aa
  github.com/btcsuite/btcd/rpcclient.New·dwrap·13()
      /home/runner/work/btcd/btcd/rpcclient/infrastructure.go:1496 +0x39

Previous read at 0x00c000216018 by goroutine 29:
  github.com/btcsuite/btcd/rpcclient.(*Client).doDisconnect()
      /home/runner/work/btcd/btcd/rpcclient/infrastructure.go:1079 +0x247
  github.com/btcsuite/btcd/rpcclient.(*Client).Disconnect()
      /home/runner/work/btcd/btcd/rpcclient/infrastructure.go:1111 +0x47
  github.com/btcsuite/btcd/rpcclient.(*Client).wsInHandler()
      /home/runner/work/btcd/btcd/rpcclient/infrastructure.go:491 +0x1eb
  github.com/btcsuite/btcd/rpcclient.(*Client).start·dwrap·11()
      /home/runner/work/btcd/btcd/rpcclient/infrastructure.go:1181 +0x39

Goroutine 31 (running) created at:
  github.com/btcsuite/btcd/rpcclient.New()
      /home/runner/work/btcd/btcd/rpcclient/infrastructure.go:1496 +0xd77
  github.com/btcsuite/btcd/rpcclient.makeClient()
      /home/runner/work/btcd/btcd/rpcclient/chain_test.go:268 +0x1f9
  github.com/btcsuite/btcd/rpcclient.TestClientConnectedToWSServerRunner.func2()
      /home/runner/work/btcd/btcd/rpcclient/chain_test.go:164 +0x47
  testing.tRunner()
      /opt/hostedtoolcache/go/1.17.5/x64/src/testing/testing.go:1259 +0x22f
  testing.(*T).Run·dwrap·21()
      /opt/hostedtoolcache/go/1.17.5/x64/src/testing/testing.go:1306 +0x47

Goroutine 29 (finished) created at:
  github.com/btcsuite/btcd/rpcclient.(*Client).start()
      /home/runner/work/btcd/btcd/rpcclient/infrastructure.go:1181 +0x2e4
  github.com/btcsuite/btcd/rpcclient.New()
      /home/runner/work/btcd/btcd/rpcclient/infrastructure.go:1493 +0xc51
  github.com/btcsuite/btcd/rpcclient.makeClient()
      /home/runner/work/btcd/btcd/rpcclient/chain_test.go:268 +0x1f9
  github.com/btcsuite/btcd/rpcclient.TestClientConnectedToWSServerRunner.func2()
      /home/runner/work/btcd/btcd/rpcclient/chain_test.go:164 +0x47
  testing.tRunner()
      /opt/hostedtoolcache/go/1.17.5/x64/src/testing/testing.go:1259 +0x22f
  testing.(*T).Run·dwrap·21()
      /opt/hostedtoolcache/go/1.17.5/x64/src/testing/testing.go:1306 +0x47
```

This arises as in `wsReconnectHandler`, the mutex isn't held while the
`conn` pointer is modified.
  • Loading branch information
Roasbeef committed Dec 31, 2023
1 parent 8d2ab63 commit 4ec8f01
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion rpcclient/infrastructure.go
Original file line number Diff line number Diff line change
Expand Up @@ -733,10 +733,10 @@ out:

// Reset the connection state and signal the reconnect
// has happened.
c.mtx.Lock()
c.wsConn = wsConn
c.retryCount = 0

c.mtx.Lock()
c.disconnect = make(chan struct{})
c.disconnected = false
c.mtx.Unlock()
Expand Down

0 comments on commit 4ec8f01

Please sign in to comment.