Skip to content
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

Disable use of (additional) ini files for workers when restarted by XdebugHandler #5809

Closed
ghost opened this issue Oct 20, 2021 · 7 comments
Closed

Comments

@ghost
Copy link

ghost commented Oct 20, 2021

Bug report

I'am using phpstan:master.

I'a getting error messages like:

Child process error (exit code 255): Cannot load Zend OPcache - it was already loaded                       
     PHP Warning:  Module 'bcmath' already loaded in Unknown on line 0                                           
     PHP Warning:  Module 'gd' already loaded in Unknown on line 0                                               
     PHP Warning:  Module 'intl' already loaded in Unknown on line 0                                             
     PHP Warning:  Module 'pdo_mysql' already loaded in Unknown on line 0                                        
     PHP Warning:  Module 'soap' already loaded in Unknown on line 0                                             
     PHP Warning:  Module 'sodium' already loaded in Unknown on line 0                                           
     PHP Warning:  Module 'zip' already loaded in Unknown on line 0       

Relates to: composer/xdebug-handler#137

Internal worker errors when restarted by XdebugHandler and compiled with --with-config-file-scan-dir

I'am using php in a standard docker container FROM php:7.4-fpm which is compiled with --with-config-file-scan-dir

root@dev-6b74fdbbb-gwbnb:/var/www# php -i | grep scan
Configure Command => './configure' '--build=x86_64-linux-gnu' '--with-config-file-path=/usr/local/etc/php' '--with-config-file-scan-dir=/usr/local/etc/php/conf.d' '--enable-option-checking=fatal' '--with-mhash' '--with-pic' '--enable-ftp' '--enable-mbstring' '--enable-mysqlnd' '--with-password-argon2' '--with-sodium=shared' '--with-pdo-sqlite=/usr' '--with-sqlite3=/usr' '--with-curl' '--with-libedit' '--with-openssl' '--with-zlib' '--with-pear' '--with-libdir=lib/x86_64-linux-gnu' '--enable-fpm' '--with-fpm-user=www-data' '--with-fpm-group=www-data' '--disable-cgi' 'build_alias=x86_64-linux-gnu'

Phpstan is missing the -n flag on it's workers which could explain the problem because workers should not get restarted when xdebug is already disabled:

                        ❯ pstree -c -p -s -a 2540066
                        systemd,1 --system --deserialize 100
                          └─containerd-shim,1456 -namespace moby -id 152f05fe5d26121e0648223785787a4443f2c2bd85e2200aae35b0e03097d46f -address /var/run/docker/containerd/containerd.sock
                              └─docker-init,1497 -- /bin/k3s server --tls-san 0.0.0.0
                                  └─containerd-shim,6775 -namespace k8s.io -id 0644b5a9143d602b5029b202685c41c2a92caacedb13046007cfc1ba40f24953 -address /run/k3s/containerd/containerd.sock
                                      └─bash,2354967
My Command >                              └─php,2540024 phpstan-src/bin/phpstan
Restarted (by XDebugHandler) >                └─php,2540028 -n -c /tmp/W31pDl phpstan-src/bin/phpstan
Worker ShellWrapper >                            └─sh,2540046 -c '/usr/local/bin/php' -c '/tmp/W31pDl' -d memory_limit=-1 'phpstan-src/bin/phpstan' 'worker' --configuration '/var/www/phpstan.neon' --port 45829 --identifier pgfusrp9ey --
Worker >                                              └─php,2540049 -c /tmp/W31pDl -d memory_limit=-1 phpstan-src/bin/phpstan worker --configuration /var/www/phpstan.neon --port 45829 --identifier pgfusrp9ey --
Restarted (by XDebugHandler) Worker >                    └─php,2540066 -n -c /tmp/YH6yG6 phpstan-src/bin/phpstan worker --configuration /var/www/phpstan.neon --port 45829 --identifier pgfusrp9ey --

When phpstan would include the -n-Flag there also would be less processes:

❯ pstree -c -p -s -a 2605960
systemd,1 --system --deserialize 100
  └─containerd-shim,1456 -namespace moby -id 152f05fe5d26121e0648223785787a4443f2c2bd85e2200aae35b0e03097d46f -address /var/run/docker/containerd/containerd.sock
      └─docker-init,1497 -- /bin/k3s server --tls-san 0.0.0.0
          └─containerd-shim,6775 -namespace k8s.io -id 0644b5a9143d602b5029b202685c41c2a92caacedb13046007cfc1ba40f24953 -address /run/k3s/containerd/containerd.sock
              └─bash,2354967
                  └─php,2605834 phpstan-src/bin/phpstan
                      └─php,2605839 -n -c /tmp/q5Ca9v phpstan-src/bin/phpstan
                          └─sh,2605958 -c '/usr/local/bin/php' -c '/tmp/q5Ca9v' -n -d memory_limit=-1 'phpstan-src/bin/phpstan' 'worker' --configuration '/var/www/phpstan.neon' --port 40061 --identifier ylkll9i4g0 --
                              └─php,2605960 -c /tmp/q5Ca9v -n -d memory_limit=-1 phpstan-src/bin/phpstan worker --configuration /var/www/phpstan.neon --port 40061 --identifier ylkll9i4g0 --

This was achieved by adding this code to:
https://github.com/phpstan/phpstan-src/blob/2c1107588603afaa8cd3e97165b7eb1736cb4393/src/Process/ProcessHelper.php#L29

if (getenv("XDEBUG_HANDLER_SETTINGS") !== false) {
	//indicates that phpstan was previously restarted by Composer\XdebugHandler\XdebugHandler
	//that means we need to disable potential loading of ini files, like XdebugHandler does
	$phpCmd.=" -n";
}
@mergeable
Copy link

mergeable bot commented Oct 20, 2021

This bug report is missing a link to reproduction on phpstan.org.

It will most likely be closed after manual review.

@ghost
Copy link
Author

ghost commented Oct 20, 2021

Related PR: phpstan/phpstan-src#726

@johnstevenson
Copy link

This issue is a result of phpstan/phpstan-src#559 which adds code to allow the user to pass a custom ini file on the command-line so that it can be used in a worker process:

https://github.com/phpstan/phpstan-src/blob/2c1107588603afaa8cd3e97165b7eb1736cb4393/src/Process/ProcessHelper.php#L27-L28

Unfortunately it overlooked xdebug-handler, which has been configured to use its standard restart strategy (https://github.com/composer/xdebug-handler#process-configuration). This results in xdebug being loaded in the worker process - which triggers another restart.

In these situations it is much better to use the persistent settings restart:

$xdebug = new XdebugHandler();
$xdebug->setPersistent();
$xdebug->check();
unset($xdebug);

Not only will it fix this particular error, but it saves additional restarts and offers much more flexibility should you want to invoke a sub-process with xdebug from a restarted xdebug-free environment (https://github.com/composer/xdebug-handler#setpersistent).

@ghost
Copy link
Author

ghost commented Oct 21, 2021

I guess Persistent uses the PHP_INI_SCAN_DIR Environment Variable - this will also resolve the Issue.

@johnstevenson
Copy link

https://github.com/composer/xdebug-handler#persistent-settings

It's all covered in some detail in the docs.

@ondrejmirtes
Copy link
Member

Should be fixed by: phpstan/phpstan-src@222e1c3

Please verify that it works by trying out phpstan/phpstan dev-master. Thanks.

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants