René's URL Explorer Experiment


Title: Add PTY support to proc_open (again after 16 long years) by alexdowad · Pull Request #5525 · php/php-src · GitHub

Open Graph Title: Add PTY support to proc_open (again after 16 long years) by alexdowad · Pull Request #5525 · php/php-src

X Title: Add PTY support to proc_open (again after 16 long years) by alexdowad · Pull Request #5525 · php/php-src

Description: Back in 2004, a feature was added to proc_open which allowed it to open a PTY, connecting specific FDs in the child process to the slave end of the PTY and returning the master end of the PTY (wrapped as a PHP stream) in the $pipes array. However, this feature was disabled just about a month later. Little information is available about why this was done, but from talking to the original implementer, it seems there were portability problems with some rare flavors of Unix. Re-enable this feature with a simplified implementation which uses openpty(). No attempt is made to support PTYs if the platform does not have openpty(). The configure script checks if linking with -lutil is necessary to use openpty(), but if anything else is required, like including some special header or linking with some other library, PTY support will be disabled. The original PTY support for proc_open automatically daemonized the child process (disassociating it from the TTY session and process group of the parent). However, I don't think this is a good idea. Just because a user opens a child process in a PTY, it doesn't mean they want it to continue running even when the parent process is killed. Of course, if the child process is some kind of server, it will likely daemonize itself; but we have no reason to preempt that decision. It turns out that since 2015, there has been one test case for PTY support in proc_open() in the test suite. This test was added in GitHub PR #1588. That PR mentioned that the PHP binary in the Debian/Ubuntu repositories is patched to enable PTY support. Checking the Debian PHP repository (https://salsa.debian.org/php-team/php.git) shows that this is still true. Debian's patch does not modify the implementation from 2004 in any way; it just removes the #if 0 line which disables it. Naturally, the test case is skipped if PTY support is not enabled. This means that ever since it was added, every test run against the 'vanilla' PHP codebase has skipped it. Interestingly, the test case which was added in 2015 fails on my Linux Mint PC... both with this simplified implementation and when enabling the original implementation. Investigation reveals the reason: when the child process using the slave end of the PTY exits and its FDs are all closed, and all buffered data is read from the master end of the PTY, any further attempt to read from the master end fails with EIO. The test case seems to expect that reading from the master end will always return an empty string if no data is available. Could this mean that on the platform where this test case was originally developed, the PHP parent process ran faster than the child process, so that all the calls to fread() complete before the child process exits and closes its FDs? Or could something else about the platform have been different, such that read() in the parent does not fail with EIO even after the child process exits? Another question: Are the Debian packagers even running this test case every time they build PHP binaries? Intriguing questions, these! There may be another way out of this sticky dilemma: IF at least one FD referring to the slave end of the PTY is kept open in the parent process, the failure with EIO will not occur even after the child process exits. This might be a good approach, though we would need a way to ensure the FD will be closed eventually in long-running programs. Perhaps associate it with the proc resource and close it when the proc is freed or explicitly closed with proc_close? And the rabbit hole goes deeper. Although (for now) the test case from 2015 has been updated so it does not attempt to fread() multiple times from the PTY, it still fails intermittently. The reason? When the child process writes "foo\n" to the PTY, the parent sometimes receives "foo" (3 bytes) and sometimes "foo\r\n" (5 bytes). The "\r" is obviously from the TTY line discipline converting "\n" to "\r\n", but why on earth would the parent sometimes read the newline and sometimes not? What is more, strace clearly shows that this is happening in the kernel. The child process always executes a single write("foo\n") syscall, but when the blocking read() syscall issued by the parent process returns, sometimes it returns "foo" and sometimes "foo\r\n". This doesn't happen when I extract the test code and run it directly from the PHP CLI, only when the test case is run by run-tests.php. Thanks to Nikita Popov for suggesting that we should just use openpty() rather than grantpt(), unlockpt(), etc.

