From 0e4c65d422b11891b9bf24f6443d92f27d559621 Mon Sep 17 00:00:00 2001 From: Matt Keeler Date: Wed, 3 Jun 2020 10:36:32 -0400 Subject: [PATCH] Fix segfault due to race condition for checking server versions (#7957) The ACL monitoring routine uses c.routers to check for server version updates. Therefore it needs to be started after initializing the routers. --- agent/consul/client.go | 10 ++++++---- agent/router/manager.go | 4 ++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/agent/consul/client.go b/agent/consul/client.go index bcaf5aac19..66acc45446 100644 --- a/agent/consul/client.go +++ b/agent/consul/client.go @@ -181,10 +181,6 @@ func NewClientLogger(config *Config, logger hclog.InterceptLogger, tlsConfigurat return nil, fmt.Errorf("Failed to start lan serf: %v", err) } - if c.acls.ACLsEnabled() { - go c.monitorACLMode() - } - // Start maintenance task for servers c.routers = router.New(c.logger, c.shutdownCh, c.serf, c.connPool, "") go c.routers.Start() @@ -193,6 +189,12 @@ func NewClientLogger(config *Config, logger hclog.InterceptLogger, tlsConfigurat // handlers depend on the router and the router depends on Serf. go c.lanEventHandler() + // This needs to happen after initializing c.routers to prevent a race + // condition where the router manager is used when the pointer is nil + if c.acls.ACLsEnabled() { + go c.monitorACLMode() + } + if err := c.startEnterprise(); err != nil { c.Shutdown() return nil, err diff --git a/agent/router/manager.go b/agent/router/manager.go index 7944e48d26..0940ee2367 100644 --- a/agent/router/manager.go +++ b/agent/router/manager.go @@ -235,6 +235,10 @@ func (m *Manager) FindServer() *metadata.Server { } func (m *Manager) checkServers(fn func(srv *metadata.Server) bool) bool { + if m == nil { + return true + } + for _, srv := range m.getServerList().servers { if !fn(srv) { return false