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 execOnMany #717

Closed
wants to merge 3 commits into from
Closed
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
24 changes: 15 additions & 9 deletions fullrt/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -834,35 +834,41 @@ func (dht *FullRT) execOnMany(ctx context.Context, fn func(context.Context, peer
putctx, cancel := context.WithCancel(ctx)
defer cancel()

waitAllCh := make(chan struct{}, len(peers))
waitFailCh := make(chan struct{})
waitSuccessCh := make(chan struct{})
numSuccessfulToWaitFor := int(float64(len(peers)) * dht.waitFrac)
waitSuccessCh := make(chan struct{}, numSuccessfulToWaitFor)
for _, p := range peers {
go func(p peer.ID) {
fnCtx, fnCancel := context.WithTimeout(putctx, dht.timeoutPerOp)
defer fnCancel()
err := fn(fnCtx, p)
if err != nil {
logger.Debug(err)
waitFailCh <- struct{}{}
} else {
waitSuccessCh <- struct{}{}
}
waitAllCh <- struct{}{}
}(p)
}

numSuccess, numDone := 0, 0
t := time.NewTimer(time.Hour)
for numDone != len(peers) {
var numFail, numSuccess int
t := time.NewTimer(0)
if !t.Stop() {
<-t.C
}
defer t.Stop()
for numFail+numSuccess != len(peers) {
select {
case <-waitAllCh:
numDone++
case <-waitFailCh:
numFail++
case <-waitSuccessCh:
if numSuccess >= numSuccessfulToWaitFor {
if !t.Stop() {
<-t.C
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will block the first time it's called because the timer is already stopped which means t.Stop() is false, and we've already emptied out the channel.

Doing it this way we need some boolean to check for the first time we've reset the timer. This follows roughly the same pattern as the pause detection timer in this function https://github.com/ipfs/go-ipfs-provider/blob/d391dae4a595473f6797eb5d5b803a529a7bbdbc/batched/system.go#L130

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nm, looks like we're discussing this in #719

}
t.Reset(time.Millisecond * 500)
}
numSuccess++
numDone++
case <-t.C:
cancel()
}
Expand Down