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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make CPU cores count more tolerant towards system command errors #1733

Merged

Conversation

romm
Copy link
Contributor

@romm romm commented Oct 6, 2022

Running Infection with the new --threads=max option was leading to an error on my local MacOS.

This fix is quite opinionated, if you see a better way of doing this I'd be happy to follow your recommendations. 馃槈

This is the error that was returned:

$ ./vendor/bin/infection --threads=max -vvv

In ExecException.php line 9:

  [Safe\Exceptions\ExecException]
  An error occured

Exception trace:
  at /some/path/to/app/vendor/thecodingmachine/safe/generated/Exceptions/ExecException.php:9
 Safe\Exceptions\ExecException::createFromPhpError() at /some/path/to/app/vendor/thecodingmachine/safe/generated/exec.php:119
 Safe\shell_exec() at /some/path/to/app/vendor/infection/infection/src/Resource/Processor/CpuCoresCountProvider.php:70
 Infection\Resource\Processor\CpuCoresCountProvider::provide() at /some/path/to/app/vendor/infection/infection/src/Command/RunCommand.php:658
 Infection\Command\RunCommand->getThreadCount() at /some/path/to/app/vendor/infection/infection/src/Command/RunCommand.php:485
 Infection\Command\RunCommand->createContainer() at /some/path/to/app/vendor/infection/infection/src/Command/RunCommand.php:356
 Infection\Command\RunCommand->executeCommand() at /some/path/to/app/vendor/infection/infection/src/Command/BaseCommand.php:75
 Infection\Command\BaseCommand->execute() at /some/path/to/app/vendor/symfony/console/Command/Command.php:308
 Symfony\Component\Console\Command\Command->run() at /some/path/to/app/vendor/symfony/console/Application.php:1002
 Symfony\Component\Console\Application->doRunCommand() at /some/path/to/app/vendor/symfony/console/Application.php:299
 Symfony\Component\Console\Application->doRun() at /some/path/to/app/vendor/symfony/console/Application.php:171
 Symfony\Component\Console\Application->run() at /some/path/to/app/vendor/infection/infection/bin/infection:83
 include() at /some/path/to/app/vendor/bin/infection:120

@romm romm force-pushed the fix/cpu-core-count-error-tolerance branch from 484ace2 to 2be8c78 Compare October 6, 2022 14:53
@maks-rafalko
Copy link
Member

Could you please past an error here?

  • to better understand what's going on on MacOS
  • to make it searchable for future developers

@romm
Copy link
Contributor Author

romm commented Oct 7, 2022

Sure, I just edited the description above; unfortunately the message is not really relevant. 馃槙

$hasNproc = trim(@shell_exec('command -v nproc'));
try {
// for Linux
$hasNproc = trim(@shell_exec('command -v nproc'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one issue I see with this try/catch is that it's unclear which statement you really expect to throw

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well the idea is mostly not to block i.e. Mac/Windows users when the Linux command does fail, to allow the next part of the function to run without trouble.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was more referring at the statement causing failure, here I doubt that anything apart from the shell exec is suppose to throw right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. Actually if any of the two shell_exec calls fails, it should be caught IMO, no matter which one. WDYT?

if (is_int($cpuCount)) {
return $cpuCount;
}
} catch (ExecException) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That being said I'd personally find unexpected that you ask several threads and end up with only one... but that's out of the scope of this PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, maybe we could add a notice/warning if all commands failed? WDYT?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

馃憤 (would do so in a different PR though)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When/if this PR is merged I'll submit a new one where we can discuss this. 馃槈

@sanmai
Copy link
Member

sanmai commented Oct 8, 2022

In addition to what @theofidry said please consider using the unsafe variant of shell_exec. We may not need to handle the exceptions in the first place.

@romm
Copy link
Contributor Author

romm commented Oct 9, 2022

In addition to what @theofidry said please consider using the unsafe variant of shell_exec. We may not need to handle the exceptions in the first place.

Sure, if others agree I see no problem to switch to the native shell_exec function.

@romm
Copy link
Contributor Author

romm commented Oct 10, 2022

@sanmai @maks-rafalko After giving it some thought, the safe version of shell_exec is easier to work with in this case: working with try/catch is more readable (IMO) than three conditions (because three shell_exec calls) checking if the result is a correct string. I stay open to changing it if you think that's better, though. :)

@maks-rafalko
Copy link
Member

Ok, let's get it merged, seems like we all ok with either safe of unsafe versions. Other comments are not related to this fix.

Thank you @romm

@maks-rafalko maks-rafalko merged commit fbd8c44 into infection:master Oct 15, 2022
@romm romm deleted the fix/cpu-core-count-error-tolerance branch October 17, 2022 14:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants