René's URL Explorer Experiment


Title: gh-114271: Fix race in `Thread.join()` by mpage · Pull Request #114839 · python/cpython · GitHub

Open Graph Title: gh-114271: Fix race in `Thread.join()` by mpage · Pull Request #114839 · python/cpython

X Title: gh-114271: Fix race in `Thread.join()` by mpage · Pull Request #114839 · python/cpython

Description: This PR makes thread joining thread-safe in free-threaded builds and fixes gh-116372, which affects both default and free-threaded builds. The combination of _tstate_lock and _ThreadHandle is replaced by _ThreadHandle and the supporting infrastructure for _tstate_lock is removed. Joining a thread now joins its _ThreadHandle. The _ThreadHandles for all threads that we want to join on shutdown (non-daemon threads that were created by the threading module) are tracked in a linked-list attached to the _thread module. At shutdown we iterate over this list and join the handles. There is one wrinkle with this approach: the _MainThread instance. The _MainThread instance is joinable. Previously, it represented whatever thread imported the threading module. Typically this is the main thread of the main interpreter, but in some rare circumstances (embedding scenarios?) it might not be. The previous approach allowed the MainThread instance to be joined even if it wasn't the main thread of the main interpreter, but this isn't possible with the current approach. To work around this we change the _MainThread to always represent the main thread of the runtime, also fixing gh-83223. Issue: gh-114271

Open Graph Description: This PR makes thread joining thread-safe in free-threaded builds and fixes gh-116372, which affects both default and free-threaded builds. The combination of _tstate_lock and _ThreadHandle is repla...

X Description: This PR makes thread joining thread-safe in free-threaded builds and fixes gh-116372, which affects both default and free-threaded builds. The combination of _tstate_lock and _ThreadHandle is repla...

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

X: @github

direct link

Domain: github.com

