Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork34k
gh-123471: Make itertools.product and itertools.combinations thread-safe#132814
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
+74 −2
Conversation
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
…thon into itertools_combinations_ft
kumaraditya303 approved these changesJun 25, 2025
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
kumaraditya303 approved these changesJun 30, 2025
847d1c2 intopython:main 40 checks passed
Uh oh!
There was an error while loading.Please reload this page.
AndPuQing pushed a commit to AndPuQing/cpython that referenced this pull requestJul 11, 2025
…read-safe (python#132814)Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Pranjal095 pushed a commit to Pranjal095/cpython that referenced this pull requestJul 12, 2025
…read-safe (python#132814)Co-authored-by: Kumar Aditya <kumaraditya@python.org>
picnixz pushed a commit to picnixz/cpython that referenced this pull requestJul 13, 2025
…read-safe (python#132814)Co-authored-by: Kumar Aditya <kumaraditya@python.org>
taegyunkim pushed a commit to taegyunkim/cpython that referenced this pull requestAug 4, 2025
…read-safe (python#132814)Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Agent-Hellboy pushed a commit to Agent-Hellboy/cpython that referenced this pull requestAug 19, 2025
…read-safe (python#132814)Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Sign up for freeto 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.
Uh oh!
There was an error while loading.Please reload this page.
We make concurrent iteration over
itertools.combinationsanditertools.productthread safe in the free-threading build.The original
combinations_nextis renamed tocombinations_next_with_lock_heldandcombinations_nextis now callingcombinations_next_with_lock_heldwith a lock (similar foritertools.product)We use a lock because it is easy to implement and avoids quite a bit of complexity (we have two pieces of mutable state to deal with:
op->indicesandop->result).Issues that can occur without the locks:
op->resultcan be overwritten, resulting in memory leakscpython/Modules/itertoolsmodule.c
Line 2342 ina4ea80d
The increment of
op->indices[i]athttps://github.com/python/cpython/blob/main/Modules/itertoolsmodule.c#L2379 is not safe. It can go out-of-bounds since the check is done earlier. This can lead to a segfaultThe refcount check for re-use of the result tuplehttps://github.com/python/cpython/blob/main/Modules/itertoolsmodule.c#L2346 is not valid in the FT build. Not sure whether this leads to crashes, but it will result in memory leaks.
Updating the indices is not atomicitertoolsmodule.c#L2380-L2381. Non-atomic updates can lead to out-of-bounds issues.
The tests in this PR trigger some of these issues, although some are not visible (e.g. the memory leak), and it typically requires more iterations to result in a segfault. On my system > 2000 iterations gives a very high probability of triggering a segfault. The number of iterations is set much lower to keep the duration of the test < 0.1 second.
Performance with the locks is about 5% less for a single-thread (see the corresponding issue).
I refactored the tests to avoid duplicated code. Currently
combinationsandproductare in the test, butcwrandpermutationshave the same style and could be added as well (in a followup PR).Could we do this without a full lock? It depends a bit on the iterator. For
productwe could make the rollover check safe by changingindices[i] == PyTuple_GET_SIZE(pool)intoindices[i] >= PyTuple_GET_SIZE(pool)and use atomic operations in all operations dealing withop->indicesorop->result. That would still leave memory leaks, but these are not crashes. And determining whether this actually safe (not crashing) requires some very careful reviews.