3

After upgrading to Magento 2.4, I've been unable to run a stable production environment. HTTPD spawns the maximum number of processes and uses all of my systems memory and swap (it takes 5-20 minutes to reach this point).

I'm constantly seeing the following errors in httpd error_log:

[Mon Aug 17 19:40:48.077047 2020] [php7:error] [pid 16939:tid 140670127806208] [client 127.0.0.1:40644] PHP Fatal error:  Allowed memory size of 792723456 bytes exhausted (tried to allocate 20480 bytes) in /var/www/html/vendor/magento/module-csp/Model/BlockCache.php on line 108
[Mon Aug 17 19:40:48.125051 2020] [php7:error] [pid 16939:tid 140670127806208] [client 127.0.0.1:40644] PHP Fatal error:  Allowed memory size of 792723456 bytes exhausted (tried to allocate 40960 bytes) in Unknown on line 0
[Mon Aug 17 19:40:50.052935 2020] [php7:error] [pid 16932:tid 140669993522944] [client 127.0.0.1:40654] PHP Fatal error:  Allowed memory size of 792723456 bytes exhausted (tried to allocate 48238592 bytes) in /var/www/html/vendor/magento/framework/Serialize/Serializer/Json.php on line 24
[Mon Aug 17 19:40:51.592030 2020] [php7:error] [pid 16933:tid 140670169769728] [client 127.0.0.1:40666] PHP Fatal error:  Allowed memory size of 792723456 bytes exhausted (tried to allocate 20480 bytes) in /var/www/html/vendor/magento/module-csp/Model/BlockCache.php on line 108
[Mon Aug 17 19:41:12.706529 2020] [php7:error] [pid 17146:tid 140670127806208] [client 127.0.0.1:40702] PHP Fatal error:  Allowed memory size of 792723456 bytes exhausted (tried to allocate 20480 bytes) in /var/www/html/vendor/magento/module-csp/Model/BlockCache.php on line 108
[Mon Aug 17 19:41:12.740424 2020] [php7:error] [pid 17146:tid 140670127806208] [client 127.0.0.1:40702] PHP Fatal error:  Allowed memory size of 792723456 bytes exhausted (tried to allocate 122880 bytes) in Unknown on line 0
[Mon Aug 17 19:41:24.887257 2020] [php7:error] [pid 16939:tid 140670111020800] [client 127.0.0.1:40798] PHP Fatal error:  Allowed memory size of 792723456 bytes exhausted (tried to allocate 4096 bytes) in /var/www/html/vendor/magento/module-csp/Model/BlockCache.php on line 78
[Mon Aug 17 19:41:24.913106 2020] [php7:error] [pid 16939:tid 140670111020800] [client 127.0.0.1:40798] PHP Fatal error:  Allowed memory size of 792723456 bytes exhausted (tried to allocate 65536 bytes) in Unknown on line 0
[Mon Aug 17 19:41:39.997127 2020] [php7:error] [pid 16933:tid 140670152984320] [client 127.0.0.1:40904] PHP Fatal error:  Allowed memory size of 792723456 bytes exhausted (tried to allocate 20480 bytes) in /var/www/html/vendor/magento/module-csp/Model/BlockCache.php on line 108, referer: http://ip-of-my-ec2-instance:80/
[Mon Aug 17 19:41:40.030659 2020] [php7:error] [pid 16933:tid 140670152984320] [client 127.0.0.1:40904] PHP Fatal error:  Allowed memory size of 792723456 bytes exhausted (tried to allocate 40960 bytes) in Unknown on line 0, referer: http://ip-of-my-ec2-instance:80/
[Mon Aug 17 19:42:08.849538 2020] [php7:error] [pid 17146:tid 140670111020800] [client 127.0.0.1:41016] PHP Fatal error:  Allowed memory size of 792723456 bytes exhausted (tried to allocate 20480 bytes) in /var/www/html/vendor/magento/framework/Serialize/Serializer/Json.php on line 42
[Mon Aug 17 19:42:08.865389 2020] [php7:error] [pid 17146:tid 140670111020800] [client 127.0.0.1:41016] PHP Fatal error:  Allowed memory size of 792723456 bytes exhausted (tried to allocate 32768 bytes) in Unknown on line 0
[Mon Aug 17 19:44:23.245570 2020] [php7:error] [pid 17146:tid 140670027093760] [client 127.0.0.1:41798] PHP Fatal error:  Allowed memory size of 792723456 bytes exhausted (tried to allocate 20480 bytes) in /var/www/html/vendor/magento/framework/Serialize/Serializer/Json.php on line 42, referer: https://www.google.com/
[Mon Aug 17 19:44:23.312177 2020] [php7:error] [pid 17146:tid 140670027093760] [client 127.0.0.1:41798] PHP Fatal error:  Allowed memory size of 792723456 bytes exhausted (tried to allocate 45056 bytes) in Unknown on line 0, referer: https://www.google.com/
[Mon Aug 17 19:44:29.224653 2020] [php7:error] [pid 16939:tid 140670052271872] [client 127.0.0.1:41742] PHP Fatal error:  Allowed memory size of 792723456 bytes exhausted (tried to allocate 20480 bytes) in /var/www/html/vendor/magento/module-csp/Model/BlockCache.php on line 108
[Mon Aug 17 19:44:29.264725 2020] [php7:error] [pid 16939:tid 140670052271872] [client 127.0.0.1:41742] PHP Fatal error:  Allowed memory size of 792723456 bytes exhausted (tried to allocate 90112 bytes) in Unknown on line 0
[Mon Aug 17 19:44:34.254009 2020] [php7:error] [pid 17146:tid 140670043879168] [client 127.0.0.1:41688] PHP Fatal error:  Allowed memory size of 792723456 bytes exhausted (tried to allocate 20480 bytes) in /var/www/html/vendor/magento/module-csp/Model/BlockCache.php on line 108
[Mon Aug 17 19:44:34.294439 2020] [php7:error] [pid 17146:tid 140670043879168] [client 127.0.0.1:41688] PHP Fatal error:  Allowed memory size of 792723456 bytes exhausted (tried to allocate 36864 bytes) in Unknown on line 0
[Mon Aug 17 19:44:40.650547 2020] [php7:error] [pid 17146:tid 140670010308352] [client 127.0.0.1:41882] PHP Fatal error:  Allowed memory size of 792723456 bytes exhausted (tried to allocate 20480 bytes) in /var/www/html/vendor/magento/framework/Serialize/Serializer/Json.php on line 42
[Mon Aug 17 19:44:40.690603 2020] [php7:error] [pid 17146:tid 140670010308352] [client 127.0.0.1:41882] PHP Fatal error:  Allowed memory size of 792723456 bytes exhausted (tried to allocate 49152 bytes) in Unknown on line 0
[Mon Aug 17 19:44:49.990225 2020] [php7:error] [pid 16939:tid 140670144591616] [client 127.0.0.1:42006] PHP Fatal error:  Allowed memory size of 792723456 bytes exhausted (tried to allocate 33554440 bytes) in /var/www/html/vendor/magento/module-csp/Model/Collector/DynamicCollector.php on line 31

