Skip to content

Commit

Permalink
fixed race
Browse files Browse the repository at this point in the history
  • Loading branch information
dvovk committed Nov 30, 2024
1 parent 688e150 commit adba57d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion client-stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type ClientStats struct {

func (cl *Client) statsLocked() (stats ClientStats) {
stats.ConnStats = cl.connStats.Copy()
stats.ActiveHalfOpenAttempts = cl.numHalfOpen
stats.ActiveHalfOpenAttempts = int(cl.numHalfOpen.Load())

stats.NumPeersUndialableWithoutHolepunch = len(cl.undialableWithoutHolepunch)
stats.NumPeersUndialableWithoutHolepunchDialedAfterHolepunchConnect = len(cl.undialableWithoutHolepunchDialedAfterHolepunchConnect)
Expand Down
8 changes: 5 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import (
gbtree "github.com/google/btree"
"github.com/pion/datachannel"

"sync/atomic"

"github.com/anacrolix/torrent/bencode"
"github.com/anacrolix/torrent/internal/check"
"github.com/anacrolix/torrent/internal/limiter"
Expand Down Expand Up @@ -84,7 +86,7 @@ type Client struct {
pieceRequestOrder map[interface{}]*request_strategy.PieceRequestOrder

acceptLimiter map[ipStr]int
numHalfOpen int
numHalfOpen atomic.Int64

websocketTrackers websocketTrackers

Expand Down Expand Up @@ -691,8 +693,8 @@ func (cl *Client) noLongerHalfOpen(t *Torrent, addr string, attemptKey outgoingC
panic("should exist")
}
path.Delete()
cl.numHalfOpen--
if cl.numHalfOpen < 0 {
cl.numHalfOpen.Store(cl.numHalfOpen.Load() - 1)
if cl.numHalfOpen.Load() < 0 {
panic("should not be possible")
}
for _, t := range cl.torrentsAsSlice() {
Expand Down
4 changes: 2 additions & 2 deletions torrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -1734,7 +1734,7 @@ func (t *Torrent) openNewConns(lock bool) (initiated int) {
if len(t.cl.dialers) == 0 {
return
}
if t.cl.numHalfOpen >= t.cl.config.TotalHalfOpenConns {
if int(t.cl.numHalfOpen.Load()) >= t.cl.config.TotalHalfOpenConns {
return
}
p := t.peers.PopMax()
Expand Down Expand Up @@ -3108,7 +3108,7 @@ func (t *Torrent) addHalfOpen(addrStr string, attemptKey *PeerInfo, lock bool) {
panic("should be unique")
}
path.Set(attemptKey)
t.cl.numHalfOpen++
t.cl.numHalfOpen.Store(t.cl.numHalfOpen.Load() + 1)
}

// Start the process of connecting to the given peer for the given torrent if appropriate. I'm not
Expand Down

0 comments on commit adba57d

Please sign in to comment.