Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #2515.
There were occasional failings of test runs in
tests/test_realtime.py
, particularly in CI, that would generally be resolved on re-running.The issue was that we used
id(settings)
as a cache key. In CPython this is simply the address of the object in memory, and in general is not guaranteed to be unique. When new objects are put in the same memory location as those that have been garbage-collected, we got cache collisions, resulting in the wrong SQL being returned.We use a slightly different approach for a cache key to how we check if we have some SQL already cached, depending on the type of object:
pathlib.Path
orstr
(representing saved models) we just use the string itselfdict
we use ajson
dump (possibly after converting to serialisable types)SettingsCreator
we use theid
still, but we also store a weak reference to the object. This won't prevent it being garbage collected, but allows us to see if the object still lives. If we find a supposèd cache hit, we check the associatedweakref
(which we also keep in the cache), and if it's dead (and thus we have the case of recycledid
values) we delete the entry and return as though we found no hit.The custom solutions for
str
anddict
are mainly because these cannot be (directly)weakref
ed.You can see in this PR test runs where we see that this
weakref
solution is triggered, and fixes the issue (with a hack to fail the tests on a separate condition, which occurs only on that logic branch).