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

fix race condition in AutoRelay candidate handling #1383

Merged
merged 1 commit into from
Apr 10, 2022
Merged
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
18 changes: 11 additions & 7 deletions p2p/host/autorelay/relay_finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,12 +281,16 @@ func (rf *relayFinder) tryNode(ctx context.Context, pi peer.AddrInfo) (supportsR

func (rf *relayFinder) handleNewCandidate(ctx context.Context) {
rf.relayMx.Lock()
defer rf.relayMx.Unlock()
if len(rf.candidates) == 0 {
numRelays := len(rf.relays)
rf.relayMx.Unlock()
// We're already connected to our desired number of relays. Nothing to do here.
if numRelays == rf.conf.desiredRelays {
return
}
// We're already connected to our desired number of relays. Nothing to do here.
if len(rf.relays) == rf.conf.desiredRelays {

rf.candidateMx.Lock()
defer rf.candidateMx.Unlock()
if len(rf.candidates) == 0 {
return
}

Expand Down Expand Up @@ -468,12 +472,10 @@ func (rf *relayFinder) usingRelay(p peer.ID) bool {
// selectCandidates returns an ordered slice of relay candidates.
// Callers should attempt to obtain reservations with the candidates in this order.
func (rf *relayFinder) selectCandidates() []*candidate {
rf.candidateMx.Lock()
var candidates []*candidate
for _, cand := range rf.candidates {
candidates = append(candidates, cand)
}
rf.candidateMx.Unlock()

// TODO: better relay selection strategy; this just selects random relays,
// but we should probably use ping latency as the selection metric
Expand Down Expand Up @@ -541,7 +543,9 @@ func (rf *relayFinder) Start() error {

func (rf *relayFinder) Stop() error {
log.Debug("stopping relay finder")
rf.ctxCancel()
if rf.ctxCancel != nil {
rf.ctxCancel()
}
rf.refCount.Wait()
rf.ctxCancel = nil
return nil
Expand Down