Skip to content

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

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

gh-151377: Fix races updating type slots and subclasses in free-threa…#151394
lpyu001 wants to merge 3 commits into
python:mainfrom
lpyu001:threading

Conversation

@lpyu001

@lpyu001 lpyu001 commented Jun 12, 2026

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 lpyu001 closed this Jun 15, 2026
@lpyu001 lpyu001 reopened this Jun 15, 2026
@lpyu001

lpyu001 commented Jun 15, 2026

Copy link
Copy Markdown
Contributor Author

Tested on Ubuntu 24.04,did not observe TSAN warnings.
image

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.

1 participant