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

Memory size exhausted for all memory limit #6911

Closed
Ciloe opened this issue Mar 25, 2022 · 13 comments · Fixed by phpstan/phpstan-src#1137
Closed

Memory size exhausted for all memory limit #6911

Ciloe opened this issue Mar 25, 2022 · 13 comments · Fixed by phpstan/phpstan-src#1137
Labels

Comments

@Ciloe
Copy link

Ciloe commented Mar 25, 2022

Bug report

Hi there, I'm currently using the version 1.5 to PHPStan (I upgrade from 0.12.88). I tested to use memory size at 1G, 2G, 3G, 4G and 8G. There is an infinite loop ? I can't show you the code and the project is a big project

{
    "totals": {
        "errors": 4,
        "file_errors": 0
    },
    "files": [],
    "errors": [
        "Child process error (exit code 255): PHP Fatal error:  Allowed memory size of 8589934592 bytes exhausted (tried to allocate 262144 bytes) in phar:///WORKFOLDER/back/app/vendor/phpstan/phpstan/phpstan.phar/src/Reflection/ReflectionProvider/MemoizingReflectionProvider.php on line 38\nFatal error: Allowed memory size of 8589934592 bytes exhausted (tried to allocate 262144 bytes) in phar:///WORKFOLDER/back/app/vendor/phpstan/phpstan/phpstan.phar/src/Reflection/ReflectionProvider/MemoizingReflectionProvider.php on line 38\n\nPHPStan process crashed because it reached configured PHP memory limit: 8G\nIncrease your memory limit in php.ini or run PHPStan with --memory-limit CLI option.\n\nIn MemoizingReflectionProvider.php line 38:\n                                                                                             \n  Error: Allowed memory size of 8589934592 bytes exhausted (tried to allocate 262144 bytes)  \n                                                                                             \n\nworker [-c|--configuration CONFIGURATION] [-l|--level LEVEL] [-a|--autoload-file AUTOLOAD-FILE] [--memory-limit MEMORY-LIMIT] [--xdebug] [--port PORT] [--identifier IDENTIFIER] [--tmp-file TMP-FILE] [--instead-of INSTEAD-OF] [--] [<paths>...]\n\n",
        "Child process error (exit code 255): PHP Fatal error:  Allowed memory size of 8589934592 bytes exhausted (tried to allocate 262144 bytes) in phar:///WORKFOLDER/back/app/vendor/phpstan/phpstan/phpstan.phar/src/PhpDoc/TypeNodeResolver.php on line 118\nFatal error: Allowed memory size of 8589934592 bytes exhausted (tried to allocate 262144 bytes) in phar:///WORKFOLDER/back/app/vendor/phpstan/phpstan/phpstan.phar/src/PhpDoc/TypeNodeResolver.php on line 118\nPHP Fatal error:  Allowed memory size of 8589934592 bytes exhausted (tried to allocate 262144 bytes) in phar:///WORKFOLDER/back/app/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/console/Output/Output.php on line 118\nFatal error: Allowed memory size of 8589934592 bytes exhausted (tried to allocate 262144 bytes) in phar:///WORKFOLDER/back/app/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/console/Output/Output.php on line 118\n",
        "Child process error (exit code 255): PHP Fatal error:  Allowed memory size of 8589934592 bytes exhausted (tried to allocate 20480 bytes) in phar:///WORKFOLDER/back/app/vendor/phpstan/phpstan/phpstan.phar/vendor/phpstan/phpdoc-parser/src/Parser/PhpDocParser.php on line 27\nFatal error: Allowed memory size of 8589934592 bytes exhausted (tried to allocate 20480 bytes) in phar:///WORKFOLDER/back/app/vendor/phpstan/phpstan/phpstan.phar/vendor/phpstan/phpdoc-parser/src/Parser/PhpDocParser.php on line 27\n\nPHPStan process crashed because it reached configured PHP memory limit: 8G\nIncrease your memory limit in php.ini or run PHPStan with --memory-limit CLI option.\n\nPHP Fatal error:  Allowed memory size of 8589934592 bytes exhausted (tried to allocate 65536 bytes) in phar:///WORKFOLDER/back/app/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/string/AbstractString.php on line 445\nFatal error: Allowed memory size of 8589934592 bytes exhausted (tried to allocate 65536 bytes) in phar:///WORKFOLDER/back/app/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/string/AbstractString.php on line 445\n",
        "Child process error (exit code 255): PHP Fatal error:  Allowed memory size of 8589934592 bytes exhausted (tried to allocate 262144 bytes) in phar:///WORKFOLDER/back/app/vendor/phpstan/phpstan/phpstan.phar/src/Reflection/ReflectionProvider/MemoizingReflectionProvider.php on line 38\nFatal error: Allowed memory size of 8589934592 bytes exhausted (tried to allocate 262144 bytes) in phar:///WORKFOLDER/back/app/vendor/phpstan/phpstan/phpstan.phar/src/Reflection/ReflectionProvider/MemoizingReflectionProvider.php on line 38\nPHP Fatal error:  Allowed memory size of 8589934592 bytes exhausted (tried to allocate 262144 bytes) in Unknown on line 0\nFatal error: Allowed memory size of 8589934592 bytes exhausted (tried to allocate 262144 bytes) in Unknown on line 0\n"
    ]
}
@mergeable
Copy link

mergeable bot commented Mar 25, 2022

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

It will most likely be closed after manual review.

@herndlm
Copy link
Contributor

herndlm commented Mar 25, 2022

Most likely a recursion problem. Can you create a minimal reproducing example and share that? If it would fit in a phpstan.org snippet, that would be perfect. If not, a dedicated public repo would be great.

@Ciloe
Copy link
Author

Ciloe commented Mar 25, 2022

Very difficult, I don't kwon, in my code, where it's broken, and my repo can't be public....
How can I see where the recursion appeared ?

@danog
Copy link

danog commented Mar 25, 2022

Might be related to my #6896, I created that snippet by first enabling debug logs to check which file is being scanned and where does it get stuck (running phpstan for one file at a time can also help), then I trimmed the contents of the file until I had a small enough snippet that reproduced the error.

@Ciloe
Copy link
Author

Ciloe commented Mar 25, 2022

This class is in error :

<?php

namespace App\Controller;

use App\AppException\AppException;
use ReflectionClass;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormErrorIterator;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\Translation\TranslatorInterface;

abstract class AbstractSmartController extends AbstractController
{
    protected array $viewVars = [];
    protected array $jsVars = [];

    /** @required */
    public TranslatorInterface $translator;

    public function getTemplatePath(string $view): string
    {
        $reflector = new ReflectionClass(get_called_class());

        $controllerName = str_replace(
            'Controller',
            '',
            basename($reflector->getFileName(), '.php')
        );

        $templateDirPath =
            str_replace(
                $this->getParameter('kernel.project_dir') . '/src/Controller/',
                '',
                dirname($reflector->getFileName())
            ) . '/' . $controllerName;

        $view = $this->toSnakeCase($view);

        return $templateDirPath
            . '/' . $view . '.html.twig';
    }

    protected function renderTemplate(string $view): Response
    {
        $view = $this->getTemplatePath($view);
        $this->viewVars['js'] = json_encode($this->jsVars);

        return $this->render($view, $this->viewVars);
    }

    protected function toSnakeCase($str, $glue = '_')
    {
        return preg_replace_callback('/[A-Z]/', function ($matches) use ($glue): string {
            return $glue . strtolower($matches[0]);
        }, $str);
    }

    protected function createJsonErrorResponse(AppException $exception): JsonResponse
    {
        return new JsonResponse([
            'errors' => [
                [
                    'message' => $exception->getAppMessage($this->translator),
                    'code' => $exception->getAppCode(),
                ]
            ]
        ]);
    }

    protected function redirectAfterSubmit(Request $request): ?Response
    {
        $next = $request->get('goto', null);
        if (null !== $next) {
            return $this->redirect($next);
        }

        return null;
    }

    protected function getErrorOriginIdentifier(FormError $error): string
    {
        $origins = [$error->getOrigin()->getName()];
        $parent = $error->getOrigin()->getParent();

        while ($parent !== null) {
            $origins[] = $parent->getName();
            $parent = $parent->getParent();
        }

        return join('_', array_reverse($origins));
    }

    protected function createJsonFormErrorResponse(FormErrorIterator $errorIterator): Response
    {
        $formErrors = [];
        foreach ($errorIterator as $error) {
            $fieldIdentifier = $this->getErrorOriginIdentifier($error);
            if (!array_key_exists($fieldIdentifier, $formErrors)) {
                $formErrors[$fieldIdentifier] = [];
            }
            $formErrors[$fieldIdentifier][] = [
                'message' => $error->getMessage(),
            ];
        }

        return new JsonResponse([
            'formErrors' => $formErrors
        ]);
    }
}

I worked on the getTemplatePath to see if in it, but not...

@Ciloe
Copy link
Author

Ciloe commented Mar 25, 2022

I tested on a simple repo with only this code, but it's not in error....
If I exclude the controller, phpstan kill in an other controller....
If I exclude all controllers, phpstan working fine !

EDIT :
I excluded this controllers :

        - src/Controller/AbstractSmartController*
        - src/Controller/Back/Admin/HolidayController*
        - src/Controller/ConnectedSpace/CardController*
        - src/Controller/ConnectedSpace/CalendarModelController*
        - src/Controller/ConnectedSpace/CalendarReservationController*
        - src/Controller/ConnectedSpace/API/CalendarModelApiController*
        - src/Controller/ConnectedSpace/API/ReservationController*
        - src/Controller/ConnectedSpace/API/CalendarReservationApiController*
        - src/Controller/Front/PartnerController*

EDIT 2 :
If I exclude from analyse it's working too :

parameters:
    level: 5
    paths:
        - src/Controller

    excludePaths:
        analyse:
            - src/Controller/AbstractSmartController*
            - src/Controller/Back/Admin/HolidayController*
            - src/Controller/ConnectedSpace/CardController*
            - src/Controller/ConnectedSpace/CalendarModelController*
            - src/Controller/ConnectedSpace/CalendarReservationController*
            - src/Controller/ConnectedSpace/API/CalendarModelApiController*
            - src/Controller/ConnectedSpace/API/ReservationController*
            - src/Controller/ConnectedSpace/API/CalendarReservationApiController*
            - src/Controller/Front/PartnerController*
        analyseAndScan:
            - */Migrations/*
            - src/Command/ImportDatabase/*
            - */tests/*

@Ciloe
Copy link
Author

Ciloe commented Mar 25, 2022

I have the same error using Rector. It's related to FormErrorIterator from symfony

@herndlm
Copy link
Contributor

herndlm commented Mar 26, 2022

Then it indeed sounds very related to what @danog reported. I can check this weekend

@ondrejmirtes
Copy link
Member

Most likely fixed by: phpstan/phpstan-src#1137

@Ciloe
Copy link
Author

Ciloe commented Mar 29, 2022

Is it possible to tag a new version ?

@ondrejmirtes
Copy link
Member

Try it out ;) https://github.com/phpstan/phpstan/releases/tag/1.5.2

@Ciloe
Copy link
Author

Ciloe commented Mar 29, 2022

Nice, it's working

@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 Apr 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants