diff --git a/command/server.go b/command/server.go index 079ca6db0..df05bc59a 100644 --- a/command/server.go +++ b/command/server.go @@ -375,8 +375,6 @@ func (c *ServerCommand) Run(args []string) int { // Instantiate the wait group c.WaitGroup = &sync.WaitGroup{} - // Wait for shutdown - shutdownTriggered := false // If the backend supports service discovery, run service discovery if coreConfig.HAPhysical != nil && coreConfig.HAPhysical.HAEnabled() { @@ -396,7 +394,7 @@ func (c *ServerCommand) Run(args []string) int { return true } - if err := sd.RunServiceDiscovery(&shutdownTriggered, c.WaitGroup, c.ShutdownCh, coreConfig.AdvertiseAddr, activeFunc, sealedFunc); err != nil { + if err := sd.RunServiceDiscovery(c.WaitGroup, c.ShutdownCh, coreConfig.AdvertiseAddr, activeFunc, sealedFunc); err != nil { c.Ui.Error(fmt.Sprintf("Error initializing service discovery: %v", err)) return 1 } @@ -421,6 +419,9 @@ func (c *ServerCommand) Run(args []string) int { // Release the log gate. logGate.Flush() + // Wait for shutdown + shutdownTriggered := false + for !shutdownTriggered { select { case <-c.ShutdownCh: @@ -757,10 +758,8 @@ func MakeShutdownCh() chan struct{} { shutdownCh := make(chan os.Signal, 4) signal.Notify(shutdownCh, os.Interrupt, syscall.SIGTERM) go func() { - for { - <-shutdownCh - resultCh <- struct{}{} - } + <-shutdownCh + close(resultCh) }() return resultCh } diff --git a/physical/consul.go b/physical/consul.go index 9c70f6f82..5f88a3194 100644 --- a/physical/consul.go +++ b/physical/consul.go @@ -416,7 +416,7 @@ func (c *ConsulBackend) checkDuration() time.Duration { return lib.DurationMinusBuffer(c.checkTimeout, checkMinBuffer, checkJitterFactor) } -func (c *ConsulBackend) RunServiceDiscovery(shutdownTriggered *bool, waitGroup *sync.WaitGroup, shutdownCh ShutdownChannel, advertiseAddr string, activeFunc activeFunction, sealedFunc sealedFunction) (err error) { +func (c *ConsulBackend) RunServiceDiscovery(waitGroup *sync.WaitGroup, shutdownCh ShutdownChannel, advertiseAddr string, activeFunc activeFunction, sealedFunc sealedFunction) (err error) { if err := c.setAdvertiseAddr(advertiseAddr); err != nil { return err } @@ -424,12 +424,12 @@ func (c *ConsulBackend) RunServiceDiscovery(shutdownTriggered *bool, waitGroup * // 'server' command will wait for the below goroutine to complete waitGroup.Add(1) - go c.runEventDemuxer(shutdownTriggered, waitGroup, shutdownCh, advertiseAddr, activeFunc, sealedFunc) + go c.runEventDemuxer(waitGroup, shutdownCh, advertiseAddr, activeFunc, sealedFunc) return nil } -func (c *ConsulBackend) runEventDemuxer(shutdownTriggered *bool, waitGroup *sync.WaitGroup, shutdownCh ShutdownChannel, advertiseAddr string, activeFunc activeFunction, sealedFunc sealedFunction) { +func (c *ConsulBackend) runEventDemuxer(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() @@ -456,7 +456,7 @@ func (c *ConsulBackend) runEventDemuxer(shutdownTriggered *bool, waitGroup *sync var checkLock int64 var registeredServiceID string var serviceRegLock int64 -shutdown: + for !shutdown { select { case <-c.notifyActiveCh: @@ -513,9 +513,7 @@ shutdown: case <-shutdownCh: c.logger.Printf("[INFO]: physical/consul: Shutting down consul backend") shutdown = true - break shutdown } - shutdown = *shutdownTriggered } c.serviceLock.RLock() diff --git a/physical/physical.go b/physical/physical.go index e563e80e3..9e96beb6d 100644 --- a/physical/physical.go +++ b/physical/physical.go @@ -72,7 +72,7 @@ type ServiceDiscovery interface { // Run executes any background service discovery tasks until the // shutdown channel is closed. - RunServiceDiscovery(shutdownTriggered *bool, waitGroup *sync.WaitGroup, shutdownCh ShutdownChannel, advertiseAddr string, activeFunc activeFunction, sealedFunc sealedFunction) error + RunServiceDiscovery(waitGroup *sync.WaitGroup, shutdownCh ShutdownChannel, advertiseAddr string, activeFunc activeFunction, sealedFunc sealedFunction) error } type Lock interface {