diff --git a/server/server.go b/server/server.go index b113890305b..3b463260496 100644 --- a/server/server.go +++ b/server/server.go @@ -2083,6 +2083,11 @@ func (s *Server) Start() { defer s.Noticef("Server is ready") + // Check is this may be a restart. + if s.startupComplete == nil { + s.startupComplete = make(chan struct{}) + } + // Check for insecure configurations. s.checkAuthforWarnings() @@ -2340,7 +2345,10 @@ func (s *Server) Start() { } // We've finished starting up. - close(s.startupComplete) + if s.startupComplete != nil { + close(s.startupComplete) + s.startupComplete = nil + } // Wait for clients. if !opts.DontListen { diff --git a/server/server_test.go b/server/server_test.go index 42b650ac69f..dfbfdba7bbd 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -2107,3 +2107,14 @@ func TestServerAuthBlockAndSysAccounts(t *testing.T) { _, err = nats.Connect(s.ClientURL()) require_Error(t, err, nats.ErrAuthorization, errors.New("nats: Authorization Violation")) } + +// https://github.com/nats-io/nats-server/issues/4789 +func TestServerRestartAfterShutdown(t *testing.T) { + opts := DefaultTestOptions + opts.Port = -1 + s := RunServer(&opts) + s.Shutdown() + s.WaitForShutdown() + s.Start() + defer s.Shutdown() +}