Skip to content

Commit

Permalink
[FIX] Data race when setting up service import subscriptions. (#4068)
Browse files Browse the repository at this point in the history
Signed-off-by: Derek Collison <derek@nats.io>
  • Loading branch information
derekcollison committed Apr 17, 2023
2 parents 7709780 + 9a3e0b7 commit d8389ec
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
7 changes: 7 additions & 0 deletions server/accounts.go
Expand Up @@ -2017,7 +2017,14 @@ func (a *Account) removeAllServiceImportSubs() {

// Add in subscriptions for all registered service imports.
func (a *Account) addAllServiceImportSubs() {
var sis [32]*serviceImport
serviceImports := sis[:0]
a.mu.RLock()
for _, si := range a.imports.services {
serviceImports = append(serviceImports, si)
}
a.mu.RUnlock()
for _, si := range serviceImports {
a.addServiceImportSub(si)
}
}
Expand Down
10 changes: 10 additions & 0 deletions server/server.go
Expand Up @@ -1594,13 +1594,23 @@ func (s *Server) fetchAccount(name string) (*Account, error) {
}
// The sub imports may have been setup but will not have had their
// subscriptions properly setup. Do that here.
var needImportSubs bool

acc.mu.Lock()
if len(acc.imports.services) > 0 {
if acc.ic == nil {
acc.ic = s.createInternalAccountClient()
acc.ic.acc = acc
}
needImportSubs = true
}
acc.mu.Unlock()

// Do these outside the lock.
if needImportSubs {
acc.addAllServiceImportSubs()
}

return acc, nil
}

Expand Down

0 comments on commit d8389ec

Please sign in to comment.