Open Graph Description: Back in 2004, a feature was added to proc_open which allowed it to open a PTY, connecting specific FDs in the child process to the slave end of the PTY and returning the master end of the PTY (wrap...

X Description: Back in 2004, a feature was added to proc_open which allowed it to open a PTY, connecting specific FDs in the child process to the slave end of the PTY and returning the master end of the PTY (wrap...

Opengraph URL: https://github.com/php/php-src/pull/5525

X: @github

direct link

Domain: github.com

IPv4 address: 140.82.114.4

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:51ba40de-c112-28a1-9a2c-49783311b8b5
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-idA3AC:7F641:1AB8B52:2335EBD:6B055F1A
html-safe-nonce595e74418b6b62f07f6debfb40182a6f1f7f971c03b8d54597dcfaceaf71867e
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBM0FDOjdGNjQxOjFBQjhCNTI6MjMzNUVCRDo2QjA1NUYxQSIsInZpc2l0b3JfaWQiOiIzMTk0NjMyMjg2MTM0MTY1Mjc0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmac5754551c930738a66690f3a0a92653362af270a545a8581504591c1f26c1e66d
hovercard-subject-tagpull_request:413000366
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/php/php-src/pull/5525/files
twitter:imagehttps://avatars.githubusercontent.com/u/1067359?s=400&v=4
twitter:cardsummary_large_image
og:imagehttps://avatars.githubusercontent.com/u/1067359?s=400&v=4
og:image:altBack in 2004, a feature was added to proc_open which allowed it to open a PTY, connecting specific FDs in the child process to the slave end of the PTY and returning the master end of the PTY (wrap...
og:site_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
None8a6760da3cce5e2d079d07e3b62df71194316c4478db1a5e38050f44fce72c72
turbo-cache-controlno-preview
diff-viewunified
go-importgithub.com/php/php-src git https://github.com/php/php-src.git
octolytics-dimension-user_id25158
octolytics-dimension-user_loginphp
octolytics-dimension-repository_id1903522
octolytics-dimension-repository_nwophp/php-src
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id1903522
octolytics-dimension-repository_network_root_nwophp/php-src
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
release8327739ac0648b9c3c722b5f401f7338553936b8
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/php/php-src/pull/5525/files#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fphp%2Fphp-src%2Fpull%2F5525%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%2Fphp%2Fphp-src%2Fpull%2F5525%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=php%2Fphp-src
Reloadhttps://github.com/php/php-src/pull/5525/files
Reloadhttps://github.com/php/php-src/pull/5525/files
Reloadhttps://github.com/php/php-src/pull/5525/files
phphttps://github.com/php
php-srchttps://github.com/php/php-src
Codehttps://github.com/php/php-src
Issues1.1k (1.1k)https://github.com/php/php-src/issues
Pull requests1.3k (1.3k)https://github.com/php/php-src/pulls
Actionshttps://github.com/php/php-src/actions
Security and quality83 (83)https://github.com/php/php-src/security
Insightshttps://github.com/php/php-src/insights
Sign up for GitHub https://github.com/signup?return_to=%2Fphp%2Fphp-src%2Fissues%2Fnew%2Fchoose
terms of servicehttps://docs.github.com/terms
privacy statementhttps://docs.github.com/privacy
Sign inhttps://github.com/login?return_to=%2Fphp%2Fphp-src%2Fissues%2Fnew%2Fchoose
alexdowadhttps://github.com/alexdowad
php:masterhttps://github.com/php/php-src/tree/master
alexdowad:enable-pty-in-proc-openhttps://github.com/alexdowad/php-src/tree/enable-pty-in-proc-open
Conversation 51 https://github.com/php/php-src/pull/5525
Commits 4 https://github.com/php/php-src/pull/5525/commits
Checks 0 https://github.com/php/php-src/pull/5525/checks
Files changed https://github.com/php/php-src/pull/5525/files
Please reload this pagehttps://github.com/php/php-src/pull/5525/files
Add PTY support to proc_open (again after 16 long years) https://github.com/php/php-src/pull/5525/files#top
Show all changes 4 commits https://github.com/php/php-src/pull/5525/files
7ba9e96 Add PTY support to proc_open (again after 16 long years) alexdowad May 3, 2020 https://github.com/php/php-src/pull/5525/commits/7ba9e9653d55a7aea263aafebcf35486fb1b7d57
d29d72b Don't leak memory if wrong resource type is passed to proc_open alexdowad May 9, 2020 https://github.com/php/php-src/pull/5525/commits/d29d72b382edd10417c27cb80f9d9f28d4a0f4ff
ba0bbaf Adjust struct names in proc_open.h (makes proc_open.c more terse) alexdowad May 12, 2020 https://github.com/php/php-src/pull/5525/commits/ba0bbaf0957e60a93f7d48abcd931c40ba9f36ef
507cac0 Further refactoring of proc_open.c alexdowad May 12, 2020 https://github.com/php/php-src/pull/5525/commits/507cac01563ced1e4a81a8a0b9c0b45770efa7ee
Clear filters https://github.com/php/php-src/pull/5525/files
Please reload this pagehttps://github.com/php/php-src/pull/5525/files
Please reload this pagehttps://github.com/php/php-src/pull/5525/files
configure.ac https://github.com/php/php-src/pull/5525/files#diff-49473dca262eeab3b4a43002adb08b4db31020d190caaad1594b47f1d5daa810
proc_open.c https://github.com/php/php-src/pull/5525/files#diff-0fb2e3ebee149b26f75a83db7b0f8194009afccdb54b8cf51a7986031af64c87
proc_open.h https://github.com/php/php-src/pull/5525/files#diff-5f44baaae9b4c2ebbc5c3f33f71451cbd6e110396b5f95334f34a2cd089154c9
bug69442.phpt https://github.com/php/php-src/pull/5525/files#diff-799b45e647d1de977f094181dafd91616fa6c84a3b7e58e5857388e007d48567
proc_open_with_wrong_resource_type.phpt https://github.com/php/php-src/pull/5525/files#diff-ae833af971a9e06b16bb366a86277f98fd160ad7758a56c13b1cda72aeef726a
proc_open_pipes3.phpt https://github.com/php/php-src/pull/5525/files#diff-10d21d08f85ddddf9d7027825d810fff51c565db461d5638f21eda0b2e803b39
configure.achttps://github.com/php/php-src/pull/5525/files#diff-49473dca262eeab3b4a43002adb08b4db31020d190caaad1594b47f1d5daa810
View file https://github.com/alexdowad/php-src/blob/507cac01563ced1e4a81a8a0b9c0b45770efa7ee/configure.ac
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/php/php-src/pull/5525/{{ revealButtonHref }}
https://github.com/php/php-src/pull/5525/files#diff-49473dca262eeab3b4a43002adb08b4db31020d190caaad1594b47f1d5daa810
https://github.com/php/php-src/pull/5525/files#diff-49473dca262eeab3b4a43002adb08b4db31020d190caaad1594b47f1d5daa810
https://github.com/php/php-src/pull/5525/files#diff-49473dca262eeab3b4a43002adb08b4db31020d190caaad1594b47f1d5daa810
https://github.com/php/php-src/pull/5525/files#diff-49473dca262eeab3b4a43002adb08b4db31020d190caaad1594b47f1d5daa810
https://github.com/php/php-src/pull/5525/files#diff-49473dca262eeab3b4a43002adb08b4db31020d190caaad1594b47f1d5daa810
https://github.com/php/php-src/pull/5525/files#diff-49473dca262eeab3b4a43002adb08b4db31020d190caaad1594b47f1d5daa810
https://github.com/php/php-src/pull/5525/files#diff-49473dca262eeab3b4a43002adb08b4db31020d190caaad1594b47f1d5daa810
https://github.com/php/php-src/pull/5525/files#diff-49473dca262eeab3b4a43002adb08b4db31020d190caaad1594b47f1d5daa810
Please reload this pagehttps://github.com/php/php-src/pull/5525/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

Response headers:
Servergithub.com
Content-Typetext/html; charset=utf-8
Cache-Controlno-cache

Viewport: width=device-width


URLs of crawlers that visited me.