Repository navigation
Fix stale bindings from DynamicKeyBindings when ids are reused - #2166
Open
hailsaturn wants to merge 3 commits into
Open
hailsaturn wants to merge 3 commits into
hailsaturn wants to merge 3 commits into
Conversation
Previously DynamicKeyBindings used (id(kb), kb._version) for its version. Since id(kb) can be reused if kb is freed, it is possible for different objects with the same number of bindings to produce an identical version and _MergedKeyBindings was serving the stale bindings. The fix instead tracks a counter that increases whenever the callback returns a different object.
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.
Summary
Fix cache invalidation for
DynamicKeyBindingswhen its callback returns a differentKeyBindingsobject with the sameid()and_versionas a previously returned object.Fixes #2165
DynamicKeyBindingsnow tracks a counter that increments whenever the callback returns a different object (checked withis), and reports(counter, key_bindings._version)instead of usingid(). See #2165 for more detail.An unused variable
_last_child_versionwas also removed from theDynamicKeyBindingsconstructor. Also,_bindings2is initialised so the first call has something to compare against.Testing
Since the issue depends on the allocator, a regression test in
tests/test_key_binding.pypatchesidto always return 0. This forces theid()collision behaviour and verifies that successive dynamic bindings are not served from a stale cache.The added regression test fails on
mainand passes with this change.The existing full suite,
ruff check,ruff format --checkandmypy --strictall pass.