Skip to content

Disable type caching for custom MRO hierarchies - #9477

Merged
youknowone merged 2 commits into
RustPython:mainfrom
youknowdot:py315-custom-mro-cache-20261030
Oct 30, 2026
Merged

youknowone merged 2 commits into
RustPython:mainfrom
youknowdot:py315-custom-mro-cache-20261030

Conversation

@youknowdot

Copy link
Copy Markdown
Contributor

Summary

Prevent cached type attributes from becoming stale when a custom MRO includes a class outside the physical base hierarchy.

  • Track permanent version-tag ineligibility separately from temporary invalidation, and refuse version assignment for affected types and their descendants.
  • Re-evaluate eligibility after initial custom-MRO publication and __bases__ changes, using raw metaclass mro descriptor identity and CPython's MRO/base subtype conditions.
  • Retain current MCACHE clearing and specialization invalidation; this does not depend on the separate cache-invalidation optimization in perf: avoid scanning the type cache on type mutation #8628.
  • Restore canonical test_metaclass.py with scripts/update_lib migrate, removing all three skipped attribute reads and the custom doctest loader. No test assertions were edited manually.

CPython basis

Compared directly with Objects/typeobject.c at d220409a13b77de7a6c30abe16ea90db01528edc: has_custom_mro, type_mro_modified, mro_internal and assign_version_tag.

The permanent eligibility state mirrors CPython's _Py_ATTR_CACHE_UNUSED sentinel, including conservative exclusion after a failed bases transition. Raw lookup bypasses descriptor binding, but dictionary key comparisons can still invoke callbacks; this patch makes no general callback-free or concurrency-safety claim.

Validation

  • Scoped locked/offline check, strict Clippy and CLI build passed.
  • Pinned CPython and RustPython produced identical transcripts for seven ordinary lookup/bases-transition observations. Each passed all 73 canonical metaclass doctest examples and the two existing finite rollback/conflict tests, without skips or expected failures.
  • Normal configured hooks passed, including cspell. Committed source is byte-identical to validated source.
  • Whole suites, GC, threaded, finalizer and historical-crash probes were not run locally. CI is pending.

AI assistance

OpenAI Codex assisted with implementation, CPython source comparison, update_lib synchronization and validation preparation. Model version is unavailable.

Track permanent version-tag ineligibility after custom MRO publication and
bases changes, propagate it through version assignment, and retain existing
MCACHE invalidation. Restore canonical metaclass doctests through update_lib.

Assisted-by: OpenAI Codex: model version unavailable
@coderabbitai

coderabbitai Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Caution

Review failed

The pull request is closed.

Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

Copy link
Copy Markdown
Contributor

📦 Library Dependencies

The following Lib/ modules were modified. Here are their dependencies:

[x] test: cpython/Lib/test/test_metaclass.py

dependencies:

dependent tests: (no tests depend on metaclass)

Legend:

  • [+] path exists in CPython
  • [x] up-to-date, [ ] outdated

@codspeed

codspeed Bot commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 62 untouched benchmarks
⏩ 4 skipped benchmarks1


Comparing youknowdot:py315-custom-mro-cache-20261030 (a4a394e) with main (d24ca41)

Open in CodSpeed

Footnotes

  1. 4 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

Comment thread crates/vm/src/builtins/type.rs Outdated
pub tp_version_tag: AtomicU32,
/// Permanent cache exclusion after an unsafe MRO, like CPython's
/// _Py_ATTR_CACHE_UNUSED state. A zero version alone is only temporary.
pub(crate) version_tag_disabled: AtomicBool,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this encoded in same way in CPython too? isnt' this integrated into tp_version_tag?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CPython keeps these states separate: type_mro_modified() clears tp_version_tag to 0 and sets tp_versions_used = _Py_ATTR_CACHE_UNUSED. assign_version_tag() rejects that sentinel through its version-budget check.

The bool here models only permanent custom-MRO cache exclusion, not CPython’s per-type version budget. A zero tag remains temporary invalidation; the existing cache retirement/invalidation semantics are unchanged.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

align field names to cpython in that case

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in ddec470: the field is now tp_versions_used, using AtomicU16 and CPython's _Py_ATTR_CACHE_UNUSED value (30000) as ATTR_CACHE_UNUSED. The existing zero/sentinel state transitions and atomic ordering are unchanged. Per-type version counting/budgeting is still not implemented, and the field comment states that explicitly.

Scoped Cargo check and strict Clippy passed; the existing canonical metaclass coverage will run again in CI.

Use tp_versions_used and the CPython ATTR_CACHE_UNUSED sentinel for permanent cache exclusion. Keep the existing zero/sentinel state behavior and atomic ordering; per-type version budgeting remains unimplemented.

Assisted-by: OpenAI Codex: model version unavailable
@youknowone
youknowone marked this pull request as ready for review October 30, 2026 23:28
@youknowone
youknowone merged commit 84b95a8 into RustPython:main Oct 30, 2026
29 checks passed
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-30T23:28:08.446957Z
We couldn’t complete this review. Comment @codex review to try again.
ddec470 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants