René's URL Explorer Experiment


Title: gh-140746: Fix Thread.start() hanging when the thread dies during bootstrap by Jihaysse · Pull Request #153776 · python/cpython · GitHub

Open Graph Title: gh-140746: Fix Thread.start() hanging when the thread dies during bootstrap by Jihaysse · Pull Request #153776 · python/cpython

X Title: gh-140746: Fix Thread.start() hanging when the thread dies during bootstrap by Jihaysse · Pull Request #153776 · python/cpython

Description: Problem Thread.start() waits for the new thread with a bare self._started.wait(). If the new thread dies before _bootstrap_inner() reaches self._started.set(); typically a MemoryError raised while the C-level thread_run() calls _bootstrap under memory pressure (the interpreter prints Exception ignored in thread started by ...), or a SystemExit which thread_run() clears silently → the event is never set and the parent blocks forever. The dead thread also lingers in threading._limbo, so it stays visible in threading.enumerate() while is_alive() is False. Any threaded server whose dispatcher thread spawns per-request threads (e.g. socketserver.ThreadingMixIn) freezes permanently the first time this fires. This was detected in production on Python 3.12: workers stopped serving after a burst of MemoryErrors, with kill -3 stack dumps showing the dispatcher thread blocked in Thread.start() → self._started.wait(), the request thread perpetually in its initial state, and no corresponding OS thread. Full investigation in gh-140746 (also reported by Patroni). Reproducer Deterministic: simulates the MemoryError that thread_run() swallows while calling _bootstrap (the same C code path, PyObject_Call() returning NULL): import threading class BrokenBootstrapThread(threading.Thread): # Simulates a MemoryError raised before _started.set(): thread_run() # catches it ("Exception ignored in thread started by ...") and the # thread exits without ever signalling startup. def _bootstrap(self): raise MemoryError t = BrokenBootstrapThread(target=lambda: None) t.start() # unpatched: hangs here forever print("started fine:", t.is_alive()) Unpatched (reproduced on 3.12.11 and main): prints the "Exception ignored" traceback, then start() blocks forever. With this PR: start() raises RuntimeError: thread failed to start and the thread is removed from _limbo. The organic reproducer from the issue (progressively lowering RLIMIT_DATA until a real MemoryError lands in the thread's bootstrap) also hangs unpatched and fails cleanly with this PR after ~1300 progressively-starved thread starts. Fix Add a C-level thread_started PyEvent to ThreadHandle, mirroring the existing thread_is_exiting: _bootstrap_inner() notifies it (via handle._set_started()) right after self._started.set(), so a waiter woken by the child always observes _started as set; thread_run() notifies it unconditionally on its exit path — covering a failed PyObject_Call of _bootstrap, the silent SystemExit case, and the _PyThreadState_MustExit() early exit during finalization. _PyEvent_Notify() is allocation-free C and a no-op on an already-set event, so this is safe on every exit path. Thread.start() waits on the C event (_wait_for_started(): GIL released, interruptible via Py_MakePendingCalls()) and then checks _started: set means the thread started normally (identical behavior to today); unset means the thread died before starting and never will run, so start() removes it from _limbo and raises RuntimeError("thread failed to start"). No timeout, no polling, no new handle states. Validation Environment: Ubuntu 24.04 x86-64 (Linux 6.17), gcc 13.3.0; main built both --with-pydebug and --disable-gil; stock 3.12.11 as the unpatched baseline. Both reproducers above: hang unpatched, clean RuntimeError + _limbo cleanup with this PR (both builds). End-to-end: a ThreadingTCPServer whose request thread dies in bootstrap freezes permanently unpatched; with this PR, handle_error() logs the RuntimeError and the server keeps serving. Test suites (both builds): test_threading, test_thread, test_threadedtempfile, test_thread_local_bytecode, test_socketserver -u all, test_concurrent_futures.test_thread_pool, test_queue — all pass. Three regression tests added: MemoryError in _bootstrap, MemoryError in _bootstrap_inner, and SystemExit in _bootstrap.

