René's URL Explorer Experiment


Title: Multiprocessing PipeConnection handles leak on failed subprocess spawn · Issue #114720 · python/cpython · GitHub

Open Graph Title: Multiprocessing PipeConnection handles leak on failed subprocess spawn · Issue #114720 · python/cpython

X Title: Multiprocessing PipeConnection handles leak on failed subprocess spawn · Issue #114720 · python/cpython

Description: Bug report Bug description: On Windows, if a multiprocessing.Process fails to spawn, and it is given multiprocessing.Pipe(duplex=True) arguments, then the underlying handles pipe handles leak. One way to cause a subprocess to fail to spa...

Open Graph Description: Bug report Bug description: On Windows, if a multiprocessing.Process fails to spawn, and it is given multiprocessing.Pipe(duplex=True) arguments, then the underlying handles pipe handles leak. One ...

X Description: Bug report Bug description: On Windows, if a multiprocessing.Process fails to spawn, and it is given multiprocessing.Pipe(duplex=True) arguments, then the underlying handles pipe handles leak. One ...

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

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Multiprocessing PipeConnection handles leak on failed subprocess spawn","articleBody":"# Bug report\n\n### Bug description:\n\nOn Windows, if a `multiprocessing.Process` fails to spawn, and it is given `multiprocessing.Pipe(duplex=True)` arguments, then the underlying handles pipe handles leak. One way to cause a subprocess to fail to spawn is forgetting to guard spawning code with `if __name__ == \"__main__\":`. Here is an MRE along those lines:\n\n```python\nimport multiprocessing\nimport traceback\n\nEXHIBIT_LEAK = True\n\n\ndef child(*pipes):\n    print(\"child\", pipes)\n    for pipe in pipes:\n        pipe.close()\n\n\ndef parent(*pipes):\n    print(\"parent\", pipes)\n    p = multiprocessing.Process(target=child, args=pipes)\n    p.start()\n    for pipe in pipes:\n        pipe.close()\n    p.join()\n\n\nif EXHIBIT_LEAK or __name__ == \"__main__\":\n    child_send_pipe, recv_pipe = multiprocessing.Pipe(duplex=True)\n    send_pipe, child_recv_pipe = multiprocessing.Pipe(duplex=True)\n    parent(child_send_pipe, child_recv_pipe)\n    print(\"after\")\n    try:\n        send_pipe.send_bytes(b\"test\")\n    except:\n        traceback.print_exc()\n    else:\n        print(\"send failed to raise\")\n    try:\n        recv_pipe.poll()\n    except:\n        traceback.print_exc()\n    else:\n        print(\"recv failed to raise\")\n\n```\n\nI noticed this because it makes the death of the process impossible to detect by looking only at the pipes, leading to an [ugly workaround](https://github.com/richardsheridan/trio-parallel/blob/22b96bd63931db1d4d6f6982adf65a9a0679bab4/trio_parallel/_proc.py#L72-L93) that I only recently realized was actually leaking resources.\n\nThis is in principle the same bug as bpo-33929. To be honest I don't understand the fix there enough to know if it could be generalized to `PipeConnection` objects, but it would likely prevent leaks from all sorts of handle stealing edge cases. Otherwise, workaround for the specific case of recursive spawning could be achieved by signaling the unpickling error back to `parent` during the `Process.start()` method. The state of the system as the MRE failure occurs can be summarized as:\n\n- `parent` calls `Process.start` -\u003e `self._Popen(self)` -\u003e`CreateProcess`\n- `parent` dumps `prep_data` and then `process_obj` sequentially `to_child` with big buffer, so it's nonblocking\n- dump of `process_obj` induces duplication of `PipeConnection` handle via `DupeHandle`\n- `child` runs `mp.spawn.spawn_main` -\u003e `_main` \n- `child` drains data (fd `from_parent` of GH-7921) only to `reduction.pickle.load(from_parent)`\n- `child` unpickles `__main__` module\n- `child` notices issue in `Process.start` -\u003e `self._Popen(self)` -\u003e `spawn.get_preparation_data` -\u003e `_check_not_importing_main`\n- `child` raises and never unpickles the arguments and so never steals the handle via `DupeHandle.detach()`\n- `parent` calls `poll` on read end of `PipeConnection`\n- `parent` never gets expected `BrokenPipeError`\n\nIf `Process.start` can raise and close [`from_parent`](https://github.com/python/cpython/blob/c87233fd3fa77067013c35328f8c4884f0567a59/Lib/multiprocessing/spawn.py#L127) (actually I'm not sure if `from_parent` or `fd` or both must be closed) in `child` before [`dump(process_obj, to_child)`](https://github.com/python/cpython/blob/c87233fd3fa77067013c35328f8c4884f0567a59/Lib/multiprocessing/popen_spawn_win32.py#L95) in `parent`, all stealing leaks would prevented. However, that would require some sort of IO wait between [line 94 and 95](https://github.com/python/cpython/blob/c87233fd3fa77067013c35328f8c4884f0567a59/Lib/multiprocessing/popen_spawn_win32.py#L94-L95). My first thought was to make another private pipe pair only for the purpose of signaling `parent` if `prep_data` were successfully unpickled or not. `parent` would then either read a sentinel out of the pipe or raise some kind of exception. Also, wouldn't it be better in principle for `start` to raise an exception and clean up if it knows it failed to start?\n\n\n\n### CPython versions tested on:\n\n3.8, 3.9, 3.10, 3.11, 3.12\n\n### Operating systems tested on:\n\nWindows","author":{"url":"https://github.com/richardsheridan","@type":"Person","name":"richardsheridan"},"datePublished":"2024-01-29T15:37:46.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/114720/cpython/issues/114720"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:22ce78a5-11ac-0198-4900-ad828537639e
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id85DA:E9032:FA1EA:16C2C0:6A58A662
html-safe-nonce2008a6c810ad20403f1c806c6e42b1b2163577c794dc75d1a08ea59360d1c557
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NURBOkU5MDMyOkZBMUVBOjE2QzJDMDo2QTU4QTY2MiIsInZpc2l0b3JfaWQiOiI2MTY4OTI1NjI1OTk4ODAyOTAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac3e157886481407751da0cec932fe6cb848ede807ec6a952f03ea2facbb211fb0
hovercard-subject-tagissue:2105776270
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/114720/issue_layout
twitter:imagehttps://opengraph.githubassets.com/f547eca68913f71a2b7484980ce8c3c9fa1737ec53bec1cad39445a293530932/python/cpython/issues/114720
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/f547eca68913f71a2b7484980ce8c3c9fa1737ec53bec1cad39445a293530932/python/cpython/issues/114720
og:image:altBug report Bug description: On Windows, if a multiprocessing.Process fails to spawn, and it is given multiprocessing.Pipe(duplex=True) arguments, then the underlying handles pipe handles leak. One ...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamerichardsheridan
hostnamegithub.com
expected-hostnamegithub.com
None5f2a0c7865178af3d91dd9f13b0cdfc3c73a2529c873d2780bb4c01271a57ec6
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
release8aae7b8d6caacacf5c66eaeb2702d8dc88d85b4a
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python/cpython/issues/114720#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F114720
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%2Fissues%2F114720
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/114720
Reloadhttps://github.com/python/cpython/issues/114720
Reloadhttps://github.com/python/cpython/issues/114720
Please reload this pagehttps://github.com/python/cpython/issues/114720
python https://github.com/python
cpythonhttps://github.com/python/cpython
Please reload this pagehttps://github.com/python/cpython/issues/114720
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
Multiprocessing PipeConnection handles leak on failed subprocess spawnhttps://github.com/python/cpython/issues/114720#top
topic-multiprocessinghttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-multiprocessing%22
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
https://github.com/richardsheridan
richardsheridanhttps://github.com/richardsheridan
on Jan 29, 2024https://github.com/python/cpython/issues/114720#issue-2105776270
ugly workaroundhttps://github.com/richardsheridan/trio-parallel/blob/22b96bd63931db1d4d6f6982adf65a9a0679bab4/trio_parallel/_proc.py#L72-L93
bpo-33929https://bugs.python.org/issue?@action=redirect&bpo=33929
bpo-33929: multiprocessing: fix handle leak on race condition #7921https://github.com/python/cpython/pull/7921
from_parenthttps://github.com/python/cpython/blob/c87233fd3fa77067013c35328f8c4884f0567a59/Lib/multiprocessing/spawn.py#L127
dump(process_obj, to_child)https://github.com/python/cpython/blob/c87233fd3fa77067013c35328f8c4884f0567a59/Lib/multiprocessing/popen_spawn_win32.py#L95
line 94 and 95https://github.com/python/cpython/blob/c87233fd3fa77067013c35328f8c4884f0567a59/Lib/multiprocessing/popen_spawn_win32.py#L94-L95
topic-multiprocessinghttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-multiprocessing%22
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
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.