Before upgrading to 2.4, I switched to Elasticsearch 6.7 and upgraded to php7.3 (7.4 isn't available on Amazon Linux AMI as far as I can tell). My system.log and exception.log don't show any relevant errors. What can I do to debug this?

3 Answers 3

4

@hunter We ran into the same thing (next to some others). Upraded to 2.4 and the VPS would kill processes via OOM killer because the RAM Magento was consuming was unstoppable.

Nothing worked until I noticed that the structure and order of the error messages were always the same. Serialize error + collector error + csp error. So we dived into the CSP module and found that around the error line first there is $data = $this->serializer->unserialize($loaded); Then $this->dynamicCollector->add( And then in line 78 it would work with the result and add dynamic policies in the function.

So we found the formula that was triggering the error messages and for testing we just disabled the CSP module and tested this in production mode.

After this all was working like a Japanese bullet train.

I will try to bugfix the module tonight but disabling CSP might keep your shop alive for now.

4
  • Disabling the CSP module worked for me. What I did notice was that if there was a duplicate url or ID in the csp_whitelist.xml, thats when the memory would sky rocket.
    – Hunter
    Commented Sep 10, 2020 at 13:46
  • just bumped into that one after moving to 2.4.1 - did you ever dig further? It seems logical that there would be duplicates in an aggregated csp_whitelist, too!
    – iphigenie
    Commented Nov 10, 2020 at 17:44
  • just to follow up on my comment, I have one live site, a staging site and a test site. Two of them exhibit this behaviour, the third doesn't. One difference is that the third uses the "internal" full page cache. It could be other differences but the whitelist.csp and magento versions etc are identical
    – iphigenie
    Commented Nov 11, 2020 at 9:29
  • ah yes, found it on magento bug list github.com/magento/magento2/issues/29964
    – iphigenie
    Commented Nov 11, 2020 at 10:13
1

Magento recommends a PHP memory limit of 2G, try increasing your php memory limit and restart the process.

https://devdocs.magento.com/guides/v2.4/install-gde/system-requirements-tech.html#memory-requirement

You can achieve this by updating the memory_limit entry within your PHP.ini file

sed -i "s/;*memory_limit =.*/memory_limit = 2048M/i" /etc/php.ini

Or by adding to your PHP pool file by appending

php_admin_value[memory_limit] = 2048M;

Then restart PHP-FPM with either

  • service php-fpm restart
  • systemctl restart php-fpm

Edit

If you are using Apache make sure to check your .user.ini and .htaccess files in both the site root and under /pub and make sure they are set at 2048M

4
  • The memory_limit in my php.ini file is 2G, but 756M. I'll try to increase this to 2G.
    – Hunter
    Commented Aug 17, 2020 at 20:59
  • Ive changed the memory limit in all .htaccess and .user.ini files, and restarted, but I get the same error.
    – Hunter
    Commented Aug 18, 2020 at 15:48
  • Within your pub/index.php or index.php dependent on your web root. Add to the top of the file if ($_GET['phptest']) { phpinfo(); die; }. Then hit example.com?phptest That would give you your PHP ini file path and current memory limit, dont forget to remove the phpinfo call afterwards
    – Sam
    Commented Aug 19, 2020 at 9:32
  • I've done all of this and still get the error occasionally. Any ideas on how to trace it further?
    – Hunter
    Commented Sep 2, 2020 at 15:55
1

Fixed this issue by overriding the below file as suggested in https://github.com/magento/magento2/issues/29964 (iphigenie thanks for adding reference) :

magento2/app/code/Magento/Csp/Model/Collector/DynamicCollector.php

Line 31

$this->added[] = $policy; 

Just replace above with below code :

if (array_key_exists($policy->getId(), $this->added)) { 
     if ($this->merger->canMerge($this->added[$policy->getId()], $policy)) { 
         $this->added[$policy->getId()] = $this->merger->merge($this->added[$policy->getId()], $policy); 
     } else { 
         throw new \RuntimeException('Cannot merge a policy of ' .get_class($policy)); 
     } 
 } else { 
     $this->added[$policy->getId()] = $policy; 
 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.