There are situations in Magento 2 when page keeps loading and then you get 500 fatal error, memory limit, or timeout error. This is an infinite loop in the PHP code, when the same code is executed over and over again. It is related to some core Magento issues or, most likely, third party extension.
To debug an infinite loop and find the loop entrance, please follow the steps below:
1. Open the app/bootstrap.php file and add this code right after PHP open tag <?php in the next line
$_SERVER['MAGE_PROFILER'] = 'html';
2. Open the vendor/magento/framework/Profiler.php file and add this code to the beginning of "public static function start($timerName, array $tags = null)" function, e.g.
private static $firsttime = null;
public static function start($timerName, array $tags = null)
{
if (!self::$firsttime) {
self::$firsttime = time();
}
if (time() - self::$firsttime > 10) { //10 - is seconds to wait
\Magento\Framework\Debug::backtrace(false, true, false); // Magento 2 Debug Backtrace
exit();
}
3. Open the page with the loop issue.
4. Check the backtrace information to get an idea of the code creating a loop. Pay attention to the custom extension, you should see their methods over and over again in the backtrace.
5. Once it is done, don't forget to revert changes in bootstrap.php and Profiler.php
My sympathies with your country, we are waiting for this unjust war to end.
I'm 4 days almost 14 hours a day trying to find just this subject. I almost cried with joy when I saw him... everything was going well in item 1. kkk
Then in item 2, everything went wrong... I couldn't understand how to do it right, and in many ways I change the error on line 300... which is Stop.
I'm a layman, I'm not a developer... but I'm a comparator and I get by.
Would it be too much to ask to detail just a little more how to do item 2?
Thank you in advance
Jean Carlos
In the backtrace you will see repeat methods execution or a group of methods. Then in one of that methods to stop the loop you need to do something like this:
self::isSomeFunctionExecutingNow = false;
public function someFunctionName($params)
{
if (self::isSomeFunctionExecutingNow) {
return $something;
}
self::isSomeFunctionExecutingNow = true;
//function code here, that creates a loop
self::isSomeFunctionExecutingNow = false;
return $something;
}
This is just a quick solution, a better solution needs to understand the code.