Make the MRO cache thread-safe in free-threaded builds · Issue #113743 · python/cpython · GitHub
Skip to content

Make the MRO cache thread-safe in free-threaded builds #113743

Description

@colesbury

Feature or enhancement

The MRO (method resolution order) cache is used to cache lookups on the type hierarchy, such as method lookups. It's currently implemented per-interpreter, which will not be thread-safe in free-threaded builds.

cpython/Objects/typeobject.c

Lines 4750 to 4803 in 5e1916b

The nogil-3.9 and nogil-3.12 forks used different approaches to make the MRO cache thread-safe. The nogil-3.9 fork moved the type cache to the PyThreadState. The nogil-3.12 fork implemented a thread-safe cache shared across threads 1. The nogil-3.9 approach is much simpler, I think we should start with that.

Suggested approach:

  • If Py_GIL_DISABLED is defined, move the type_cache to the private struct PyThreadStateImpl.
  • Refactor _PyType_Lookup() as necessary to use the correct cache

Linked PRs

Footnotes

  1. For reference, here is the nogil-3.12 implementation: https://github.com/colesbury/nogil-3.12/commit/9c1f7ba1b4

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions