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
Comment #2
abrammHere'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.
Comment #3
abrammSeems like this is a failing piece:
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.
Comment #4
abrammThe 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():
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.
Comment #5
abrammHere 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.
Comment #6
abrammI'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.
Comment #8
abrammQuick 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.
Comment #9
abrammComment #10
abrammUpdated the title/description a little bit.
Comment #11
bojanz commentedWe 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)
Comment #12
abrammThat'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.
Comment #13
agoradesign commentedJust 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?
Comment #14
abrammHi @agoradesign,
These numbers are real product prices and discount which caused error at my project.
Comment #15
abrammI'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.
Comment #18
abrammOops, 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.
Comment #22
bojanz commentedGreat, that test is just what we needed.
Comment #23
bojanz commentedChanges:
- 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?
Comment #24
abrammHi @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!
Comment #26
bojanz commentedTests 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.
Comment #28
liquidcms commentedClosed, 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.