Merge pull request #1276 from nats-io/fix_lame_duck_mode_shutdown

[FIXED] Server that is sent the lame duck mode signal does not exit
This commit is contained in:
Ivan Kozlovic
2020-02-10 18:13:49 -07:00
committed by GitHub
2 changed files with 13 additions and 3 deletions

View File

@@ -17,7 +17,6 @@ import (
"flag"
"fmt"
"os"
"runtime"
"github.com/nats-io/nats-server/v2/server"
)
@@ -110,5 +109,5 @@ func main() {
if err := server.Run(s); err != nil {
server.PrintAndDie(err.Error())
}
runtime.Goexit()
s.WaitForShutdown()
}

View File

@@ -139,7 +139,8 @@ type Server struct {
dialTimeout time.Duration
}
quitCh chan struct{}
quitCh chan struct{}
shutdownComplete chan struct{}
// Tracking Go routines
grMu sync.Mutex
@@ -323,6 +324,9 @@ func NewServer(opts *Options) (*Server, error) {
// Used to kick out all go routines possibly waiting on server
// to shutdown.
s.quitCh = make(chan struct{})
// Closed when Shutdown() is complete. Allows WaitForShutdown() to block
// waiting for complete shutdown.
s.shutdownComplete = make(chan struct{})
// For tracking accounts
if err := s.configureAccounts(); err != nil {
@@ -1334,6 +1338,13 @@ func (s *Server) Shutdown() {
l.Close()
}
}
// Notify that the shutdown is complete
close(s.shutdownComplete)
}
// WaitForShutdown will block until the server has been fully shutdown.
func (s *Server) WaitForShutdown() {
<-s.shutdownComplete
}
// AcceptLoop is exported for easier testing.