Open Graph Description: Problem Thread.start() waits for the new thread with a bare self._started.wait(). If the new thread dies before _bootstrap_inner() reaches self._started.set(); typically a MemoryError raised while ...

X Description: Problem Thread.start() waits for the new thread with a bare self._started.wait(). If the new thread dies before _bootstrap_inner() reaches self._started.set(); typically a MemoryError raised while ...

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

X: @github

direct link

Domain: github.com

react-profiling0
react-import-mapreact,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322
route-pattern/:user_id/:repository/pull/:id/files(.:format)
route-controllerpull_requests
route-actionfiles
fetch-noncev2:d0412870-5bfe-8d18-d3f9-06397f22cb89
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-idBE9C:D4E59:119C3DB:161F425:6B023CB9
html-safe-noncedae019450dce0622367c06d61cb8d8ad1d8c505acbbd0f62db66d29ac9d388d8
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRTlDOkQ0RTU5OjExOUMzREI6MTYxRjQyNTo2QjAyM0NCOSIsInZpc2l0b3JfaWQiOiIxNTE0NjI5MDM5NTcwNDQ0MDkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmacf29fe3b6308e9c4572601fadd01ae382e5688be510eb8fe6499004a9a55c4075
hovercard-subject-tagpull_request:4062456916
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/153776/files
twitter:imagehttps://avatars.githubusercontent.com/u/64892838?s=400&v=4
twitter:cardsummary_large_image
og:imagehttps://avatars.githubusercontent.com/u/64892838?s=400&v=4
og:image:altProblem Thread.start() waits for the new thread with a bare self._started.wait(). If the new thread dies before _bootstrap_inner() reaches self._started.set(); typically a MemoryError raised while ...
og:site_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
None87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456
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
release423dbb54c5e7bfecc838eb387c1435eb3d0e760d
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python/cpython/pull/153776/files#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fpull%2F153776%2Ffiles
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
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
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://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
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fpull%2F153776%2Ffiles
Sign uphttps://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/153776/files
Reloadhttps://github.com/python/cpython/pull/153776/files
Reloadhttps://github.com/python/cpython/pull/153776/files
pythonhttps://github.com/python
cpythonhttps://github.com/python/cpython
Codehttps://github.com/python/cpython
Issues5k+ (5k+)https://github.com/python/cpython/issues
Pull requests2.4k (2.4k)https://github.com/python/cpython/pulls
Actionshttps://github.com/python/cpython/actions
Projectshttps://github.com/python/cpython/projects
Security and qualityhttps://github.com/python/cpython/security
Insightshttps://github.com/python/cpython/insights
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
Jihayssehttps://github.com/Jihaysse
python:mainhttps://github.com/python/cpython/tree/main
Jihaysse:fix-140746-thread-start-hanghttps://github.com/Jihaysse/cpython/tree/fix-140746-thread-start-hang
Conversation 3 https://github.com/python/cpython/pull/153776
Commits 2 https://github.com/python/cpython/pull/153776/commits
Checks 52 https://github.com/python/cpython/pull/153776/checks
Files changed https://github.com/python/cpython/pull/153776/files
Please reload this pagehttps://github.com/python/cpython/pull/153776/files
gh-140746: Fix Thread.start() hanging when the thread dies during bootstrap https://github.com/python/cpython/pull/153776/files#top
Show all changes 2 commits https://github.com/python/cpython/pull/153776/files
07d6226 gh-140746: Fix Thread.start() hanging when the thread dies during boo… Jihaysse Jul 15, 2026 https://github.com/python/cpython/pull/153776/commits/07d622665005ca81de49f4da5812a4d807e6f96f
0dfdddd Merge remote-tracking branch 'upstream/main' into fix-140746-thread-s… Jihaysse Oct 29, 2026 https://github.com/python/cpython/pull/153776/commits/0dfddddcf5caf90b5a07273caaed5f1395a3a91f
Clear filters https://github.com/python/cpython/pull/153776/files
Please reload this pagehttps://github.com/python/cpython/pull/153776/files
Please reload this pagehttps://github.com/python/cpython/pull/153776/files
threading.rst https://github.com/python/cpython/pull/153776/files#diff-6a424b7e58eb2394ee0335dd6e08ab8d67e7058852fae0f3aa1b40493fa14a64
test_threading.py https://github.com/python/cpython/pull/153776/files#diff-8b4c7f5f9b60a27c4132899af6fa7157c7f1e57a0b6213bc616fe72fba04c168
threading.py https://github.com/python/cpython/pull/153776/files#diff-b250d3d0e0d909784a67083e9069ce507513a67e8514a011e2f364027e25e0b0
2026-07-15-10-00-00.gh-issue-140746.limThd.rst https://github.com/python/cpython/pull/153776/files#diff-063b57c388cdf54b961c16528f00a9107d2e6a0d10048465d025f5470ebc79fd
_threadmodule.c https://github.com/python/cpython/pull/153776/files#diff-23071700ee893f365a4c117210b9129d4ac6afe5cc6e52bb5cd4cabbc6816e07
_threadmodule.c.h https://github.com/python/cpython/pull/153776/files#diff-394dbb9e1e29b146c0ab7bbad937dc073eb0cc3144d503d60942e79d93eb3094
Doc/library/threading.rsthttps://github.com/python/cpython/pull/153776/files#diff-6a424b7e58eb2394ee0335dd6e08ab8d67e7058852fae0f3aa1b40493fa14a64
View file https://github.com/python/cpython/blob/0dfddddcf5caf90b5a07273caaed5f1395a3a91f/Doc/library/threading.rst
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/153776/{{ revealButtonHref }}
https://github.com/python/cpython/pull/153776/files#diff-6a424b7e58eb2394ee0335dd6e08ab8d67e7058852fae0f3aa1b40493fa14a64
https://github.com/python/cpython/pull/153776/files#diff-6a424b7e58eb2394ee0335dd6e08ab8d67e7058852fae0f3aa1b40493fa14a64
https://github.com/python/cpython/pull/153776/files#diff-6a424b7e58eb2394ee0335dd6e08ab8d67e7058852fae0f3aa1b40493fa14a64
Lib/test/test_threading.pyhttps://github.com/python/cpython/pull/153776/files#diff-8b4c7f5f9b60a27c4132899af6fa7157c7f1e57a0b6213bc616fe72fba04c168
View file https://github.com/python/cpython/blob/0dfddddcf5caf90b5a07273caaed5f1395a3a91f/Lib/test/test_threading.py
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/153776/{{ revealButtonHref }}
https://github.com/python/cpython/pull/153776/files#diff-8b4c7f5f9b60a27c4132899af6fa7157c7f1e57a0b6213bc616fe72fba04c168
https://github.com/python/cpython/pull/153776/files#diff-8b4c7f5f9b60a27c4132899af6fa7157c7f1e57a0b6213bc616fe72fba04c168
Lib/threading.pyhttps://github.com/python/cpython/pull/153776/files#diff-b250d3d0e0d909784a67083e9069ce507513a67e8514a011e2f364027e25e0b0
View file https://github.com/python/cpython/blob/0dfddddcf5caf90b5a07273caaed5f1395a3a91f/Lib/threading.py
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/153776/{{ revealButtonHref }}
https://github.com/python/cpython/pull/153776/files#diff-b250d3d0e0d909784a67083e9069ce507513a67e8514a011e2f364027e25e0b0
https://github.com/python/cpython/pull/153776/files#diff-b250d3d0e0d909784a67083e9069ce507513a67e8514a011e2f364027e25e0b0
https://github.com/python/cpython/pull/153776/files#diff-b250d3d0e0d909784a67083e9069ce507513a67e8514a011e2f364027e25e0b0
https://github.com/python/cpython/pull/153776/files#diff-b250d3d0e0d909784a67083e9069ce507513a67e8514a011e2f364027e25e0b0
Misc/NEWS.d/next/Core_and_Builtins/2026-07-15-10-00-00.gh-issue-140746.limThd.rsthttps://github.com/python/cpython/pull/153776/files#diff-063b57c388cdf54b961c16528f00a9107d2e6a0d10048465d025f5470ebc79fd
View file https://github.com/python/cpython/blob/0dfddddcf5caf90b5a07273caaed5f1395a3a91f/Misc/NEWS.d/next/Core_and_Builtins/2026-07-15-10-00-00.gh-issue-140746.limThd.rst
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/153776/{{ revealButtonHref }}
Modules/_threadmodule.chttps://github.com/python/cpython/pull/153776/files#diff-23071700ee893f365a4c117210b9129d4ac6afe5cc6e52bb5cd4cabbc6816e07
View file https://github.com/python/cpython/blob/0dfddddcf5caf90b5a07273caaed5f1395a3a91f/Modules/_threadmodule.c
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/153776/{{ revealButtonHref }}
https://github.com/python/cpython/pull/153776/files#diff-23071700ee893f365a4c117210b9129d4ac6afe5cc6e52bb5cd4cabbc6816e07
https://github.com/python/cpython/pull/153776/files#diff-23071700ee893f365a4c117210b9129d4ac6afe5cc6e52bb5cd4cabbc6816e07
https://github.com/python/cpython/pull/153776/files#diff-23071700ee893f365a4c117210b9129d4ac6afe5cc6e52bb5cd4cabbc6816e07
https://github.com/python/cpython/pull/153776/files#diff-23071700ee893f365a4c117210b9129d4ac6afe5cc6e52bb5cd4cabbc6816e07
https://github.com/python/cpython/pull/153776/files#diff-23071700ee893f365a4c117210b9129d4ac6afe5cc6e52bb5cd4cabbc6816e07
https://github.com/python/cpython/pull/153776/files#diff-23071700ee893f365a4c117210b9129d4ac6afe5cc6e52bb5cd4cabbc6816e07
https://github.com/python/cpython/pull/153776/files#diff-23071700ee893f365a4c117210b9129d4ac6afe5cc6e52bb5cd4cabbc6816e07
https://github.com/python/cpython/pull/153776/files#diff-23071700ee893f365a4c117210b9129d4ac6afe5cc6e52bb5cd4cabbc6816e07
https://github.com/python/cpython/pull/153776/files#diff-23071700ee893f365a4c117210b9129d4ac6afe5cc6e52bb5cd4cabbc6816e07
https://github.com/python/cpython/pull/153776/files#diff-23071700ee893f365a4c117210b9129d4ac6afe5cc6e52bb5cd4cabbc6816e07
https://github.com/python/cpython/pull/153776/files#diff-23071700ee893f365a4c117210b9129d4ac6afe5cc6e52bb5cd4cabbc6816e07
https://github.com/python/cpython/pull/153776/files#diff-23071700ee893f365a4c117210b9129d4ac6afe5cc6e52bb5cd4cabbc6816e07
Modules/clinic/_threadmodule.c.hhttps://github.com/python/cpython/pull/153776/files#diff-394dbb9e1e29b146c0ab7bbad937dc073eb0cc3144d503d60942e79d93eb3094
View file https://github.com/python/cpython/blob/0dfddddcf5caf90b5a07273caaed5f1395a3a91f/Modules/clinic/_threadmodule.c.h
Open in desktop https://desktop.github.com
how customized files appear on GitHubhttps://docs.github.com/github/administering-a-repository/customizing-how-changed-files-appear-on-github
Please reload this pagehttps://github.com/python/cpython/pull/153776/files
Please reload this pagehttps://github.com/python/cpython/pull/153776/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.