python / python/cpython

Incorrect MRO cache entry is populated for types that do not have a version assigned

Open
#128,108 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

interpreter-core type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

_PyType_LookupRefAndVersion will populate the incorrect cache entry if it is called with a type that does not have a version tag (tp_version_tag is 0). I think this only impacts performance, not correctness. The next call to _PyType_LookupRefAndVersion for the same (type, name) pair will miss the cache, but will populate the correct entry, so any subsequent calls will hit the cache. Additionally, this may create some amount of unnecessary thrash on the cache.

The root of the issue is that the version tag is an input to the hash that is used to determine the cache entry and we do not recompute the hash, and the associated cache entry, after assigning a version.

_PyType_LookupRefAndVersion first computes the cache entry:

https://github.com/python/cpython/blob/39e69a7cd54d44c9061db89bb15c460d30fba7a6/Objects/typeobject.c#L5542-L5544

If the cache entry doesn't match, and a type version was assigned successfully, it populates the same entry:

https://github.com/python/cpython/blob/39e69a7cd54d44c9061db89bb15c460d30fba7a6/Objects/typeobject.c#L5629-L5634

The next call for the same (type, name) pair, will find a different entry, because the type version is used to compute the hash that indexes into the cache:

https://github.com/python/cpython/blob/39e69a7cd54d44c9061db89bb15c460d30fba7a6/Objects/typeobject.c#L46-L48

It looks like the change to use the same entry was introduced in gh-113930. cc @DinoV

CPython versions tested on:

3.13, 3.14

Operating systems tested on:

Linux

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in Objects/typeobject.c at _PyType_LookupRefAndVersion and the cache hash calculation around the referenced lines. Trace the no-version and newly-assigned-version paths, then verify that repeated lookups use the cache entry indexed by the assigned version rather than leaving an unnecessary entry behind. A suitable regression check should demonstrate the corrected cache behavior without changing lookup results.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, python
Domain
performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.