René's URL Explorer Experiment


Title: bpo-35823: subprocess: Use vfork() instead of fork() on Linux when safe by izbyshev · Pull Request #11671 · python/cpython · GitHub

Open Graph Title: bpo-35823: subprocess: Use vfork() instead of fork() on Linux when safe by izbyshev · Pull Request #11671 · python/cpython

X Title: bpo-35823: subprocess: Use vfork() instead of fork() on Linux when safe by izbyshev · Pull Request #11671 · python/cpython

Description: When used to run a new executable image, fork() is not a good choice for process creation, especially if the parent has a large working set: fork() needs to copy page tables, which is slow, and may fail on systems where overcommit is disabled, despite that the child is not going to touch most of its address space. Currently, subprocess is capable of using posix_spawn() instead, which normally provides much better performance. However, posix_spawn() does not support many of child setup operations exposed by subprocess.Popen(). Most notably, it's not possible to express close_fds=True, which happens to be the default, via posix_spawn(). As a result, most users can't benefit from faster process creation, at least not without changing their code. However, Linux provides vfork() system call, which creates a new process without copying the address space of the parent, and which is actually used by C libraries to efficiently implement posix_spawn(). Due to sharing of the address space and even the stack with the parent, extreme care is required to use vfork(). At least the following restrictions must hold: No signal handlers must execute in the child process. Otherwise, they might clobber memory shared with the parent, potentially confusing it. Any library function called after vfork() in the child must be async-signal-safe (as for fork()), but it must also not interact with any library state in a way that might break due to address space sharing and/or lack of any preparations performed by libraries on normal fork(). POSIX.1 permits to call only execve() and _exit(), and later revisions remove vfork() specification entirely. In practice, however, almost all operations needed by subprocess.Popen() can be safely implemented on Linux. Due to sharing of the stack with the parent, the child must be careful not to clobber local variables that are alive across vfork() call. Compilers are normally aware of this and take extra care with vfork() (and setjmp(), which has a similar problem). In case the parent is privileged, special attention must be paid to vfork() use, because sharing an address space across different privilege domains is insecure[1]. This patch adds support for using vfork() instead of fork() on Linux when it's possible to do safely given the above. In particular: vfork() is not used if credential switch is requested. The reverse case (simple subprocess.Popen() but another application thread switches credentials concurrently) is not possible for pure-Python apps because subprocess.Popen() and functions like os.setuid() are mutually excluded via GIL. We might also consider to add a way to opt-out of vfork() (and posix_spawn() on platforms where it might be implemented via vfork()) in a future PR. vfork() is not used if preexec_fn != None. With this change, subprocess will still use posix_spawn() if possible, but will fallback to vfork() on Linux in most cases, and, failing that, to fork(). [1] https://ewontfix.com/7 https://bugs.python.org/issue35823

Open Graph Description: When used to run a new executable image, fork() is not a good choice for process creation, especially if the parent has a large working set: fork() needs to copy page tables, which is slow, and may...

X Description: When used to run a new executable image, fork() is not a good choice for process creation, especially if the parent has a large working set: fork() needs to copy page tables, which is slow, and may...

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

X: @github

direct link

Domain: github.com

