FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-151377: Fix races updating type slots and subclasses in free-threa… by lpyu001 · Pull Request #151394 · python/cpython · GitHub

Repository navigation

gh-151377: Fix races updating type slots and subclasses in free-threa… - #151394

Closed
lpyu001 wants to merge 3 commits into
python:mainfrom
lpyu001:threading
Closed

lpyu001 wants to merge 3 commits into
python:mainfrom
lpyu001:threading

Conversation

lpyu001 commented Jun 12, 2026 •
edited
Loading

Copy link
Copy Markdown
Contributor

In free-threaded builds, a newly created heap type can become visible through its base type’s subclasses dictionary after PyType_Ready(). Updating its slot dispatchers after that point must not write slot pointers unsafely while other threads may observe or update the type hierarchy.
This change makes fixup_slot_dispatchers() hold TYPE_LOCK in free-threaded builds, queue the required slot updates, and apply them with the world stopped. It also protects removal from base tp_subclasses during type deallocation with TYPE_LOCK.
Additionally, the debug-only _Py_TYPE_REVEALED_FLAG is read and written from different threads by type-safety assertions, so its accesses are changed to atomic operations to avoid TSAN reports on the debug marker itself.

lpyu001 closed this Jun 15, 2026
lpyu001 reopened this Jun 15, 2026

lpyu001 commented Jun 15, 2026

Copy link
Copy Markdown
Contributor Author

Tested on Ubuntu 24.04,did not observe TSAN warnings.

nascheme commented Jul 3, 2026

Copy link
Copy Markdown
Member

Using STW (stop-the-world) is a big hammer and could have significant performance issues. If at all possible, we should avoid it when creating types. I think a potential alternative solution is to do the slot update before we publish the new type. See this branch for a prototype implementation: https://github.com/nascheme/cpython/tree/gh-151377-type-data-race

Note that re-org of the type init steps is tricky and we need to be careful this doesn't cause some kind of bug. I would prefer this approach if we can make it work. STW is likely too expensive to do on this code path.

lpyu001 deleted the threading branch September 22, 2026 08:07
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants


Back | FazBrowse Home | New Git URL