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 on BasePeerLeecher.done #77

Merged
merged 1 commit into from
Jan 16, 2024
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
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