Skip to content

Add global type mutex for thread-safe type operations#7416

Draft
youknowone wants to merge 1 commit intoRustPython:mainfrom
youknowone:typelock
Draft

Add global type mutex for thread-safe type operations#7416
youknowone wants to merge 1 commit intoRustPython:mainfrom
youknowone:typelock

Conversation

@youknowone
Copy link
Member

Introduce vm.state.type_mutex to serialize type mutation and version-tag assignment, matching CPython's free-threading model.

  • Add with_type_lock() helper and split modified/assign_version_tag into inner (lock-free) and public (lock-acquiring) variants
  • Add version_for_specialization() and lookup_ref_and_version_interned() for atomic type-cache lookups under the lock
  • Wrap all type-mutating paths (bases, annotations, module, type_params, doc, SetAttr) with the type lock
  • Drop old values outside the lock to prevent deadlock from weakref callbacks that may re-enter specialization
  • Fix _ctypes set_attr calls to route through proper SetAttr path
  • Reinitialize type_mutex after fork

Introduce vm.state.type_mutex to serialize type mutation and
version-tag assignment, matching CPython's free-threading model.

- Add with_type_lock() helper and split modified/assign_version_tag
  into inner (lock-free) and public (lock-acquiring) variants
- Add version_for_specialization() and lookup_ref_and_version_interned()
  for atomic type-cache lookups under the lock
- Wrap all type-mutating paths (__bases__, __annotations__, __module__,
  __type_params__, __doc__, SetAttr) with the type lock
- Drop old values outside the lock to prevent deadlock from weakref
  callbacks that may re-enter specialization
- Fix _ctypes set_attr calls to route through proper SetAttr path
- Reinitialize type_mutex after fork
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 13, 2026

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: 078fe0b7-0334-4da7-b861-a7884381757a

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
📝 Coding Plan
  • Generate coding plan for human review comments

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 and usage tips.

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.

1 participant