Skip to content

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

@ghost

Description

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";
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions