René's URL Explorer Experiment


Title: gh-123471: Make itertools.product and itertools.combinations thread-safe by eendebakpt · Pull Request #132814 · python/cpython · GitHub

Open Graph Title: gh-123471: Make itertools.product and itertools.combinations thread-safe by eendebakpt · Pull Request #132814 · python/cpython

X Title: gh-123471: Make itertools.product and itertools.combinations thread-safe by eendebakpt · Pull Request #132814 · python/cpython

Description: We make concurrent iteration over itertools.combinations and itertools.product thread safe in the free-threading build. The original combinations_next is renamed to combinations_next_with_lock_held and combinations_next is now calling combinations_next_with_lock_held with a lock (similar for itertools.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 and op->result). Issues that can occur without the locks: On initialization of the results structure the op->result can be overwritten, resulting in memory leaks cpython/Modules/itertoolsmodule.c Line 2342 in a4ea80d PyTuple_SET_ITEM(result, i, elem); The increment of op->indices[i] at https://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 segfault The refcount check for re-use of the result tuple https://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 atomic itertoolsmodule.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 combinations and product are in the test, but cwr and permutations 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. For product we could make the rollover check safe by changing indices[i] == PyTuple_GET_SIZE(pool) into indices[i] >= PyTuple_GET_SIZE(pool) and use atomic operations in all operations dealing with op->indices or op->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. Issue: gh-123471

Open Graph Description: We make concurrent iteration over itertools.combinations and itertools.product thread safe in the free-threading build. The original combinations_next is renamed to combinations_next_with_lock_hel...

X Description: We make concurrent iteration over itertools.combinations and itertools.product thread safe in the free-threading build. The original combinations_next is renamed to combinations_next_with_lock_hel...

Opengraph URL: https://github.com/python/cpython/pull/132814

X: @github

direct link

Domain: github.com

