[FIXED] Gateway: possible panic if monitor endpoint inspected too soon

The monitoring http server is started early and the gateway setup
(when configured) may not be fully ready when the `/gatewayz`
endpoint is inspected and could cause a panic.

Signed-off-by: Ivan Kozlovic <ivan@synadia.com>
This commit is contained in:
Ivan Kozlovic
2022-08-17 13:30:58 -06:00
parent c67d6aad79
commit 5d3ee8ebf4
3 changed files with 33 additions and 2 deletions

View File

@@ -603,7 +603,7 @@ func (g *srvGateway) generateInfoJSON() {
// We could be here when processing a route INFO that has a gateway URL,
// but this server is not configured for gateways, so simply ignore here.
// The configuration mismatch is reported somewhere else.
if !g.enabled {
if !g.enabled || g.info == nil {
return
}
g.info.GatewayURLs = g.URLs.getAsStringSlice()

View File

@@ -6749,3 +6749,34 @@ func TestGatewayDuplicateServerName(t *testing.T) {
// to cluster "A"
checkForDupError(scl.errCh)
}
func TestGatewayNoPanicOnStartupWithMonitoring(t *testing.T) {
o := testDefaultOptionsForGateway("B")
o.HTTPHost = "127.0.0.1"
o.HTTPPort = 8888
s, err := NewServer(o)
require_NoError(t, err)
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
time.Sleep(50 * time.Millisecond)
s.Start()
s.WaitForShutdown()
}()
for {
g, err := s.Gatewayz(nil)
if err != nil {
continue
}
if g.Port != 0 && g.Port != s.GatewayAddr().Port {
t.Fatalf("Unexpected port: %v vs %v", g.Port, s.GatewayAddr().Port)
}
break
}
s.Shutdown()
wg.Wait()
}

View File

@@ -1777,7 +1777,7 @@ func (s *Server) Gatewayz(opts *GatewayzOptions) (*Gatewayz, error) {
now := time.Now().UTC()
gw := s.gateway
gw.RLock()
if !gw.enabled {
if !gw.enabled || gw.info == nil {
gw.RUnlock()
gwz := &Gatewayz{
ID: srvID,