Problem/Motivation

Sometimes the 'Maximum execution time exceeded' error occurs during checkout while trying to apply a coupon code / promotion.

The problem is caused by the \Drupal\commerce_order\PriceSplitter::split() method which runs into infinite loop while trying to distribute the reminding amount between order items in some cases.

Here's the case when in happens:
An order containing 4 order items, $59.99 each. The coupon code with 30% off order amount is applied.

The following code in split() runs infinitely:

    // The individual amounts don't add up to the full amount, distribute
    // the reminder among them.
    if (!$amount->isZero()) {
      /** @var \Drupal\commerce_price\Entity\CurrencyInterface $currency */
      $currency = $this->currencyStorage->load($amount->getCurrencyCode());
      $precision = $currency->getFractionDigits();
      // Use the smallest rounded currency amount (e.g. '0.01' for USD).
      $smallest_number = Calculator::divide('1', pow(10, $precision), $precision);
      $smallest_amount = new Price($smallest_number, $amount->getCurrencyCode());
      while (!$amount->isZero()) {
        foreach ($amounts as $order_item_id => $individual_amount) {
          $amounts[$order_item_id] = $individual_amount->add($smallest_amount);
          $amount = $amount->subtract($smallest_amount);
          if ($amount->isZero()) {
            break 2;
          }
        }
      }
    }

The amount subtracted is greater than remaining amount in some cases, which leads to negative $amount and failure in isZero() check.

It turned out the $amount is negative because of incorrect rounding; see #4.

Proposed resolution

Do not use rounding, discard digits behind the precision instead.

Remaining tasks

None.

User interface changes

None.

API changes

None.

Data model changes

None.

Comments

abramm created an issue. See original summary.

abramm’s picture

Here's the failing test case.

The test is incomplete, it's missing assertions; but at least it helps identifying infinite loop.

I've also added a simple condition check and an exception in PriceSplitter so that PHPUnit won't run for ages resulting in timeout and no test results.

abramm’s picture

Seems like this is a failing piece:

$individual_amount = $order_item->getTotalPrice()->multiply($percentage);
$individual_amount = $this->rounder->round($individual_amount, PHP_ROUND_HALF_DOWN);

Given the order item price is 59.99 and $percentage is 0.3, I would expect the $individual_amount would be 17.99; however, the value calculated is 18.00.

59.99 * 0.3 = 17.997, rounding half down it should be 17.99.

abramm’s picture

The rounder works fine; it does exactly what it's supposed to do. If the amount were 17.995, it would round to 17.99.
However, rounding 17.997 always gives 17.99 since 7 > 5.
This may be seen in \Drupal\commerce_price\Calculator::round():

  if ($relevant_decimal < 5) {
      $number = $rounded_down;
    }
    elseif ($relevant_decimal == 5) {
      // Skipped  PHP_ROUND_* logic.
    }
    elseif ($relevant_decimal > 5) {
      $number = $rounded_up;
    }

Here's a patch with a check and test which shows the exact place where calculations goes wrong.
Seems like the PriceSplitter shouldn't use rounder at all, instead it should simply discard any digits after the precision.

abramm’s picture

Here we go.

The first patch is a test only and is expected to fail.
The second patch is a test + fix. I'm not sure about details such as wording so any suggestions are welcome.

Also, I'd like to explain why I've changed the test case with 4.54/0.46 split to 4.55/0.45.
In this test case, there are two order items, their prices are 9.99 and 1.01. Each order item quantity is 3. The amount to split is 5.

Amounts are:
5/33*9.99*3 = 4.5409 <- this amount looked correct, was rounded to 4.54.
5/33*1.01*3 = 0.459090909 <- this amount was incorrectly rounded to 0.46 while it should be 0.45.

Now, when 0.459 becomes 0.45, the remaining 1 cent is distributed among all order items and so it's being added to the first order item which becomes 0.45 + 0.01 = 0.46.

abramm’s picture

Assigned: abramm » Unassigned
Issue summary: View changes
Status: Active » Needs review

I've queued tests with both PHP 5.5 and 7 to make sure math works the same way.

The patch should now be ready for review.

abramm’s picture

Quick addition. I've just realized I forgot to include my changes in services.yml; I've removed one of the service arguments since it's not needed anymore.

abramm’s picture

abramm’s picture

Title: Infinite loop in PriceSplitter » Maximum execution time exceeded error caused by infinite loop in PriceSplitter
Issue summary: View changes

Updated the title/description a little bit.

bojanz’s picture

Status: Needs review » Needs work
StatusFileSize
new1.52 KB

We need a test that's going to fail without a fix. I've tried running just your test change without the fix, and it still passes.

Attaching a patch with just your test additions, for reference.

(Note that #5 fails because of the changes to the existing test, not because of the new test case)

abramm’s picture

Assigned: Unassigned » abramm

That's strange. The same test case was causing an infinite loop for me and phpunit run never completed. Maybe something changed in Commerce code.

Thanks for the review @bojanz.

I'm currently out of office; I'll check in a week or two.

agoradesign’s picture

Just read through the test. I've two questions:

First, why do you call the splitter with 30% as $percentage parameter?

Second, shouldn't it theoretically be easier to reproduce with a division causing a repeating decimal? Like distributing 10 across 3 items?

abramm’s picture

Hi @agoradesign,

These numbers are real product prices and discount which caused error at my project.

abramm’s picture

I've just found another failing case.

This time I've added two products to a cart, $69.99 each and applied the same 30% off promotion.

I'm not sure how D.org CI handles infinite loops in tests so I've added two failing tests patches, one causing infinite loop and another one throwing an exception.

The patch with a test+fix is updated as well.

Status: Needs review » Needs work
abramm’s picture

Oops, I forgot about order items quantities.

Three patches again.

1) Expected to fail due to infinite loop.
2) Expected to fail due to an exception thrown in a place where infinite loop occurs.
3) Expected to pass.

bojanz’s picture

Assigned: Unassigned » bojanz

Great, that test is just what we needed.

bojanz’s picture

Assigned: bojanz » Unassigned
StatusFileSize
new3.94 KB

Changes:
- Cleaned up the tests some more (default $quantity = 1, differentiate $amount and $unit_price, remove the previous test case with a reminder, which obviously wasn't good enough)
- Simplified the fix. If the last individual item is larger than the amount, just take the amount. This way we don't need to load the currency or perform the truncation.

Can I get a +1, abramm?

abramm’s picture

Hi @bojanz,

TBH I don't like an idea to use rounder here (and remember, the Rounder loads currency too). But that's just my opinion. That's not a big deal. Maybe we would want to add a truncateDecimals method to the Rounder service some day?

I'm happy with your variant as long as tests passes.

Thanks!

  • bojanz committed 5e63627 on 8.x-2.x authored by abramm
    Issue #2999561 by abramm, bojanz: Maximum execution time exceeded error...
bojanz’s picture

Status: Needs review » Fixed

Tests pass with the exact same numbers. So I'm going to proceed with #23 and explore truncation in some later issue, if it gets raised as needed somewhere else.

Thank you for the big effort needed to reproduce this, and write the test.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

liquidcms’s picture

Closed, so likely no one will see this, but...

curious how I still have this issue in 2.17; which is much more recent than 2 years ago.

Will track down if the patch has been lost along the way somewhere in the morning.