lazy_image: fix cache collisions leading to unrelated data being returned #443
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.
Summary
Currently, the key used to look up the cached image is based on a hash of a tuple containing
id(self)
,path
, andloader
. This means there are two situations in which alazy_image
can look up the wrong data:If there previously existed another
lazy_image
object whoseself
andloader
had the same object IDs as the currentlazy_image
'sself
andloader
. This is possible, because deleted objects' IDs can be reused.If a hash collision occurs between the current
lazy_image
's tuple and some other's.Fix it by using a weak reference to
self
as the key instead. Different weak references will only compare equal if they point to the same object.This will only work correctly if the loader and path of a
lazy_image
are not modified after creation. I don't think there are any use cases for modifying them (and there are no instances of that happening in the codebase), so it shouldn't be an issue. To reduce the temptation of client code to modify these fields, mark them as private.Modifying the
cache
field should not cause issues, but just in case, make it private as well.Fixes #409 (probably; I never managed to reliably reproduce it)
How to test
Checklist
develop
branchLicense
Feel free to contact the maintainers if that's a concern.