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 ini files when restarted by XdebugHandler #726

Closed
wants to merge 2 commits into from
Closed

Disable ini files when restarted by XdebugHandler #726

wants to merge 2 commits into from

Conversation

ghost
Copy link

@ghost ghost commented Oct 20, 2021

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

This relates to: phpstan/phpstan#5809

Disable use of (additional) ini files for workers when restarted by XdebugHandler
use better check for existing environment variables
@staabm
Copy link
Contributor

staabm commented Oct 23, 2021

Does composer itself work for you with your setup (composer uses the same xdebug-handler internally)? I am wondering whether there is a issue with your setup, or with the xdebug handler, or the phpstan integration

@ghost
Copy link
Author

ghost commented Oct 23, 2021

Does composer itself work for you with your setup (composer uses the same xdebug-handler internally)? I am wondering whether there is a issue with your setup, or with the xdebug handler, or the phpstan integration

Composer works.

Have a look at this post: composer/xdebug-handler#137 (comment)

@johnstevenson
Copy link
Contributor

This is a summary of my post on the linked issue:

This error is a result of #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.

$phpIni = php_ini_loaded_file();
$phpCmd = $phpIni === false ? escapeshellarg(PHP_BINARY) : sprintf('%s -c %s', escapeshellarg(PHP_BINARY), escapeshellarg($phpIni));

Unfortunately it overlooked xdebug-handler, which in this case had already performed a restart using a tmp ini file created from the loaded and additional ini files. Because the worker process is called without preventing additional ini files scanning, php loads the extensions from the main ini file (actually xdebug-handler's tmp ini) , then tries to reload them from the additional inis.

And because xdebug is loaded in the worker process, xdebug-handler performs another restart.

xdebug-handler has been designed to handle these situations with its persistent settings strategy: https://github.com/composer/xdebug-handler#persistent-settings

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

This creates a flexible xdebug-free environment for all child processes and in my view is the best solution.

@ondrejmirtes
Copy link
Member

@johnstevenson So instead of this PR, the only thing we need to add is $xdebug->setPersistent();? Thank you.

@johnstevenson
Copy link
Contributor

@ondrejmirtes Yes, correct

@ondrejmirtes
Copy link
Member

Alright, just did that: 222e1c3

Can you please verify it works as expected? Thanks.

@johnstevenson
Copy link
Contributor

Okay, you can probably get rid of the extra code that was added in ProcessHelper.php. I'll reproduce the various scenarios and check them just to confirm.

@ondrejmirtes
Copy link
Member

What extra code?

@ondrejmirtes
Copy link
Member

Also please wait a bit; the new commit hasn't yet built to phpstan/phpstan. The CI pipeline is full today.

@johnstevenson
Copy link
Contributor

The extra code introduced in #559

Also please wait a bit; the new commit hasn't yet built to phpstan/phpstan.

I tend to specifically replicate these scenarious in my own testcase classes first. One day I'll put them on Github, when I've worked out a simple way to automate them!

@ondrejmirtes
Copy link
Member

It's now in phpstan/phpstan dev-master, so you can test it :)

@johnstevenson
Copy link
Contributor

@ondrejmirtes Great, this works nicely and keeps Xdebug out of all subprocesses.

you can probably get rid of the extra code that was added in ProcessHelper.php

I was wrong about this. Explicitly setting the loaded ini on the comand-line has no downsides when XdebugHandler has restarted the process, because everything is in a single ini file (which is made available to PHP through through PHPRC environment variable). However for the sake of clarity it should probably be amended to something like:

    $phpCmd = escapeshellarg(PHP_BINARY);

    if (XdebugHandler::getRestartSettings() === null && php_ini_loaded_file() !== false) {
        // Explicitly set ini file in case it was passed in on the command line
        $phpCmd .= ' -c ' .escapeshellarg(php_ini_loaded_file());             
    }
    
    $processCommandArray = [
      $phpCmd,
    ];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants