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 { for {
select { select {
case i := <-in: case i, ok := <-in:
if !ok {
return
}
select { select {
case <-out: case <-out:
default: default:
@ -193,14 +196,22 @@ func NewRegistry(localID enode.ID, delivery *Delivery, syncChunkStore storage.Sy
return out return out
} }
go func() {
// wait for kademlia table to be healthy
time.Sleep(options.SyncUpdateDelay)
kad := streamer.delivery.kad 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()) depthC := latestIntC(kad.NeighbourhoodDepthC())
addressBookSizeC := latestIntC(kad.AddrCountC()) addressBookSizeC := latestIntC(kad.AddrCountC())
go func() {
// wait for kademlia table to be healthy
// but return if Registry is closed before
select {
case <-time.After(options.SyncUpdateDelay):
case <-quit:
return
}
// initial requests for syncing subscription to peers // initial requests for syncing subscription to peers
streamer.updateSyncing() streamer.updateSyncing()