Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Conversation

@eendebakpt
Copy link
Contributor

@eendebakpteendebakpt commentedApr 22, 2025
edited by bedevere-appbot
Loading

We make concurrent iteration overitertools.combinations anditertools.product thread safe in the free-threading build.
The originalcombinations_next is renamed tocombinations_next_with_lock_held andcombinations_next is now callingcombinations_next_with_lock_held with 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->indices andop->result).

Issues that can occur without the locks:

  • On initialization of the results structure theop->result can be overwritten, resulting in memory leaks

PyTuple_SET_ITEM(result,i,elem);

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. Currentlycombinations andproduct are in the test, butcwr andpermutations have 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. Forproduct we 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->indices orop->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.

@rhettingerrhettinger removed their request for reviewApril 22, 2025 22:22
@kumaraditya303kumaraditya303enabled auto-merge (squash)June 30, 2025 11:05
@kumaraditya303kumaraditya303 merged commit847d1c2 intopython:mainJun 30, 2025
40 checks passed
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

Reviewers

@kumaraditya303kumaraditya303kumaraditya303 approved these changes

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

@eendebakpt@kumaraditya303

[8]ページ先頭

©2009-2026 Movatter.jp