route-pattern/:user_id/:repository/pull/:id/commits/:range(.:format)
route-controllerpull_requests
route-actioncommits
fetch-noncev2:428c4a05-98ec-5279-a8d6-810ce46c1657
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-idCED8:2178BE:48DB61D:641F199:6995EB02
html-safe-noncefc5f2040660b700c61e4236b194ce33614dba2efadaee20f530ca465efc316e6
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRUQ4OjIxNzhCRTo0OERCNjFEOjY0MUYxOTk6Njk5NUVCMDIiLCJ2aXNpdG9yX2lkIjoiNDA4NzIzNTIwNDQzMTA4MDE5NCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac9a997066639990148f300fa0e9c145fed118662d735cf1dd3117fbf46dac8c51
hovercard-subject-tagpull_request:1705241216
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/commits
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/python/cpython/pull/114839/commits/c86d349b4e935f3848f7d4dc42912effbdd9c963
twitter:imagehttps://avatars.githubusercontent.com/u/577841?s=400&v=4
twitter:cardsummary_large_image
og:imagehttps://avatars.githubusercontent.com/u/577841?s=400&v=4
og:image:altThis PR makes thread joining thread-safe in free-threaded builds and fixes gh-116372, which affects both default and free-threaded builds. The combination of _tstate_lock and _ThreadHandle is repla...
og:site_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
None1161b1e91d57e776171599ebe7c3b8c4690792d1e87e83b5c77a9b1aeb391094
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
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
releasea8c6902042885fe21ac3491e553d741a50765a66
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python/cpython/pull/114839/commits/c86d349b4e935f3848f7d4dc42912effbdd9c963#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fpull%2F114839%2Fcommits%2Fc86d349b4e935f3848f7d4dc42912effbdd9c963
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub SparkBuild and deploy intelligent appshttps://github.com/features/spark
GitHub ModelsManage and compare promptshttps://github.com/features/models
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
GitHub SponsorsFund open source developershttps://github.com/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/accelerator
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/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%2F114839%2Fcommits%2Fc86d349b4e935f3848f7d4dc42912effbdd9c963
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%2Fcommits&source=header-repo&source_repo=python%2Fcpython
Reloadhttps://github.com/python/cpython/pull/114839/commits/c86d349b4e935f3848f7d4dc42912effbdd9c963
Reloadhttps://github.com/python/cpython/pull/114839/commits/c86d349b4e935f3848f7d4dc42912effbdd9c963
Reloadhttps://github.com/python/cpython/pull/114839/commits/c86d349b4e935f3848f7d4dc42912effbdd9c963
python https://github.com/python
cpythonhttps://github.com/python/cpython
Please reload this pagehttps://github.com/python/cpython/pull/114839/commits/c86d349b4e935f3848f7d4dc42912effbdd9c963
Notifications https://github.com/login?return_to=%2Fpython%2Fcpython
Fork 34.1k https://github.com/login?return_to=%2Fpython%2Fcpython
Star 71.6k 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.1k https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects 31 https://github.com/python/cpython/projects
Security 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 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
pitrouhttps://github.com/pitrou
python:mainhttps://github.com/python/cpython/tree/main
mpage:gh-114271-remove-tstate_lockhttps://github.com/mpage/cpython/tree/gh-114271-remove-tstate_lock
Conversation 60 https://github.com/python/cpython/pull/114839
Commits 34 https://github.com/python/cpython/pull/114839/commits
Checks 0 https://github.com/python/cpython/pull/114839/checks
Files changed https://github.com/python/cpython/pull/114839/files
Please reload this pagehttps://github.com/python/cpython/pull/114839/commits/c86d349b4e935f3848f7d4dc42912effbdd9c963
gh-114271: Fix race in Thread.join() https://github.com/python/cpython/pull/114839/commits/c86d349b4e935f3848f7d4dc42912effbdd9c963#top
Show all changes 34 commits https://github.com/python/cpython/pull/114839/files
fb13cf6 Fix race in `Thread.join()` mpage Jan 26, 2024 https://github.com/python/cpython/pull/114839/commits/fb13cf6dfa4b5f0420f283823581bcc6e2c9839d
b093e5d 📜🤖 Added by blurb_it. blurb-it[bot] Feb 1, 2024 https://github.com/python/cpython/pull/114839/commits/b093e5db2c9e4abf8f550f1d7b2af636d8f87baf
1039208 Fix NEWS entry mpage Feb 1, 2024 https://github.com/python/cpython/pull/114839/commits/1039208652c0254bccd70e26bd43327e3508ff5e
196081d Work around c-analyzer limitation mpage Feb 5, 2024 https://github.com/python/cpython/pull/114839/commits/196081d6346b994780667812aebea898c142c86d
19d7af1 Merge branch 'main' into gh-114271-remove-tstate_lock mpage Feb 5, 2024 https://github.com/python/cpython/pull/114839/commits/19d7af12f14e615c4cb3f107c49084c8bbfcb314
c86d349 Merge branch 'main' into gh-114271-remove-tstate_lock mpage Feb 7, 2024 https://github.com/python/cpython/pull/114839/commits/c86d349b4e935f3848f7d4dc42912effbdd9c963
40e4b36 Merge branch 'main' into gh-114271-remove-tstate_lock mpage Feb 14, 2024 https://github.com/python/cpython/pull/114839/commits/40e4b36f5d93a4c277679bcc30604fc07d29948d
4ed1083 Merge branch 'main' into gh-114271-remove-tstate_lock mpage Feb 14, 2024 https://github.com/python/cpython/pull/114839/commits/4ed10832b090a902a953e91ad78a6917fd9c7d7d
34b6065 Merge branch 'main' into gh-114271-remove-tstate_lock mpage Mar 1, 2024 https://github.com/python/cpython/pull/114839/commits/34b60651ef36ca65b8957751c7ed85c3af064f37
1c82786 Fix two compilation errors post merge mpage Mar 1, 2024 https://github.com/python/cpython/pull/114839/commits/1c82786c20e2b4e9ba7c2f25bfb3f7d2d5954dbc
0e86cf9 Use ThreadHandle as the single abstraction for thread joining mpage Mar 2, 2024 https://github.com/python/cpython/pull/114839/commits/0e86cf97670d54391cf9f3a2fa444009e8b0a97c
76bde03 Isolate all logic in _threadmodule mpage Mar 4, 2024 https://github.com/python/cpython/pull/114839/commits/76bde036984431df51f2c432b4cd078e6a9ccc58
02123b8 Fix flag mpage Mar 5, 2024 https://github.com/python/cpython/pull/114839/commits/02123b84818079be7cb870fd2f14bd69fbcccee7
24c1d47 Note that the once flag serializes both join and set_done mpage Mar 6, 2024 https://github.com/python/cpython/pull/114839/commits/24c1d476f19260914ace0cbb3449bed6015da9c7
1a1bfde Fix unused variable warning mpage Mar 6, 2024 https://github.com/python/cpython/pull/114839/commits/1a1bfde979cba5059b134ffa278428252952d926
b3c2c45 Rename ThreadHandleObject to PyThreadHandleObject mpage Mar 6, 2024 https://github.com/python/cpython/pull/114839/commits/b3c2c4513b25435b8ad097029ed4dd901dfe024a
7b9d007 Be consistent with documentation of true values mpage Mar 6, 2024 https://github.com/python/cpython/pull/114839/commits/7b9d0074b6581ac9fffd908caa669d9987abc069
5d0dc7a Threads started using `start_joinable_thread` should be daemon thread… mpage Mar 6, 2024 https://github.com/python/cpython/pull/114839/commits/5d0dc7a77d53cf184d8311347203722e2082c72a
11bb826 Remove any remaining handles once the module is cleared mpage Mar 7, 2024 https://github.com/python/cpython/pull/114839/commits/11bb8267daaf987c923bab2197c5fc60bc7ec034
06f6787 Always have a _ThreadHandle in Thread mpage Mar 8, 2024 https://github.com/python/cpython/pull/114839/commits/06f67879992e7aeb315834b0656f8a64ef45f227
d56f892 Check main thread handle during shutdown mpage Mar 8, 2024 https://github.com/python/cpython/pull/114839/commits/d56f89226bf3e68a60f7ec5b06d442ac5c05f0f9
57e106d Merge branch 'main' into gh-114271-remove-tstate_lock mpage Mar 8, 2024 https://github.com/python/cpython/pull/114839/commits/57e106dee41ce32e914afa970796428f99a30c0b
2218c0a Remove vestigial _PyEventRc declarations mpage Mar 8, 2024 https://github.com/python/cpython/pull/114839/commits/2218c0a4b6406c303b9e6dcc1383685d3403fdf6
f9d3290 Remove duplicate declaration mpage Mar 8, 2024 https://github.com/python/cpython/pull/114839/commits/f9d329078e8423e731c3406ed4e585b4023c790d
a7095e4 Revert order change in `_DummyThread.is_alive` mpage Mar 8, 2024 https://github.com/python/cpython/pull/114839/commits/a7095e47b60465c9a96fd1b2e7c822d7ae040f40
ccd1c2e Merge branch 'main' into gh-114271-remove-tstate_lock mpage Mar 11, 2024 https://github.com/python/cpython/pull/114839/commits/ccd1c2e3bc0db374b7b27c67e91b8109b53b040a
180300c Add handles to shutdown list before starting the thread mpage Mar 11, 2024 https://github.com/python/cpython/pull/114839/commits/180300c70fe8058c0aefa3ed97495ccfcc26008f
c486503 Move some code around to remove need for forward decls mpage Mar 12, 2024 https://github.com/python/cpython/pull/114839/commits/c486503616cb91932b247e6d35b57dc0ac8a8844
3121623 Simplify start failure path mpage Mar 12, 2024 https://github.com/python/cpython/pull/114839/commits/31216236c8bbe1a73b1b7f781a116cabbd49ee45
ee96259 Merge branch 'main' into gh-114271-remove-tstate_lock mpage Mar 15, 2024 https://github.com/python/cpython/pull/114839/commits/ee962590ded8a24a8d8b4c9c00d65afb443fc6b9
dc57ed2 Use infinitive in docstring for _shutdown mpage Mar 15, 2024 https://github.com/python/cpython/pull/114839/commits/dc57ed256a4a72b7c4f4fa91a35240472e631b65
48b86ae Update comment for _make_thread_handle mpage Mar 15, 2024 https://github.com/python/cpython/pull/114839/commits/48b86aee1e3fc74dbe87f8d7d08f61b51c321aec
9a8ea9b Merge branch 'main' into gh-114271-remove-tstate_lock pitrou Mar 16, 2024 https://github.com/python/cpython/pull/114839/commits/9a8ea9b5cca7fdc0e9cf0d3d4b40a87053c8be28
339b2e6 Merge branch 'main' into gh-114271-remove-tstate_lock pitrou Mar 16, 2024 https://github.com/python/cpython/pull/114839/commits/339b2e67784d0bbb157fb39f76e2b3160fbb6911
Clear filters https://github.com/python/cpython/pull/114839/commits/c86d349b4e935f3848f7d4dc42912effbdd9c963
Please reload this pagehttps://github.com/python/cpython/pull/114839/commits/c86d349b4e935f3848f7d4dc42912effbdd9c963
Please reload this pagehttps://github.com/python/cpython/pull/114839/commits/c86d349b4e935f3848f7d4dc42912effbdd9c963
Prev https://github.com/python/cpython/pull/114839/commits/19d7af12f14e615c4cb3f107c49084c8bbfcb314
Next https://github.com/python/cpython/pull/114839/commits/40e4b36f5d93a4c277679bcc30604fc07d29948d
gh-114271https://github.com/python/cpython/issues/114271
Please reload this pagehttps://github.com/python/cpython/pull/114839/commits/c86d349b4e935f3848f7d4dc42912effbdd9c963
https://github.com/mpage
mpagehttps://github.com/python/cpython/commits?author=mpage
view the diff.https://github.com/python/cpython/commit/c86d349b4e935f3848f7d4dc42912effbdd9c963
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.