mirror of
https://github.com/gogrlx/nats-server.git
synced 2026-04-02 03:38:42 -07:00
[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:
@@ -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()
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user