diff --git a/command/server.go b/command/server.go index 1e1d39b6e..f7b0ce9a9 100644 --- a/command/server.go +++ b/command/server.go @@ -437,6 +437,7 @@ func (c *ServerCommand) Run(args []string) int { } } + // Wait for dependant goroutines to complete c.WaitGroup.Wait() return 0 } diff --git a/physical/consul.go b/physical/consul.go index 754a314c3..450c3bcc9 100644 --- a/physical/consul.go +++ b/physical/consul.go @@ -421,6 +421,7 @@ func (c *ConsulBackend) RunServiceDiscovery(shutdownTriggered *bool, waitGroup * return err } + // 'server' command will wait for the belog goroutine to complete waitGroup.Add(1) go c.runEventDemuxer(shutdownTriggered, waitGroup, shutdownCh, advertiseAddr, activeFunc, sealedFunc) @@ -429,6 +430,9 @@ func (c *ConsulBackend) RunServiceDiscovery(shutdownTriggered *bool, waitGroup * } func (c *ConsulBackend) runEventDemuxer(shutdownTriggered *bool, waitGroup *sync.WaitGroup, shutdownCh ShutdownChannel, advertiseAddr string, activeFunc activeFunction, sealedFunc sealedFunction) { + // This defer statement should be executed last. So push it first. + defer waitGroup.Done() + // Fire the reconcileTimer immediately upon starting the event demuxer reconcileTimer := time.NewTimer(0) defer reconcileTimer.Stop() @@ -519,7 +523,6 @@ shutdown: if err := c.client.Agent().ServiceDeregister(registeredServiceID); err != nil { c.logger.Printf("[WARN]: physical/consul: service deregistration failed: %v", err) } - defer waitGroup.Done() } // checkID returns the ID used for a Consul Check. Assume at least a read