diff --git a/server/reload_test.go b/server/reload_test.go index a44e6e0f0d7..a16f4e1769d 100644 --- a/server/reload_test.go +++ b/server/reload_test.go @@ -5427,6 +5427,41 @@ func TestConfigReloadRoutePoolAndPerAccount(t *testing.T) { } } +func TestConfigReloadRoutePoolAndPerAccountNoPanicIfFirstAdded(t *testing.T) { + tmpl := ` + port: -1 + server_name: "%s" + accounts { + A { users: [{user: "user1", password: "pwd"}] } + B { users: [{user: "user2", password: "pwd"}] } + } + cluster { + name: "local" + listen: 127.0.0.1:-1 + pool_size: 2 + %s + %s + } + no_sys_acc: true + ` + conf1 := createConfFile(t, fmt.Appendf(nil, tmpl, "A", _EMPTY_, _EMPTY_)) + s1, o1 := RunServerWithConfig(conf1) + defer s1.Shutdown() + + route := fmt.Sprintf("routes: [\"nats://127.0.0.1:%d\"]", o1.Cluster.Port) + conf2 := createConfFile(t, fmt.Appendf(nil, tmpl, "B", _EMPTY_, route)) + s2, _ := RunServerWithConfig(conf2) + defer s2.Shutdown() + + checkClusterFormed(t, s1, s2) + + reloadUpdateConfig(t, s1, conf1, fmt.Sprintf(tmpl, "A", "accounts:[\"A\"]", _EMPTY_)) + reloadUpdateConfig(t, s2, conf2, fmt.Sprintf(tmpl, "B", "accounts:[\"A\"]", route)) + + time.Sleep(50 * time.Millisecond) + checkClusterFormed(t, s1, s2) +} + func TestConfigReloadRoutePoolCannotBeDisabledIfAccountsPresent(t *testing.T) { tmpl := ` port: -1 diff --git a/server/route.go b/server/route.go index 45be8eed44e..d56cdf9728f 100644 --- a/server/route.go +++ b/server/route.go @@ -572,6 +572,12 @@ func (c *client) processRouteInfo(info *Info) { return } s.mu.Lock() + // If running without system account and adding a dedicated + // route for an account for the first time, it could be that + // the map is nil. If so, create it. + if s.accRoutes == nil { + s.accRoutes = make(map[string]map[string]*client) + } if _, ok := s.accRoutes[an]; !ok { s.accRoutes[an] = make(map[string]*client) }