New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
php: fix for macOS fork error #137909
php: fix for macOS fork error #137909
Conversation
Signed-off-by: Rui Chen <rui@chenrui.dev>
0f300cd
to
94fa901
Compare
So what is the actual bug and had this been reported to upstream ? |
IssueThe latest macOS update has begun terminating certain fork() processes under specific circumstances. This is evidenced by the following screenshots and error logs In the Nginx Error Log, we find multiple instances of "upstream prematurely closed connection" errors. The PHP Error Log indicates warnings about "__NSPlaceholderDate initialize" possibly running in another thread when fork() was invoked, and subsequently, child processes exiting prematurely.
[error] 1957#0: *2 upstream prematurely closed connection
[error] 50639#0: *77 upstream prematurely closed connection
[error] 28667#0: *16 upstream prematurely closed connection
[27-Jul-2023 09:49:38] NOTICE: ready to handle connections
[27-Jul-2023 09:49:48] WARNING: [pool valet] child 54119 said into stderr: "objc[54119]: +[__NSPlaceholderDate initialize] may have been in progress in another thread when fork() was called."
[27-Jul-2023 09:49:48] WARNING: [pool valet] child 54119 said into stderr: "objc[54119]: +[__NSPlaceholderDate initialize] may have been in progress in another thread when fork() was called. We cannot safely call it or ignore it in the fork() child process. Crashing instead. Set a breakpoint on objc_initializeAfterForkError to debug."
[27-Jul-2023 09:49:48] WARNING: [pool valet] child 54119 exited on signal 6 (SIGABRT) after 9.676681 seconds from start
[27-Jul-2023 09:49:48] NOTICE: [pool valet] child 54693 started
[27-Jul-2023 09:50:29] WARNING: [pool valet] child 54120 said into stderr: "objc[54120]: +[__NSPlaceholderDate initialize] may have been in progress in another thread when fork() was called."
[27-Jul-2023 09:50:29] WARNING: [pool valet] child 54120 said into stderr: "objc[54120]: +[__NSPlaceholderDate initialize] may have been in progress in another thread when fork() was called. We cannot safely call it or ignore it in the fork() child process. Crashing instead. Set a breakpoint on objc_initializeAfterForkError to debug."
[27-Jul-2023 09:50:29] WARNING: [pool valet] child 54120 exited on signal 6 (SIGABRT) after 50.322856 seconds from start
[27-Jul-2023 09:50:29] NOTICE: [pool valet] child 54703 started How to reproduceFollow this step: laravel/valet#1433 (comment) Proposed Solution (This PR):Rails users faced a similar issue a few years ago (refer to rails/rails#38560) and resolved it by introducing the environment variable
However, Laravel Valet differs as it uses PHP, which is launched via LaunchDaemon. This leaves no room for adding the environment variable except in the The aim of this PR is to add the |
And the PHP team should be made aware of that and investigate a fix. Without a bug report upstream I'm very hesitant to merge this. |
I agree with you and see your point. My bad, it took some time to report this to PHP upstream. I submitted the issue. It might take some time for them to fix this. Meanwhile, hopefully this PR gets merged and bringing back better life experience for those who are using macOS 🙏😉 |
I am glad to let you know that I have fulfilled all your requests 😊. This PR is targeted specifically to assist PHP developers on macOS who may not initially comprehend the cause of these unexpected errors. The proposed solution consists of a single line of code: ( This is a simple workaround that can be easily reverted (by removing it) if necessary. It's worth noting that ideally, the PHP team can address this issue (php/php-src#11818) in the future. However, given the uncertainty regarding the timeframe of such a resolution, this workaround provides an immediate fix. Additionally, considering the recent changes to macOS security behavior—such as the disabling of system preference changes through LaunchDaemons (which caused an issue when the max file descriptors on my macOS suddenly dropped to 256 following the 13.5 update)—it's possible we might need to prepare additional workarounds should such changes continue to occur. I sincerely hope this PR can be merged promptly to assist developers currently grappling with this issue. Your feedback and any further suggestions are highly valued and appreciated. |
So one thing I'm worried about is that it only fixes the situation for people who use |
I'm not sure Could you give me your "If you use the CLI it's still broken." comment's example? Maybe I can help. |
If you trigger the same codepath using |
Can you avoid this issue by just running it like this? export OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES
php -S 0.0.0.0:8080 ./index.php |
Not really convinced that this is a safe workaround: https://blog.phusion.nl/2017/10/13/why-ruby-app-servers-break-on-macos-high-sierra-and-what-can-be-done-about-it/ |
grpc/grpc#33281 has relevant issues too. |
I think I've done a pretty good job troubleshooting the issue here (#137431) And It seems that the upstream PHP team won't be able to resolve this issue soon (php/php-src#11818 (comment)) Would you consider merging this PR in the meantime? We can always easily revert these environment settings after it gets fixed. Please make the life of PHP developers on macOS easier. |
I've worked with PHP upstream team, and tried thier solution php/php-src#11818 (comment) but did not work out. As @devnexen mentioned in the comment:
Could you now merge this PR for the time being? |
As I mention at php/php-src#11818 (comment), I don't think merging this is a good idea. I'll quote the important parts from the article I shared again here:
Enabling this disables safety features implemented by Apple, and that's not something we're going to foist on unsuspecting users. As mentioned in php/php-src#11818 (comment),
Thus, closing this. If you're able to find a different, safer, workaround, feel free to open a different pull request for that. |
We're crashing again (in macOS) since Homebrew/homebrew-core#132976 This has occured before, from https://bugs.ruby-lang.org/issues/16239 > This is a fatal interaction between the PostgreSQL 12 client libraries > and the GSS implementation provided by macOS. This is being tracked in > the pg gem at ged/ruby-pg#311 More in ged/ruby-pg#311 (comment) Docs on PGGSSENCMODE https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-GSSENCMODE OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES has been a workaround in the past (puma/puma#1421) but it shouldn't be used without care: Homebrew/homebrew-core#137909 (comment)
brew install --build-from-source <formula>
, where<formula>
is the name of the formula you're submitting?brew test <formula>
, where<formula>
is the name of the formula you're submitting?brew audit --strict <formula>
(after doingbrew install --build-from-source <formula>
)? If this is a new formula, does it passbrew audit --new <formula>
?This PR is related to this Laravel Valet issue: laravel/valet#1433 and it provides fix by adding following to the php plist.
So far, php 8.2.8 has problems connecting to PostgreSQL and Mongodb using Laravel. I'm sure there are another hidden problems with this version. Adding this env value will prevent some problems from happening.
Update
Apparently my solution also works for people having problem with PHP
gettext
#137431And It seems that the upstream PHP team won't be able to resolve this issue soon (php/php-src#11818 (comment)). Because this is more of a system-level issue than a PHP issue.
Until it somehow ideally gets fixed by php upstream, Homebrew adding this environment variable to its php startup script is really providing nice iced water for people in the living hell. I mean, it's usually Mac people using Homebrew. Linux has their own distoro's package manager. Using homebrew for linux is unlikely. Homebrew providing Mac only work-around to its package does sound like it should be.