Skip to content

Commit

Permalink
Merge pull request #77 from hkalina/jkalina/race4-fix
Browse files Browse the repository at this point in the history
Fix race on BasePeerLeecher.done
  • Loading branch information
uprendis authored Jan 16, 2024
2 parents 2653b30 + 4b14945 commit a75735c
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions gossip/basestream/basestreamleecher/basepeerleecher/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package basepeerleecher
import (
"errors"
"sync"
"sync/atomic"
"time"
)

Expand Down Expand Up @@ -37,9 +38,8 @@ type BasePeerLeecher struct {

notifyReceivedChunk chan *receivedChunk

quitMu sync.Mutex
quit chan struct{}
done bool
quit chan struct{}
done uint32

wg *sync.WaitGroup

Expand Down Expand Up @@ -78,16 +78,14 @@ func (d *BasePeerLeecher) Stop() {
}

func (d *BasePeerLeecher) Terminate() {
d.quitMu.Lock()
defer d.quitMu.Unlock()
if !d.done {
// set done, close the chan if it was not done yet
if atomic.SwapUint32(&d.done, 1) == 0 {
close(d.quit)
d.done = true
}
}

func (d *BasePeerLeecher) Stopped() bool {
return d.done
return atomic.LoadUint32(&d.done) != 0
}

// NotifyChunkReceived injects new pack infos from a peer
Expand Down Expand Up @@ -115,8 +113,7 @@ func (d *BasePeerLeecher) loop() {
return

case op := <-d.notifyReceivedChunk:
if d.done {
d.Terminate()
if d.Stopped() {
continue
}
if len(d.processingChunks) < d.cfg.ParallelChunksDownload*2 {
Expand Down

0 comments on commit a75735c

Please sign in to comment.