Use try/hash() to detect uncacheable args in process_cached#38389
Use try/hash() to detect uncacheable args in process_cached#38389Chessing234 wants to merge 1 commit intoopenedx:masterfrom
Conversation
process_cached.__call__ guarded the cache lookup with
if not isinstance(args, collections.abc.Hashable):
but `args` is always a tuple, and tuple itself is registered as Hashable.
That check therefore passes even when the tuple contains an unhashable
element (list, dict, set), so the subsequent `args in self.cache` lookup
raises `TypeError: unhashable type: 'list'` - exactly the failure the
comment "uncacheable. a list, for instance. better to not cache than
blow up." claims to prevent.
Switch to the `try: hash(args) / except TypeError:` pattern, which
actually probes the container's element-wise hashability. Drop the
now-unused `import collections`.
|
Thanks for the pull request, @Chessing234! This repository is currently maintained by Once you've gone through the following steps feel free to tag them in a comment and let them know that your changes are ready for engineering review. 🔘 Get product approvalIf you haven't already, check this list to see if your contribution needs to go through the product review process.
🔘 Provide contextTo help your reviewers and other members of the community understand the purpose and larger context of your changes, feel free to add as much of the following information to the PR description as you can:
🔘 Submit a signed contributor agreement (CLA)
If you've signed an agreement in the past, you may need to re-sign. Once you've signed the CLA, please allow 1 business day for it to be processed. 🔘 Get a green buildIf one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green. DetailsWhere can I find more information?If you'd like to get more details on all aspects of the review process for open source pull requests (OSPRs), check out the following resources: When can I expect my changes to be merged?Our goal is to get community contributions seen and reviewed as efficiently as possible. However, the amount of time that it takes to review and merge a PR can vary significantly based on factors such as:
💡 As a result it may take up to several weeks or months to complete a review and merge your PR. |
|
Hi @Chessing234! As mentioned in the other pull request you opened, please make sure to submit a CLA form so we can get this contribution review for you. Thanks! |
Bug
process_cached.__call__crashes withTypeError: unhashable type: 'list'when the decorated function is invoked with an unhashableargument, even though the code has an explicit guard intended to fall
back to an uncached call in that exact case.
Root cause
argsis always a tuple (Python's*argscollection), andtupleis registered ascollections.abc.Hashable.isinstance(..., Hashable)therefore answers "yes, hashable" for any tuple, includingtuples whose elements are not actually hashable (
([1, 2, 3],),({"k": 1},), etc.). The guard passes, execution continues intoargs in self.cache, and the dict lookup attemptshash(args)- which recurses into the list element and raisesTypeError.The comment ("uncacheable. a list, for instance. better to not cache
than blow up.") documents the intended behaviour, so the bug is
specifically in the check, not the fallback.
Why the fix is correct
hash(args)is whatargs in self.cachewill call internally, soattempting it in a
tryis the faithful probe: if the lookup wouldcrash, so does this, and we fall through to the uncached
return self.func(*args)path.HashableABC docs) explicitlycall out this tuple-of-list case as the reason
isinstance(..., Hashable)is insufficient.argsis truly hashable:hash(args)succeeds, the caching path runs as before.
import collections.Change
openedx/core/lib/cache_utils.py: replace theisinstance(..., collections.abc.Hashable)check inprocess_cached.__call__with atry: hash(args) / except TypeError:guard; remove the unusedcollectionsimport.