From 33b65f943f218e7e7f91ae2108f1511ff4a36802 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Thu, 12 Sep 2013 17:24:37 -0500 Subject: [PATCH] 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. --- addrmanager.go | 2 +- blockmanager.go | 2 +- rpcserver.go | 2 +- server.go | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/addrmanager.go b/addrmanager.go index 394fd914..8eb76400 100644 --- a/addrmanager.go +++ b/addrmanager.go @@ -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 } diff --git a/blockmanager.go b/blockmanager.go index b793698c..0bfba57c 100644 --- a/blockmanager.go +++ b/blockmanager.go @@ -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 } diff --git a/rpcserver.go b/rpcserver.go index 201e47ab..d4552730 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -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 } diff --git a/server.go b/server.go index 12cfbdad..389a5276 100644 --- a/server.go +++ b/server.go @@ -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 {