route-pattern/:user_id/:repository/pull/:id/files(.:format)
route-controllerpull_requests
route-actionfiles
fetch-noncev2:32e74278-1d44-0f9d-2acf-6bdfc7fab59e
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-idA9E4:23CB5E:CC5EA9:1209A0B:696A6409
html-safe-nonce50d2305e295c7d148cebf4d48eca4a1e859669d967485fa785192523b61df8c6
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBOUU0OjIzQ0I1RTpDQzVFQTk6MTIwOUEwQjo2OTZBNjQwOSIsInZpc2l0b3JfaWQiOiI0MDU5MzUwOTkxMjg0NzI1ODUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmace3e99d35561a613681da1a59b8261e5b62d1271075972e23471424f10599aaa8
hovercard-subject-tagpull_request:247537234
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/11671/files
twitter:imagehttps://avatars.githubusercontent.com/u/11427362?s=400&v=4
twitter:cardsummary_large_image
og:imagehttps://avatars.githubusercontent.com/u/11427362?s=400&v=4
og:image:altWhen used to run a new executable image, fork() is not a good choice for process creation, especially if the parent has a large working set: fork() needs to copy page tables, which is slow, and may...
og:site_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
None9b7735a184970dd9333b2cbe036c8f3c0a9108c64aaa93827c5a64fc70993392
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
release87cbd411c2982752221b5751d583a515b23bf5fa
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python/cpython/pull/11671/files#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fpull%2F11671%2Ffiles
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%2F11671%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/11671/files
Reloadhttps://github.com/python/cpython/pull/11671/files
Reloadhttps://github.com/python/cpython/pull/11671/files
python https://github.com/python
cpythonhttps://github.com/python/cpython
Please reload this pagehttps://github.com/python/cpython/pull/11671/files
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/pull/11671/files
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
gpsheadhttps://github.com/gpshead
python:masterhttps://github.com/python/cpython/tree/master
izbyshev:vforkhttps://github.com/izbyshev/cpython/tree/vfork
Conversation 18 https://github.com/python/cpython/pull/11671
Commits 2 https://github.com/python/cpython/pull/11671/commits
Checks 0 https://github.com/python/cpython/pull/11671/checks
Files changed https://github.com/python/cpython/pull/11671/files
Please reload this pagehttps://github.com/python/cpython/pull/11671/files
bpo-35823: subprocess: Use vfork() instead of fork() on Linux when safe https://github.com/python/cpython/pull/11671/files#top
Show all changes 2 commits https://github.com/python/cpython/pull/11671/files
c81bf6e bpo-35823: subprocess: Use vfork() instead of fork() on Linux when safe Oct 16, 2020 https://github.com/python/cpython/pull/11671/commits/c81bf6e2a2e02077fe87c967a53a542d8e2dc4a0
f226d33 Prevent vfork() when call_setsid is set. gpshead Oct 23, 2020 https://github.com/python/cpython/pull/11671/commits/f226d335d1090d7325d1c6329dec0dd2733bddbb
Clear filters https://github.com/python/cpython/pull/11671/files
Please reload this pagehttps://github.com/python/cpython/pull/11671/files
Please reload this pagehttps://github.com/python/cpython/pull/11671/files
2020-10-16-07-45-35.bpo-35823.SNQo56.rst https://github.com/python/cpython/pull/11671/files#diff-71e9319c1b19e38fa207bf1c9c2549aa05170f3abfdb5110709a2d7e4722a882
_posixsubprocess.c https://github.com/python/cpython/pull/11671/files#diff-e182817ec4633ebe50bbfa9a6d0b7c00be01307f5457abfc85f9689055be908b
configure https://github.com/python/cpython/pull/11671/files#diff-90d08e583c4c9c6f391b2ae90f819f600a6326928ea9512c9e0c6d98e9f29ac2
configure.ac https://github.com/python/cpython/pull/11671/files#diff-49473dca262eeab3b4a43002adb08b4db31020d190caaad1594b47f1d5daa810
pyconfig.h.in https://github.com/python/cpython/pull/11671/files#diff-f67052280dd3a53f0e713339632a8c10bc2e420e24a70852d83aeb50d495835f
Misc/NEWS.d/next/Library/2020-10-16-07-45-35.bpo-35823.SNQo56.rsthttps://github.com/python/cpython/pull/11671/files#diff-71e9319c1b19e38fa207bf1c9c2549aa05170f3abfdb5110709a2d7e4722a882
View file https://github.com/izbyshev/cpython/blob/f226d335d1090d7325d1c6329dec0dd2733bddbb/Misc/NEWS.d/next/Library/2020-10-16-07-45-35.bpo-35823.SNQo56.rst
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/11671/{{ revealButtonHref }}
Modules/_posixsubprocess.chttps://github.com/python/cpython/pull/11671/files#diff-e182817ec4633ebe50bbfa9a6d0b7c00be01307f5457abfc85f9689055be908b
View file https://github.com/izbyshev/cpython/blob/f226d335d1090d7325d1c6329dec0dd2733bddbb/Modules/_posixsubprocess.c
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/11671/{{ revealButtonHref }}
https://github.com/python/cpython/pull/11671/files#diff-e182817ec4633ebe50bbfa9a6d0b7c00be01307f5457abfc85f9689055be908b
Please reload this pagehttps://github.com/python/cpython/pull/11671/files
https://github.com/python/cpython/pull/11671/files#diff-e182817ec4633ebe50bbfa9a6d0b7c00be01307f5457abfc85f9689055be908b
https://github.com/python/cpython/pull/11671/files#diff-e182817ec4633ebe50bbfa9a6d0b7c00be01307f5457abfc85f9689055be908b
Please reload this pagehttps://github.com/python/cpython/pull/11671/files
https://github.com/python/cpython/pull/11671/files#diff-e182817ec4633ebe50bbfa9a6d0b7c00be01307f5457abfc85f9689055be908b
https://github.com/python/cpython/pull/11671/files#diff-e182817ec4633ebe50bbfa9a6d0b7c00be01307f5457abfc85f9689055be908b
https://github.com/python/cpython/pull/11671/files#diff-e182817ec4633ebe50bbfa9a6d0b7c00be01307f5457abfc85f9689055be908b
https://github.com/python/cpython/pull/11671/files#diff-e182817ec4633ebe50bbfa9a6d0b7c00be01307f5457abfc85f9689055be908b
https://github.com/python/cpython/pull/11671/files#diff-e182817ec4633ebe50bbfa9a6d0b7c00be01307f5457abfc85f9689055be908b
https://github.com/python/cpython/pull/11671/files#diff-e182817ec4633ebe50bbfa9a6d0b7c00be01307f5457abfc85f9689055be908b
https://github.com/python/cpython/pull/11671/files#diff-e182817ec4633ebe50bbfa9a6d0b7c00be01307f5457abfc85f9689055be908b
Please reload this pagehttps://github.com/python/cpython/pull/11671/files
Please reload this pagehttps://github.com/python/cpython/pull/11671/files
https://github.com/python/cpython/pull/11671/files#diff-e182817ec4633ebe50bbfa9a6d0b7c00be01307f5457abfc85f9689055be908b
configurehttps://github.com/python/cpython/pull/11671/files#diff-90d08e583c4c9c6f391b2ae90f819f600a6326928ea9512c9e0c6d98e9f29ac2
View file https://github.com/izbyshev/cpython/blob/f226d335d1090d7325d1c6329dec0dd2733bddbb/configure
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/11671/{{ revealButtonHref }}
https://github.com/python/cpython/pull/11671/files#diff-90d08e583c4c9c6f391b2ae90f819f600a6326928ea9512c9e0c6d98e9f29ac2
https://github.com/python/cpython/pull/11671/files#diff-90d08e583c4c9c6f391b2ae90f819f600a6326928ea9512c9e0c6d98e9f29ac2
configure.achttps://github.com/python/cpython/pull/11671/files#diff-49473dca262eeab3b4a43002adb08b4db31020d190caaad1594b47f1d5daa810
View file https://github.com/izbyshev/cpython/blob/f226d335d1090d7325d1c6329dec0dd2733bddbb/configure.ac
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/11671/{{ revealButtonHref }}
https://github.com/python/cpython/pull/11671/files#diff-49473dca262eeab3b4a43002adb08b4db31020d190caaad1594b47f1d5daa810
https://github.com/python/cpython/pull/11671/files#diff-49473dca262eeab3b4a43002adb08b4db31020d190caaad1594b47f1d5daa810
pyconfig.h.inhttps://github.com/python/cpython/pull/11671/files#diff-f67052280dd3a53f0e713339632a8c10bc2e420e24a70852d83aeb50d495835f
View file https://github.com/izbyshev/cpython/blob/f226d335d1090d7325d1c6329dec0dd2733bddbb/pyconfig.h.in
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/11671/{{ revealButtonHref }}
https://github.com/python/cpython/pull/11671/files#diff-f67052280dd3a53f0e713339632a8c10bc2e420e24a70852d83aeb50d495835f
https://github.com/python/cpython/pull/11671/files#diff-f67052280dd3a53f0e713339632a8c10bc2e420e24a70852d83aeb50d495835f
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.