Fix datarace for Peers.melted

Using the boolean value was unnecessary since we already have a channel
we can check for closure.
This commit is contained in:
Cecylia Bocovich 2021-06-17 17:42:22 -04:00
parent ddcdfc4f09
commit bb7ff6180b

View file

@ -27,7 +27,6 @@ type Peers struct {
activePeers *list.List activePeers *list.List
melt chan struct{} melt chan struct{}
melted bool
collection sync.WaitGroup collection sync.WaitGroup
} }
@ -51,8 +50,10 @@ func (p *Peers) Collect() (*WebRTCPeer, error) {
// Engage the Snowflake Catching interface, which must be available. // Engage the Snowflake Catching interface, which must be available.
p.collection.Add(1) p.collection.Add(1)
defer p.collection.Done() defer p.collection.Done()
if p.melted { select {
case <-p.melt:
return nil, fmt.Errorf("Snowflakes have melted") return nil, fmt.Errorf("Snowflakes have melted")
default:
} }
if nil == p.Tongue { if nil == p.Tongue {
return nil, errors.New("missing Tongue to catch Snowflakes with") return nil, errors.New("missing Tongue to catch Snowflakes with")
@ -120,7 +121,6 @@ func (p *Peers) purgeClosedPeers() {
// Close all Peers contained here. // Close all Peers contained here.
func (p *Peers) End() { func (p *Peers) End() {
close(p.melt) close(p.melt)
p.melted = true
p.collection.Wait() p.collection.Wait()
close(p.snowflakeChan) close(p.snowflakeChan)
cnt := p.Count() cnt := p.Count()