-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Don't cache SublistResult
if from MatchWithResult
call
#5324
Conversation
Otherwise the cache might end up with multiple cache entries mangling the same underlying memory. Signed-off-by: Neil Twigg <neil@nats.io>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, but there is still some possibility of misusing this, which will lead to weird bugs.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By the way, the other danger is that - although in this PR we make sure we don't store the result in the cache - we still may get a cache hit from a previously stored result (from some other code doing a regular match, with result being cached). If we get a hit, we will return the result from the cache, which is fine with the way you have use it in the other PR. However, if later some dev thinks that the returned result is the same than the pointer to the SublistResult from the stack, bad things will happen. Say:
r := &SublistResult{}
for _, subj := range []subjectsToCheck {
if r = sl.MatchWithResult(subj, r); len(r.psubs) > 0 {
// do something
}
}
The code above would be bad if one result was returned from a cache hit, because at the next invocation in the loop, we would reset the cache result's content.
This is a far-fetched misuse, but we have to be mindful of the consequences.
Where are we on this issue? I think a bool check for interest that does not add to the cache might be worth considering. |
This current PR should be merged for now since it fixes the previous one by preventing adding the stack result to the cache. But even with the fix, as I mentioned in this PR, I think there is still a risk of misuse (albeit low). But since the use case so far is just to check interest, I think we could add a I would be happy to work on that later today unless @neilalexander is planning or has already started. The question would be - if we add the "has interest" function - do we still keep |
I like the idea of Will let you and @neilalexander decide how to proceed but should have something before we release 2.10.15 IMO. |
I actually already have |
Should we merge this one and do a follow on? |
@neilalexander I think Actually, part of the test would be that calling HasInterest() first would leave the cache empty, but if calling Match() and then HasInterest(), the cache hit should increase to show that HasInterest() makes use of the cache. |
Suggest we merge this for now, given it's safe in the context that it's being used in, and we'll take it back out once we have worked out a good caching strategy for a
Agreed, that is easily done for sure. |
Otherwise the cache might end up with multiple cache entries sharing the same underlying memory.
Signed-off-by: Neil Twigg neil@nats.io