route-pattern/:user_id/:repository/pull/:id/files(.:format)
route-controllerpull_requests
route-actionfiles
fetch-noncev2:98141f5a-b726-785c-f8e4-c391c8a6be1e
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-idA372:A6212:1EBF799:2C745D3:6A58DD29
html-safe-nonce63190ef5e3c829f5b3031a0615724fc4938a09310f63664ff84c4af503db628d
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMzcyOkE2MjEyOjFFQkY3OTk6MkM3NDVEMzo2QTU4REQyOSIsInZpc2l0b3JfaWQiOiI3Nzg3MTQzMzYyOTI3NzEzNTc3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmac6ec33cfe93641a3857707a2e325cd56aee1b0a11acefc471dfdbd5e9a36f75e3
hovercard-subject-tagpull_request:2474538597
github-keyboard-shortcutsrepository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///pull_requests/show/files
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/python/cpython/pull/132814/files
twitter:imagehttps://avatars.githubusercontent.com/u/883786?s=400&v=4
twitter:cardsummary_large_image
og:imagehttps://avatars.githubusercontent.com/u/883786?s=400&v=4
og:image:altWe make concurrent iteration over itertools.combinations and itertools.product thread safe in the free-threading build. The original combinations_next is renamed to combinations_next_with_lock_hel...
og:site_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
None5f2a0c7865178af3d91dd9f13b0cdfc3c73a2529c873d2780bb4c01271a57ec6
turbo-cache-controlno-preview
diff-viewunified
go-importgithub.com/python/cpython git https://github.com/python/cpython.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id81598961
octolytics-dimension-repository_nwopython/cpython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id81598961
octolytics-dimension-repository_network_root_nwopython/cpython
turbo-body-classeslogged-out env-production page-responsive full-width
disable-turbotrue
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release8aae7b8d6caacacf5c66eaeb2702d8dc88d85b4a
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python/cpython/pull/132814/files#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fpull%2F132814%2Ffiles
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/open-source/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/open-source/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fpull%2F132814%2Ffiles
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fpull_requests%2Fshow%2Ffiles&source=header-repo&source_repo=python%2Fcpython
Reloadhttps://github.com/python/cpython/pull/132814/files
Reloadhttps://github.com/python/cpython/pull/132814/files
Reloadhttps://github.com/python/cpython/pull/132814/files
Please reload this pagehttps://github.com/python/cpython/pull/132814/files
python https://github.com/python
cpythonhttps://github.com/python/cpython
Please reload this pagehttps://github.com/python/cpython/pull/132814/files
Notifications https://github.com/login?return_to=%2Fpython%2Fcpython
Fork 35k https://github.com/login?return_to=%2Fpython%2Fcpython
Star 73.8k https://github.com/login?return_to=%2Fpython%2Fcpython
Code https://github.com/python/cpython
Issues 5k+ https://github.com/python/cpython/issues
Pull requests 2.3k https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects https://github.com/python/cpython/projects
Security and quality 0 https://github.com/python/cpython/security
Insights https://github.com/python/cpython/pulse
Code https://github.com/python/cpython
Issues https://github.com/python/cpython/issues
Pull requests https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects https://github.com/python/cpython/projects
Security and quality https://github.com/python/cpython/security
Insights https://github.com/python/cpython/pulse
Sign up for GitHub https://github.com/signup?return_to=%2Fpython%2Fcpython%2Fissues%2Fnew%2Fchoose
terms of servicehttps://docs.github.com/terms
privacy statementhttps://docs.github.com/privacy
Sign inhttps://github.com/login?return_to=%2Fpython%2Fcpython%2Fissues%2Fnew%2Fchoose
kumaraditya303https://github.com/kumaraditya303
python:mainhttps://github.com/python/cpython/tree/main
eendebakpt:itertools_combinations_fthttps://github.com/eendebakpt/cpython/tree/itertools_combinations_ft
Conversation 2 https://github.com/python/cpython/pull/132814
Commits 7 https://github.com/python/cpython/pull/132814/commits
Checks 38 https://github.com/python/cpython/pull/132814/checks
Files changed https://github.com/python/cpython/pull/132814/files
Please reload this pagehttps://github.com/python/cpython/pull/132814/files
gh-123471: Make itertools.product and itertools.combinations thread-safe https://github.com/python/cpython/pull/132814/files#top
Show all changes 7 commits https://github.com/python/cpython/pull/132814/files
f20855c Make itertools.product and itertools.combinations thread-safe eendebakpt Apr 22, 2025 https://github.com/python/cpython/pull/132814/commits/f20855cb4ad9968b21ff31d45e7a66f2ee7b0cfa
6f14bd0 📜🤖 Added by blurb_it. blurb-it[bot] Apr 22, 2025 https://github.com/python/cpython/pull/132814/commits/6f14bd0c15cee9eb5a1a0a8763dfdf27ad01cc37
a204f6d whitespace eendebakpt Apr 22, 2025 https://github.com/python/cpython/pull/132814/commits/a204f6db8d4b90f6e131396a0bdc94659d69b025
cdfe7c2 Merge branch 'itertools_combinations_ft' of github.com:eendebakpt/cpy… eendebakpt Apr 22, 2025 https://github.com/python/cpython/pull/132814/commits/cdfe7c2eb0990ed561db6de729d4917aac8ba244
24c8f68 Merge branch 'main' into itertools_combinations_ft eendebakpt Jun 18, 2025 https://github.com/python/cpython/pull/132814/commits/24c8f68252baa97c28adbd5ce86936bebee6e2ce
7ab53c3 Merge branch 'main' into itertools_combinations_ft kumaraditya303 Jun 25, 2025 https://github.com/python/cpython/pull/132814/commits/7ab53c30afe7837bef085df6fbb4c3c5cff32b76
ba9c4b7 Apply suggestions from code review kumaraditya303 Jun 30, 2025 https://github.com/python/cpython/pull/132814/commits/ba9c4b7fdc9f86b77c0990f514d6206735ce8578
Clear filters https://github.com/python/cpython/pull/132814/files
Please reload this pagehttps://github.com/python/cpython/pull/132814/files
Please reload this pagehttps://github.com/python/cpython/pull/132814/files
test_itertools_combinatoric.py https://github.com/python/cpython/pull/132814/files#diff-2bb5d94fc763856ec0cd3b17f75aa857c797e81f36e538494e77974e215d55f1
2025-04-22-21-00-23.gh-issue-123471.asOLA2.rst https://github.com/python/cpython/pull/132814/files#diff-89f72bc256fb559433d7d1124b783aa69e68e29c4aa5e335a24394f164447581
itertoolsmodule.c https://github.com/python/cpython/pull/132814/files#diff-dc27cbdf2b9f6e5236cade13cf301d352a7abd2a8b1988f2517d6f5b7a30c201
Lib/test/test_free_threading/test_itertools_combinatoric.pyhttps://github.com/python/cpython/pull/132814/files#diff-2bb5d94fc763856ec0cd3b17f75aa857c797e81f36e538494e77974e215d55f1
View file https://github.com/eendebakpt/cpython/blob/ba9c4b7fdc9f86b77c0990f514d6206735ce8578/Lib/test/test_free_threading/test_itertools_combinatoric.py
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/132814/{{ revealButtonHref }}
Misc/NEWS.d/next/Library/2025-04-22-21-00-23.gh-issue-123471.asOLA2.rsthttps://github.com/python/cpython/pull/132814/files#diff-89f72bc256fb559433d7d1124b783aa69e68e29c4aa5e335a24394f164447581
View file https://github.com/eendebakpt/cpython/blob/ba9c4b7fdc9f86b77c0990f514d6206735ce8578/Misc/NEWS.d/next/Library/2025-04-22-21-00-23.gh-issue-123471.asOLA2.rst
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/132814/{{ revealButtonHref }}
https://github.com/python/cpython/blob/main/.github/CODEOWNERS#L522
Modules/itertoolsmodule.chttps://github.com/python/cpython/pull/132814/files#diff-dc27cbdf2b9f6e5236cade13cf301d352a7abd2a8b1988f2517d6f5b7a30c201
View file https://github.com/eendebakpt/cpython/blob/ba9c4b7fdc9f86b77c0990f514d6206735ce8578/Modules/itertoolsmodule.c
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/132814/{{ revealButtonHref }}
https://github.com/python/cpython/pull/132814/files#diff-dc27cbdf2b9f6e5236cade13cf301d352a7abd2a8b1988f2517d6f5b7a30c201
https://github.com/python/cpython/pull/132814/files#diff-dc27cbdf2b9f6e5236cade13cf301d352a7abd2a8b1988f2517d6f5b7a30c201
https://github.com/python/cpython/pull/132814/files#diff-dc27cbdf2b9f6e5236cade13cf301d352a7abd2a8b1988f2517d6f5b7a30c201
https://github.com/python/cpython/pull/132814/files#diff-dc27cbdf2b9f6e5236cade13cf301d352a7abd2a8b1988f2517d6f5b7a30c201
https://github.com/python/cpython/pull/132814/files#diff-dc27cbdf2b9f6e5236cade13cf301d352a7abd2a8b1988f2517d6f5b7a30c201
https://github.com/python/cpython/pull/132814/files#diff-dc27cbdf2b9f6e5236cade13cf301d352a7abd2a8b1988f2517d6f5b7a30c201
https://github.com/python/cpython/pull/132814/files#diff-dc27cbdf2b9f6e5236cade13cf301d352a7abd2a8b1988f2517d6f5b7a30c201
https://github.com/python/cpython/pull/132814/files#diff-dc27cbdf2b9f6e5236cade13cf301d352a7abd2a8b1988f2517d6f5b7a30c201
Please reload this pagehttps://github.com/python/cpython/pull/132814/files
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.