Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed race #37

Open
wants to merge 1 commit into
base: release/v1.54
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading