Increment waitgroups before goroutines are started.
This commit changes the code so that all calls to .Add on waitgroups happen before the associated goroutines are launched. Doing this after the goroutine could technically cause a race where the goroutine started and finished before the main goroutine has a chance to increment the counter. In our particular case none of the goroutines exit quickly enough for this to be an issue, but nevertheless the correct way should be used.
This commit is contained in:
parent
69efe46eb5
commit
33b65f943f
4 changed files with 5 additions and 5 deletions
|
@ -103,8 +103,8 @@ func (a *AddrManager) Start() {
|
|||
|
||||
log.Trace("[AMGR] Starting address manager")
|
||||
|
||||
go a.addressHandler()
|
||||
a.wg.Add(1)
|
||||
go a.addressHandler()
|
||||
a.started = true
|
||||
}
|
||||
|
||||
|
|
|
@ -350,10 +350,10 @@ func (b *blockManager) Start() {
|
|||
}
|
||||
|
||||
log.Trace("[BMGR] Starting block manager")
|
||||
b.wg.Add(3)
|
||||
go b.syncHandler()
|
||||
go b.blockHandler()
|
||||
go b.chainNotificationHandler()
|
||||
b.wg.Add(3)
|
||||
b.started = true
|
||||
}
|
||||
|
||||
|
|
|
@ -44,13 +44,13 @@ func (s *rpcServer) Start() {
|
|||
})
|
||||
httpServer := &http.Server{}
|
||||
for _, listener := range s.listeners {
|
||||
s.wg.Add(1)
|
||||
go func(listener net.Listener) {
|
||||
log.Infof("[RPCS] RPC server listening on %s", listener.Addr())
|
||||
httpServer.Serve(listener)
|
||||
log.Tracef("[RPCS] RPC listener done for %s", listener.Addr())
|
||||
s.wg.Done()
|
||||
}(listener)
|
||||
s.wg.Add(1)
|
||||
}
|
||||
s.started = true
|
||||
}
|
||||
|
|
|
@ -337,14 +337,14 @@ func (s *server) Start() {
|
|||
// Start all the listeners. There will not be any if listening is
|
||||
// disabled.
|
||||
for _, listener := range s.listeners {
|
||||
go s.listenHandler(listener)
|
||||
s.wg.Add(1)
|
||||
go s.listenHandler(listener)
|
||||
}
|
||||
|
||||
// Start the peer handler which in turn starts the address and block
|
||||
// managers.
|
||||
go s.peerHandler()
|
||||
s.wg.Add(1)
|
||||
go s.peerHandler()
|
||||
|
||||
// Start the RPC server if it's not disabled.
|
||||
if !cfg.DisableRpc {
|
||||
|
|
Loading…
Reference in a new issue