Browse Source

consensus: wait until peerUpdates channel is closed to close remaining peers (#7058) (#7060)

The race occurred as a result of a goroutine launched by `processPeerUpdate` racing with the `OnStop` method. The `processPeerUpdates` goroutine deletes from the map as `OnStop` is reading from it. This change updates the `OnStop` method to wait for the peer updates channel to be done before closing the peers. It also copies the map contents to a new map so that it will not conflict with the view of the map that the goroutine created in `processPeerUpdate` sees.
pull/7063/head
William Banfield 3 years ago
committed by GitHub
parent
commit
42ed5d75a5
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 9 deletions
  1. +7
    -9
      internal/consensus/reactor.go

+ 7
- 9
internal/consensus/reactor.go View File

@ -230,16 +230,14 @@ func (r *Reactor) OnStop() {
}
r.mtx.Lock()
peers := r.peers
r.mtx.Unlock()
// wait for all spawned peer goroutines to gracefully exit
for _, ps := range peers {
ps.closer.Close()
}
for _, ps := range peers {
ps.broadcastWG.Wait()
// Close and wait for each of the peers to shutdown.
// This is safe to perform with the lock since none of the peers require the
// lock to complete any of the methods that the waitgroup is waiting on.
for _, state := range r.peers {
state.closer.Close()
state.broadcastWG.Wait()
}
r.mtx.Unlock()
// Close the StateChannel goroutine separately since it uses its own channel
// to signal closure.


Loading…
Cancel
Save