Repository navigation
[mypyc] Stop list loops on free-threaded builds if the list shrinks concurrently - #22565
Open
Jahongir-Qurbonov wants to merge 2 commits into
Open
Jahongir-Qurbonov wants to merge 2 commits into
Jahongir-Qurbonov wants to merge 2 commits into
Conversation
…oncurrently On free-threaded builds, another thread could shrink a list between the length check and the item read in a for loop, and the read then raised IndexError. Stop the loop instead, like the list iterator.
On free-threaded builds, the list item is read as part of the loop condition, so skipping the first condition check for a sequence with a known non-zero length left the item uninitialized in the first iteration.
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.
On free-threaded builds, a
forloop over a list checks the index against the current length of the list and then reads the item withCPyList_GetItemInt64. If another thread shrinks the list between these two steps, the read raisesIndexError. The list iterator in CPython stops the iteration instead, so a loop like this could fail only when compiled.Read the item using a new
CPyList_GetItemOrNullhelper on free-threaded builds, and stop the loop if the index is out of range. This also applies toreversed(),enumerate(),zip()and comprehensions over lists, since they all useForSequence. Builds with the GIL are not affected, since nothing can change the list between the length check and the read there.The new test in
run-threading.testiterates over a list in a few ways while other threads add and remove items. It's a race, so it doesn't fail every time without the fix (it failed in 2 of 5 runs on a free-threaded 3.14 build on a busy machine), but it can't fail with the fix. I also ran a separate script that iterates over a list while another thread pops items: without the fix, hundreds of loops raisedIndexError, and none did with the fix.