Fix to prevent hash()/id() collisions in the memoizer - #4901
Open
pjfo wants to merge 1 commit into
Open
Conversation
The _Memoizer records processed objects in a single set of raw ints that mixes hash() values with id() addresses. A hash() value that numerically equals a recorded id() address (or vice versa) collapses a never-processed object to the already-processed placeholder, changing the serialized JSON and thus the play hash. Because str hashes are randomized per process, whether such a coincidence occurs varies between otherwise identical runs, so a cached scene can silently re-render partial movie files.
Tag each membership sign with its signature kind ("h" for hash, "i" for id) so the two kinds can never collide, and add a regression test covering both collision directions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Overview: What does this pull request change?
Tag each membership sign with its signature kind ("h" for hash, "i" for id) so the two kinds can never collide, and add a regression test covering both collision directions.
The test fails on the current main branch, but succeeds on this branch.
Motivation and Explanation: Why and how do your changes improve the library?
This is a separate bug to the one found in PR 4896: Hashing stability for transient objects in manim 0.20.1.
The _Memoizer class mixes two classes of ints when it is processing, hashes and id addresses. If a hash matches a recorded id or vice-versa then the current behaviour is to record a potentially never recorded object to the already processed placeholder, which changes the serialisation of the json and the play hash.
Because str hashes are randomised per process, whether such a coincidence occurs varies between otherwise identical runs, so a cached scene can silently re-render partial movie files.
Links to added or changed documentation pages
Further Information and Comments
Depending on which (if either) are merged first between this and PR 4896, a trivial rebase will be needed on the other, but even though the code is adjacent it does not conflict.
Reviewer Checklist