How a wrong carrier implementation causes a server outage

How a wrong carrier implementation causes a server outage

Sometimes one wrong line of code can break your site. In the following I will describe a mistake in a Magento 2 custom carrier implementation, which causes a massive overloading of server resources (CPU, RAM, DB processes) and even can cause an outage of your Magento store.

The one line of code

The following line of code is the reason for the problems, if used in the collectRates() method, or in methods, called from collectRates() in the Carrier class:

So, in other words, you must not obtain the quote object globally via the checkout session.

The reason

The method \Magento\Checkout\Model\Session::getQuote(), called for the first time, triggers loading the quote. If we then look at the method \Magento\Quote\Model\Quote::_afterLoad() :

We then can see, that for quotes, having the field (also a DB column) trigger_recollect set to 1, collectTotals() method is called.

An attentive reader will already notice, what is going wrong here. It’s an infinite loop! Quote::collectTotals() will trigger shipping carriers’ method collectRates() and thats where the loop is closed.

The trigger_recollect flag is set in Magento:

  • for quotes depending on catalog price rules
  • for quotes containing products which were updated (e.g. in Admin or via API)

In my case there were a lot of such kind of quotes because of frequent product updates.

The result was overloaded CPUs, RAM, full MySQL process list and several outages as the infinite loops were being executed for the value of seconds equals PHP max_execution_time.

How to avoid this

The shipping carrier’s method collectRates() gets the object of the class \Magento\Quote\Model\Quote\Address\RateRequest passed, where the already loaded quote object should be obtained from (if needed). Unfortunately there is no method “getQuote()” in the RateRequest class. The following snippet shows an example of obtaining the quote correctly:

I hope this post can save some nerves for you and your team. Feel free to leave a comment.

Magento Certified Developer Plus

One thought on “How a wrong carrier implementation causes a server outage

Leave a Reply

%d bloggers like this: