swarm/network/stream: fix sync between NewRegistry goroutine and Close method

This commit is contained in:
Janos Guljas 2019-02-19 17:28:04 +01:00
parent 7579260cc0
commit 77b0599fd1

View file

@ -178,7 +178,10 @@ func NewRegistry(localID enode.ID, delivery *Delivery, syncChunkStore storage.Sy
for {
select {
case i := <-in:
case i, ok := <-in:
if !ok {
return
}
select {
case <-out:
default:
@ -193,13 +196,21 @@ func NewRegistry(localID enode.ID, delivery *Delivery, syncChunkStore storage.Sy
return out
}
kad := streamer.delivery.kad
// get notification channels from Kademlia before returning
// from this function to avoid race with Close method and
// the goroutine created below
depthC := latestIntC(kad.NeighbourhoodDepthC())
addressBookSizeC := latestIntC(kad.AddrCountC())
go func() {
// wait for kademlia table to be healthy
time.Sleep(options.SyncUpdateDelay)
kad := streamer.delivery.kad
depthC := latestIntC(kad.NeighbourhoodDepthC())
addressBookSizeC := latestIntC(kad.AddrCountC())
// but return if Registry is closed before
select {
case <-time.After(options.SyncUpdateDelay):
case <-quit:
return
}
// initial requests for syncing subscription to peers
streamer.updateSyncing()