Fix deadlock in cluster shutdown

This commit is contained in:
Ingo Oppermann 2024-09-17 15:08:11 +02:00
parent 705c7fa946
commit 0f6d7949c4
No known key found for this signature in database
GPG Key ID: 2AB32426E9DD229E

View File

@ -620,7 +620,7 @@ func (c *cluster) CertManager() autocert.Manager {
}
func (c *cluster) Shutdown() error {
c.logger.Info().Log("Shutting down cluster")
c.logger.Info().Log("Shutting down cluster ...")
c.shutdownLock.Lock()
defer c.shutdownLock.Unlock()
@ -631,9 +631,14 @@ func (c *cluster) Shutdown() error {
c.shutdown = true
close(c.shutdownCh)
c.logger.Info().Log("Waiting for all routines to exit ...")
c.shutdownWg.Wait()
c.logger.Info().Log("All routines exited ...")
if c.manager != nil {
c.logger.Info().Log("Shutting down node manager ...")
c.manager.NodesClear()
}
@ -641,13 +646,18 @@ func (c *cluster) Shutdown() error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
c.logger.Info().Log("Shutting down API ...")
c.api.Shutdown(ctx)
}
if c.raft != nil {
c.logger.Info().Log("Shutting down raft ...")
c.raft.Shutdown()
}
c.logger.Info().Log("Cluster stopped")
return nil
}
@ -1029,7 +1039,7 @@ func (c *cluster) trackLeaderChanges() {
if !isNodeInCluster {
// We're not anymore part of the cluster, shutdown
c.logger.Warn().WithField("id", c.nodeID).Log("This node left the cluster. Shutting down.")
c.Shutdown()
go c.Shutdown()
}
case <-c.shutdownCh: