René's URL Explorer Experiment


Title: test_concurrent_futures.test_shutdown: test_interpreter_shutdown() fails randomly (race condition) · Issue #109047 · python/cpython · GitHub

Open Graph Title: test_concurrent_futures.test_shutdown: test_interpreter_shutdown() fails randomly (race condition) · Issue #109047 · python/cpython

X Title: test_concurrent_futures.test_shutdown: test_interpreter_shutdown() fails randomly (race condition) · Issue #109047 · python/cpython

Description: The test_interpreter_shutdown() test of test_concurrent_futures.test_shutdown has a race condition. On purpose, the test doesn't wait until the executor completes (!). Moreover, it expects the executor to always be able to submit its job...

Open Graph Description: The test_interpreter_shutdown() test of test_concurrent_futures.test_shutdown has a race condition. On purpose, the test doesn't wait until the executor completes (!). Moreover, it expects the exec...

X Description: The test_interpreter_shutdown() test of test_concurrent_futures.test_shutdown has a race condition. On purpose, the test doesn't wait until the executor completes (!). Moreover, it expects the ...

Opengraph URL: https://github.com/python/cpython/issues/109047

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"test_concurrent_futures.test_shutdown: test_interpreter_shutdown() fails randomly (race condition)","articleBody":"The ``test_interpreter_shutdown()`` test of ``test_concurrent_futures.test_shutdown`` has a race condition. On purpose, the test doesn't wait until the executor completes (!). Moreover, it **expects** the executor to always be able to submit its job, and the job to complete successfully! It's a very optimistic bet.\r\n\r\nSee also issue #107219: test_concurrent_futures: **test_crash_big_data()** hangs randomly on **Windows**.\r\n\r\n---\r\n\r\nWhen Python is shutting down, ``Py_Finalize()`` quickly blocks the creation of new threads in ``_thread.start_new_thread()``:\r\n\r\nhttps://github.com/python/cpython/blob/3bfa24e29f286cbc1f42bdb4d2b1c0c9d643c8d6/Modules/_threadmodule.c#L1161-L1165\r\n\r\nThis exception was added recently (last June) by commit ce558e69d4087dd3653207de78345fbb8a2c7835: see issue gh-104690 for the rationale.\r\n\r\nThe multiprocessing executor spawns ``_ExecutorManagerThread`` thread which runs its \"main loop\" in its ``run()`` method:\r\n\r\nhttps://github.com/python/cpython/blob/3bfa24e29f286cbc1f42bdb4d2b1c0c9d643c8d6/Lib/concurrent/futures/process.py#L335-L378\r\n\r\nIt tries to submit new jobs to the worker process through a queue, but oops, the Python main thread is finalizing (called ``Py_Finalizing()``)! There is not notification system to notify threads that Python is being finalized.\r\n\r\n---\r\n\r\nMoreover, there are **3** \"finalization\" states:\r\n\r\n* ``interp-\u003efinalizing`` -- used by ``_thread.start_new_thread()`` to block thread creation during Python finazlization\r\n* ``runtime-\u003e_finalizing`` -- used by ``sys.is_finalizing()``, ``Py_IsFinalizing()`` and ``_PyRuntimeState_GetFinalizing(runtime)``\r\n* ``interp-\u003e_finalizing`` -- used by ``ceval.c`` to decide if a Python thread \"must exit\" or not, as soon as it's set, all Python threads must exit as soon as they **attempt to acquire the GIL**\r\n\r\nThese 3 states at not set at the same time.\r\n\r\n1. Calling ``Py_Finalize()`` sets ``interp-\u003efinalizing`` to 1 as soon as possible: so spawning new threads is *immediately* blocked (which is a good thing to get a reliable finalization!)\r\n2. ``Py_Finalize()`` calls ``threading._shutdown()`` which blocks until all non-daemon threads completes\r\n3. ``Py_Finalize()`` calls ``atexit`` callbacks\r\n4. And only then, ``Py_Finalize()`` sets ``runtime-\u003e_finalizing`` *and* ``interp-\u003e_finalizing`` to the Python thread state (``tstate``) which calls ``Py_Finalize()``\r\n\r\nThe delay between (1) and (4) can be quite long, a thread can take several milliseconds, if not seconds, to complete.\r\n\r\n---\r\n\r\nCan ``multiprocessing`` or ``concurrent.futures`` check if Python is finalizing or be notified? Well, did you hear about [Time-of-check to time-of-use](https://en.wikipedia.org/wiki/Time-of-check_to_time-of-use) race conditions? Even if it would be possible, I don't think that we can \"check\" if it's safe to spawn a thread *just before* spawning a thread, since the main thread can decide to finalize Python \"at any time\". It will become even more tricky with Python nogil ;-)\r\n\r\nSo what's left? Well, ``multiprocessing`` and ``concurrent.futures`` should be optimistic, call Python functions and only *then* check for exceptions. Depending on the exceptions, they can decide how to handle it. I would suggest to exit as soon as possible, and try to cleanup resources if possible.\r\n\r\nAnother option would be to make ``multiprocessing`` and ``concurrent.futures`` more determistic. Rather than spawning threads and processes in the background \"on demand\" and hope that everything will be fine, add more synchronization to \"wait\" until everything is ready to submit jobs. I think that I already tried this approach in the past, but @pitrou didn't like it since it made some workloads slower. You may not always need to actually submits jobs. You may not always need all threads and processes.\r\n\r\nWell, I don't know even these complex modules to tell which option is the least bad :-)\r\n\r\nFinally, as usually, I beg you to make these APIs less magical, and enforce more explicit resources management! It shouldn't even be possible to not wait until an executor complete. It should be enforced by emitting loudly ``ResourceWarning`` warnings :-) Well, that's my opinion. I know that it's not shared by @pitrou :-)\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-109809\n* gh-109810\n* gh-110126\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/vstinner","@type":"Person","name":"vstinner"},"datePublished":"2023-09-07T02:15:33.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":8},"url":"https://github.com/109047/cpython/issues/109047"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:80431b84-0637-7fae-4072-85b51e87758f
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id96D2:338071:431C18:5DF353:696A6AD3
html-safe-noncef7908f9a6c5a48306b280a9d95620f28934a7c7d3ca7646aea12f96ba47a26df
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NkQyOjMzODA3MTo0MzFDMTg6NURGMzUzOjY5NkE2QUQzIiwidmlzaXRvcl9pZCI6IjE1NzEzMDk4NzAwMDAwMDc4OTEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac25a4a7f5acc26b90d4448e0a8c85d2c1d913bcb50b23d176006c934d9dbcf4bc
hovercard-subject-tagissue:1885001097
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/109047/issue_layout
twitter:imagehttps://opengraph.githubassets.com/7812ae2d39c2f4ca879d85190f17035cbd5edb740c3efe4cfa4fcef2be0c9f18/python/cpython/issues/109047
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/7812ae2d39c2f4ca879d85190f17035cbd5edb740c3efe4cfa4fcef2be0c9f18/python/cpython/issues/109047
og:image:altThe test_interpreter_shutdown() test of test_concurrent_futures.test_shutdown has a race condition. On purpose, the test doesn't wait until the executor completes (!). Moreover, it expects the exec...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamevstinner
hostnamegithub.com
expected-hostnamegithub.com
None6fea32d5b7276b841b7a803796d9715bc6cfb31ed549fdf9de2948ac25d12ba6
turbo-cache-controlno-preview
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
releasef2d9f6432a5a115ec709295ae70623f33bb80aee
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python/cpython/issues/109047#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F109047
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%2Fissues%2F109047
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%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Fcpython
Reloadhttps://github.com/python/cpython/issues/109047
Reloadhttps://github.com/python/cpython/issues/109047
Reloadhttps://github.com/python/cpython/issues/109047
python https://github.com/python
cpythonhttps://github.com/python/cpython
Please reload this pagehttps://github.com/python/cpython/issues/109047
Notifications https://github.com/login?return_to=%2Fpython%2Fcpython
Fork 33.9k https://github.com/login?return_to=%2Fpython%2Fcpython
Star 71.1k 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 Uh oh! There was an error while loading. Please reload this page. https://github.com/python/cpython/security
Please reload this pagehttps://github.com/python/cpython/issues/109047
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
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/109047
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/109047
test_concurrent_futures.test_shutdown: test_interpreter_shutdown() fails randomly (race condition)https://github.com/python/cpython/issues/109047#top
testsTests in the Lib/test dirhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22tests%22
https://github.com/vstinner
https://github.com/vstinner
vstinnerhttps://github.com/vstinner
on Sep 7, 2023https://github.com/python/cpython/issues/109047#issue-1885001097
#107219https://github.com/python/cpython/issues/107219
cpython/Modules/_threadmodule.chttps://github.com/python/cpython/blob/3bfa24e29f286cbc1f42bdb4d2b1c0c9d643c8d6/Modules/_threadmodule.c#L1161-L1165
3bfa24ehttps://github.com/python/cpython/commit/3bfa24e29f286cbc1f42bdb4d2b1c0c9d643c8d6
ce558e6https://github.com/python/cpython/commit/ce558e69d4087dd3653207de78345fbb8a2c7835
gh-104690https://github.com/python/cpython/issues/104690
cpython/Lib/concurrent/futures/process.pyhttps://github.com/python/cpython/blob/3bfa24e29f286cbc1f42bdb4d2b1c0c9d643c8d6/Lib/concurrent/futures/process.py#L335-L378
3bfa24ehttps://github.com/python/cpython/commit/3bfa24e29f286cbc1f42bdb4d2b1c0c9d643c8d6
Time-of-check to time-of-usehttps://en.wikipedia.org/wiki/Time-of-check_to_time-of-use
@pitrouhttps://github.com/pitrou
@pitrouhttps://github.com/pitrou
gh-109047: Add PythonFinalizationError exception #109809https://github.com/python/cpython/pull/109809
gh-109047: concurrent.futures catches RuntimeError #109810https://github.com/python/cpython/pull/109810
[3.12] gh-109047: concurrent.futures catches RuntimeError (#109810) #110126https://github.com/python/cpython/pull/110126
testsTests in the Lib/test dirhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22tests%22
Multiprocessing issueshttps://github.com/orgs/python/projects/14
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.