Skip to content

Defer custom-MRO subclass publication until initialization succeeds - #9527

Merged
youknowone merged 2 commits into
RustPython:mainfrom
youknowdot:fix-failed-mro-subclass-9526-20261102
Nov 2, 2026
Merged

youknowone merged 2 commits into
RustPython:mainfrom
youknowdot:fix-failed-mro-subclass-9526-20261102

Conversation

@youknowdot

Copy link
Copy Markdown
Contributor

Summary

Fixes #9526.

Defer subclass-list publication for types using a custom metaclass until MRO and slot initialization succeed. A user can retain a type whose mro() raised; publishing that still-incomplete type early lets the outer type's slot initialization reach its empty MRO.

  • Keep the non-deferred construction path unchanged.
  • Publish the completed custom-MRO type before __set_name__ / __init_subclass__ callbacks, under the existing type lock.
  • Use the current bases after possible __bases__ reentrancy and deduplicate the cached callback-free weak reference. This avoids a second subclass entry when a reentrant bases setter has already registered the type, without upgrading unrelated weak references while holding subclass locks.
  • Do not hide the problem by skipping empty MROs in slot lookup.

This follows CPython 3.15.0 type_ready_publish, which completes MRO/slot setup before publishing subclass links. CPython's add_subclass is idempotent by type identity. This does not claim that every externally retained type whose creation fails is unpublished: a reentrant __bases__ operation may itself complete and publish an MRO before a later outer failure.

Validation and limits

  • Independent source review checked publication order, current-base selection, weakref identity reuse, duplicate prevention and lock/reentrancy boundaries.
  • Normal commit hooks and git diff --check passed.
  • Local native compilation and interpreter tests were not run: locked offline dependency resolution is blocked by the missing parking_lot_core git checkout. This Draft relies on hosted CI for compilation and existing canonical tests.
  • Existing test_descr.MroTest incomplete-type and reentrant-bases tests are unchanged. In particular, canonical test_incomplete_extend does not keep the failed subclass alive, so its success alone does not establish the specific retained-child regression is fixed at runtime.
  • A bounded external fixed-only observer is prepared but unexecuted. The known-panic program was not run. No new snippet test, modified canonical assertion, or empty-MRO workaround is included.

Keep a type whose custom MRO failed out of its bases subclass lists until its MRO and slots are initialized. Register current bases under the type lock and reuse the cached callback-free weak reference to avoid duplicate registration after reentrant __bases__ assignment.

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

coderabbitai Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Caution

Review failed

The pull request is closed.

  • 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

Address clippy::use_self without changing initialization or locking behavior.

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

codspeed Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 62 untouched benchmarks
⏩ 4 skipped benchmarks1


Comparing youknowdot:fix-failed-mro-subclass-9526-20261102 (0a02c6c) with main (ef023bc)

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. ↩

@youknowone
youknowone marked this pull request as ready for review November 2, 2026 15:19
@youknowone
youknowone merged commit 504a08b into RustPython:main Nov 2, 2026
21 checks passed
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 2, 2026 •

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 ✅ Completed 2026-11-02T15:31:23.777627Z 0a02c6c 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 chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0a02c6cbb6

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +2435 to +2436
for base in typ.bases.read().as_slice() {
let mut subclasses = base.subclasses.write();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Refresh collection flags after custom MRO callbacks

If a custom mro() calls collections.abc.Sequence.register(Base) (or Mapping.register(Base)) while constructing Child(Base), deferring this link means _abc_register()'s set_abc_collection_flags_recursive() no longer reaches Child. new_heap_inner() copied the collection flags before the callback, and neither init_slots() nor update_subclass_flags_recursive() refreshes PyTypeFlags::COLLECTION, so the completed child keeps stale flags and sequence/mapping patterns silently fail to match its instances. The old early link propagated the registration correctly. Refresh the inherited collection flags after the callback and before publishing the type.

Useful? React with 👍 / 👎.

let mut subclasses = base.subclasses.write();
// A reentrant __bases__ assignment may have registered
// this type already. Callback-free weakrefs are cached.
if !subclasses.iter().any(|existing| existing.is(&weak)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Avoid scanning the full subclass history on every new type

When many dynamic classes are created with a custom metaclass—for example repeated typing.TypedDict(...) calls, which use dict as a physical base—this check scans the base's entire subclass history for each new class. GC leaves dead weakrefs in that vector until a pruning operation such as __subclasses__(), so collecting the classes does not prevent N creations from doing O(N²) comparisons where the previous publication path did O(N) appends. These scans also hold the interpreter-wide type lock. Preserve duplicate prevention for reentrant registration without scanning all prior subclasses on the normal construction path.

Useful? React with 👍 / 👎.

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.

Panic in lookup_slot_in_mro when a class whose mro() failed is still alive

3 participants