[FIX] Data race when setting up service import subscriptions. (#4068)

Signed-off-by: Derek Collison <derek@nats.io>
This commit is contained in:
Derek Collison
2023-04-17 06:57:27 -07:00
committed by GitHub
2 changed files with 17 additions and 0 deletions

View File

@@ -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)
}
}

View File

@@ -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
}