Skip to content

Commit

Permalink
Add reload lock to ensure new connections await completion of config …
Browse files Browse the repository at this point in the history
…reload (#5207)

This may help with some problems during configuration reload with e.g.
OCSP stapling.

Signed-off-by: Neil Twigg <neil@nats.io>
  • Loading branch information
derekcollison committed Mar 12, 2024
2 parents 0364a06 + 0575f9e commit 9fa359a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions locksordering.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@ possible with the normal account lock.
accountLeafList -> client

AccountResolver interface has various implementations, but assume: AccountResolver -> Server

A reloadMu lock was added to prevent newly connecting clients racing with the configuration reload.
This must be taken out as soon as a reload is about to happen before any other locks:

reloadMu -> Server
reloadMu -> optsMu
3 changes: 3 additions & 0 deletions server/reload.go
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,9 @@ func (s *Server) Reload() error {
// type. This returns an error if an option which doesn't support
// hot-swapping was changed.
func (s *Server) ReloadOptions(newOpts *Options) error {
s.reloadMu.Lock()
defer s.reloadMu.Unlock()

s.mu.Lock()

curOpts := s.getOpts()
Expand Down
3 changes: 3 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ type Server struct {
stats
scStats
mu sync.RWMutex
reloadMu sync.RWMutex // Write-locked when a config reload is taking place ONLY
kp nkeys.KeyPair
xkp nkeys.KeyPair
xpub string
Expand Down Expand Up @@ -2727,7 +2728,9 @@ func (s *Server) acceptConnections(l net.Listener, acceptName string, createFunc
}
tmpDelay = ACCEPT_MIN_SLEEP
if !s.startGoRoutine(func() {
s.reloadMu.RLock()
createFunc(conn)
s.reloadMu.RUnlock()
s.grWG.Done()
}) {
conn.Close()
Expand Down

0 comments on commit 9fa359a

Please sign in to comment.