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

PHP Fatal error: Allowed memory size of XXXXX bytes exhausted #8543

Closed
eusonlito opened this issue Dec 16, 2022 · 15 comments
Closed

PHP Fatal error: Allowed memory size of XXXXX bytes exhausted #8543

eusonlito opened this issue Dec 16, 2022 · 15 comments

Comments

@eusonlito
Copy link

eusonlito commented Dec 16, 2022

Bug report

php ./vendor/bin/phpstan --version

PHPStan - PHP Static Analysis Tool 1.9.3

php -v

PHP 8.1.13 (cli) (built: Nov 26 2022 14:07:55) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.13, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.13, Copyright (c), by Zend Technologies

When parsing some PHP files it stuck with a recursive load and memory is drained:

php ./vendor/bin/phpstan analyse app --memory-limit=1G 
Note: Using configuration file /home/lito/www/phpstan-memory-exhausted/phpstan.neon.
 6/6 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

 -- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
     Error                                                                                                                                                                                                            
 -- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
     Child process error (exit code 255): PHP Fatal error:  Allowed memory size of 1073741824 bytes exhausted (tried to allocate 262144 bytes) in                                                                     
     phar:///home/lito/www/phpstan-memory-exhausted/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php on line 559                                                                                
                                                                                                                                                                                                                      
     PHPStan process crashed because it reached configured PHP memory limit: 1G                                                                                                                                       
     Increase your memory limit in php.ini or run PHPStan with --memory-limit CLI option.                                                                                                                             
     PHP Fatal error:  Allowed memory size of 1073741824 bytes exhausted (tried to allocate 2621440 bytes) in /home/lito/www/phpstan-memory-exhausted/vendor/symfony/console/Output/AnsiColorMode.php on line 73  
                                                                                                                                                                                                                      
 -- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 

Code snippet that reproduces the problem

git clone https://github.com/eusonlito/phpstan-memory-exhausted.git

cd phpstan-memory-exhausted

composer install

php ./vendor/bin/phpstan analyse app --memory-limit=1G
  • Deleting methods payloadRow, payloadRowIsSame and payloadRowOther at app/Domains/Log/Model/Traits/Payload.php it works
  • Deleting database migration table log at database/migrations/2021_01_14_000000_base.php it works

Expected output

Finish without error

@mergeable mergeable bot closed this as completed Dec 16, 2022
@mergeable
Copy link

mergeable bot commented Dec 16, 2022

Hi there! 👋

Looks like you opened an issue without following one of the issue templates:

Bug report 🐛 (open an issue)

If something isn't working as expected 🤔.

Feature request 🚀 (open an issue)

I have a suggestion (and may want to implement it 🙂)!

Support question ❓ (open a discussion)

I need some help with my code because PHPStan doesn't like it.


The current issue will be closed. This is a precaution to save maintainer's time, I hope you'll understand.

Sincerely, the bot 🤖

@canvural
Copy link
Contributor

@eusonlito It looks like this is related to Larastan rather than PHPStan. It works fine if I don't include Larastan's config.

So please open an issue there.

@eusonlito
Copy link
Author

Hello @canvural, why looks like related with Larastan? Because the .neon included?

@canvural
Copy link
Contributor

Because like I wrote: It works fine if I don't include Larastan's config.

@eusonlito
Copy link
Author

😅😅

Ok!

@canvural
Copy link
Contributor

canvural commented Dec 16, 2022

Ok, now I'm not so sure about it 😄 I played with it little bit. And it seems to be caused by passing an arrow function to $this->related->first() call. But there is no special code around first (method in Collection class) in Larastan 🤷🏽

@ondrejmirtes ondrejmirtes reopened this Dec 16, 2022
@ondrejmirtes
Copy link
Member

This is the snippet of the stack trace of the infinite recursion:

