Skip to content

gh-130091: Reorder _PyThreadState_Attach to avoid data race#130092

Merged
colesbury merged 1 commit into
python:mainfrom
colesbury:gh-130091-tstate-attach
Feb 27, 2025
Merged

gh-130091: Reorder _PyThreadState_Attach to avoid data race#130092
colesbury merged 1 commit into
python:mainfrom
colesbury:gh-130091-tstate-attach

Conversation

@colesbury
Copy link
Copy Markdown
Contributor

@colesbury colesbury commented Feb 13, 2025

This moves tstate_activate() down to avoid a data race in the free threading build on the _PyRuntime's thread-local autoTSSkey. This key is deleted during runtime finalization, which may happen concurrently with a call to _PyThreadState_Attach.

The earlier tstate_try/wait_attach ensures that the thread is blocked before it attempts to access the deleted autoTSSkey.

This fixes a TSAN reported data race in
test_threading.test_import_from_another_thread.

This moves `tstate_activate()` down to avoid a data race in the free
threading build on the `_PyRuntime`'s thread-local `autoTSSkey`. This
key is deleted during runtime finalization, which may happen
concurrently with a call to `_PyThreadState_Attach`.

The earlier `tstate_try/wait_attach` ensures that the thread is blocked
before it attempts to access the deleted `autoTSSkey`.

This fixes a TSAN reported data race in
`test_threading.test_import_from_another_thread`.
@colesbury colesbury merged commit 7aeaa5a into python:main Feb 27, 2025
@colesbury colesbury deleted the gh-130091-tstate-attach branch February 27, 2025 18:57
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.

2 participants