Skip to content

Commit

Permalink
Don't cache SublistResult if from MatchWithResult call (#5324)
Browse files Browse the repository at this point in the history
Otherwise the cache might end up with multiple cache entries sharing the
same underlying memory.

Signed-off-by: Neil Twigg <neil@nats.io>
  • Loading branch information
derekcollison committed Apr 22, 2024
2 parents 0aa65d3 + cd36785 commit 1007c14
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions server/sublist.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ var emptyResult = &SublistResult{}
// Match will match all entries to the literal subject.
// It will return a set of results for both normal and queue subscribers.
func (s *Sublist) Match(subject string) *SublistResult {
return s.match(subject, true, &SublistResult{})
return s.match(subject, true, nil)
}

// MatchWithResult will match all entries to the literal subject, reusing the
Expand All @@ -538,7 +538,7 @@ func (s *Sublist) MatchWithResult(subject string, result *SublistResult) *Sublis
}

func (s *Sublist) matchNoLock(subject string) *SublistResult {
return s.match(subject, false, &SublistResult{})
return s.match(subject, false, nil)
}

func (s *Sublist) match(subject string, doLock bool, result *SublistResult) *SublistResult {
Expand All @@ -548,7 +548,9 @@ func (s *Sublist) match(subject string, doLock bool, result *SublistResult) *Sub
if doLock {
s.RLock()
}
cacheEnabled := s.cache != nil
// Writing to the cache is only allowed if not supplying our
// own SublistResult, i.e. via call to MatchWithResult.
cacheEnabled := result == nil && s.cache != nil
r, ok := s.cache[subject]
if doLock {
s.RUnlock()
Expand Down Expand Up @@ -576,6 +578,9 @@ func (s *Sublist) match(subject string, doLock bool, result *SublistResult) *Sub
tokens = append(tokens, subject[start:])

// FIXME(dlc) - Make shared pool between sublist and client readLoop?
if result == nil {
result = &SublistResult{}
}
result.psubs = result.psubs[:0]
result.qsubs = result.qsubs[:0]

Expand Down

0 comments on commit 1007c14

Please sign in to comment.