#14 phar:///Users/ondrej/Downloads/phpstan-memory-exhausted/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(3370): PHPStan\Reflection\ParametersAcceptorSelector::selectFromArgs(Object(PHPStan\Analyser\MutatingScope), Array, Array)
#15 phar:///Users/ondrej/Downloads/phpstan-memory-exhausted/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(1403): PHPStan\Analyser\MutatingScope->methodCallReturnType(Object(PHPStan\Type\Generic\GenericObjectType), 'first', Object(PhpParser\Node\Expr\MethodCall))
#16 phar:///Users/ondrej/Downloads/phpstan-memory-exhausted/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(1409): PHPStan\Analyser\MutatingScope->PHPStan\Analyser\{closure}()
#17 phar:///Users/ondrej/Downloads/phpstan-memory-exhausted/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(559): PHPStan\Analyser\MutatingScope->resolveType('$this->related-...', Object(PhpParser\Node\Expr\MethodCall))
#18 phar:///Users/ondrej/Downloads/phpstan-memory-exhausted/vendor/phpstan/phpstan/phpstan.phar/src/Rules/Properties/PropertyReflectionFinder.php(73): PHPStan\Analyser\MutatingScope->getType(Object(PhpParser\Node\Expr\MethodCall))
#19 phar:///Users/ondrej/Downloads/phpstan-memory-exhausted/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(2484): PHPStan\Rules\Properties\PropertyReflectionFinder->findPropertyReflectionFromNode(Object(PhpParser\Node\Expr\PropertyFetch), Object(PHPStan\Analyser\MutatingScope))
#20 phar:///Users/ondrej/Downloads/phpstan-memory-exhausted/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(2446): PHPStan\Analyser\MutatingScope->shouldInvalidateExpression('$value', Object(PhpParser\Node\Expr\Variable), Object(PhpParser\Node\Expr\PropertyFetch), true)
#21 phar:///Users/ondrej/Downloads/phpstan-memory-exhausted/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(2434): PHPStan\Analyser\MutatingScope->invalidateExpression(Object(PhpParser\Node\Expr\Variable), true)
#22 phar:///Users/ondrej/Downloads/phpstan-memory-exhausted/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(2359): PHPStan\Analyser\MutatingScope->assignExpression(Object(PhpParser\Node\Expr\Variable), Object(PHPStan\Type\MixedType), Object(PHPStan\Type\MixedType))
#23 phar:///Users/ondrej/Downloads/phpstan-memory-exhausted/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(2218): PHPStan\Analyser\MutatingScope->assignVariable('value', Object(PHPStan\Type\MixedType), Object(PHPStan\Type\MixedType))
#24 phar:///Users/ondrej/Downloads/phpstan-memory-exhausted/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(1045): PHPStan\Analyser\MutatingScope->enterArrowFunctionWithoutReflection(Object(PhpParser\Node\Expr\ArrowFunction), NULL)
#25 phar:///Users/ondrej/Downloads/phpstan-memory-exhausted/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(559): PHPStan\Analyser\MutatingScope->resolveType('fn($value) => $...', Object(PhpParser\Node\Expr\ArrowFunction))
#26 phar:///Users/ondrej/Downloads/phpstan-memory-exhausted/vendor/phpstan/phpstan/phpstan.phar/src/Reflection/ParametersAcceptorSelector.php(128): PHPStan\Analyser\MutatingScope->getType(Object(PhpParser\Node\Expr\ArrowFunction))
#27 phar:///Users/ondrej/Downloads/phpstan-memory-exhausted/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(3370): PHPStan\Reflection\ParametersAcceptorSelector::selectFromArgs(Object(PHPStan\Analyser\MutatingScope), Array, Array)

@rajyan
Copy link
Contributor

rajyan commented Dec 17, 2022

I believe this is related to the conditional expression changes
phpstan/phpstan-src#2011
I think I can look into it.

@ondrejmirtes
Copy link
Member

I think we can fix this problem AND this bug at the same time: https://phpstan.org/r/19a13153-2263-405a-be78-55539493a439

The endless recursion comes from the fact that when we're calling assignVariable when entering an arrow function, MutatingScope asks shouldInvalidateExpression, because it wants to retain readonly properties.

But it doesn't make sense to retain readonly properties when we're reassigning the whole object, so PHPStan shouldn't ask in that case -> no more endless recursion, and the assertType failure from my example will no longer happen either.

@eusonlito
Copy link
Author

I only comment that this project is amazing and a basic pillar to keep the code quality of any project 😊

@ondrejmirtes
Copy link
Member

@eusonlito Thank you for your kind words! :)

Fixed the problem: phpstan/phpstan-src@a7fed03

@rajyan I had to modify some conditional types tests, the results are a bit worse now, but I think the code makes more sense - if we're assigning a variable, we want to invalidate everything.

@rajyan
Copy link
Contributor

rajyan commented Dec 17, 2022

Thank you for the fix.

if we're assigning a variable, we want to invalidate everything.

Yeah, I agree with you. I left the original code, but had the same feeling.
Not sure yet why the result got worse. Looks like the conditional type is created before the assignment?

@ondrejmirtes
Copy link
Member

I have no idea why this changed :)

@martio
Copy link

martio commented Dec 25, 2022

@eusonlito Thank you for your kind words! :)

Fixed the problem: phpstan/phpstan-src@a7fed03

@rajyan I had to modify some conditional types tests, the results are a bit worse now, but I think the code makes more sense - if we're assigning a variable, we want to invalidate everything.

@ondrejmirtes It doesn't work for me 🙁 I get an error with a large file... I didn't have this problem before 😳 See: 8593 👈

@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 26, 2023
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

5 participants