Closed
Description
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.
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 structPyThreadStateImpl
. - Refactor
_PyType_Lookup()
as necessary to use the correct cache
Linked PRs
- gh-113743: Make the MRO cache thread-safe in free-threaded builds #113930
- gh-113743: Use per-interpreter locks for types #115541
- gh-113743: Give _PyTypes_AfterFork a prototype. #115563
Footnotes
-
For reference, here is the nogil-3.12 implementation: https://github.com/colesbury/nogil-3.12/commit/9c1f7ba1b4 ↩