gh-152741: Fix data race on PyThreadState.thread_id during thread bind#152755
Open
tonghuaroot wants to merge 1 commit into
Open
gh-152741: Fix data race on PyThreadState.thread_id during thread bind#152755tonghuaroot wants to merge 1 commit into
PyThreadState.thread_id during thread bind#152755tonghuaroot wants to merge 1 commit into
Conversation
…ad bind bind_tstate() set thread_id/native_thread_id without synchronization while the thread state was already published to interp->threads.head but not yet stop-the-world-stoppable, racing the stop-the-world readers sys._current_exceptions() and sys._current_frames(). Publish them with a relaxed atomic store and read them with a relaxed atomic load in the readers that walk the thread list, matching the treatment of tstate->state.
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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
bind_tstate()setststate->thread_id(andnative_thread_id) withoutsynchronization while the thread state is already published to
interp->threads.headand the thread is not yet stop-the-world-stoppable. Thestop-the-world readers
_PyThread_CurrentExceptions(sys._current_exceptions())and
_PyThread_CurrentFrames(sys._current_frames()) readt->thread_id, so thebind store races the reader (reproduced under a free-threaded ThreadSanitizer
build; see gh-152741).
Publish
thread_id/native_thread_idwith a relaxed atomic store inbind_tstate()and read them with a relaxed atomic load in the readers that walkthe thread list —
_PyThread_CurrentExceptions,_PyThread_CurrentFrames, andPyThreadState_SetAsyncExc— matching the relaxed-atomic treatment already usedfor
tstate->state.thread_idis a standalone scalar written once during attach(never reset on unbind) and read only as an identity value, so relaxed ordering is
sufficient. In the default (GIL) build the
FT_ATOMIC_*wrappers expand to plainloads/stores — no overhead.
Verified TSan-clean: 0 races across 32 short reproducer runs + 4 full 30s windows
(vs the race firing on every unpatched run); functional and
test_threadingsmokes pass under the FT+TSAN build.