Fix infinite loop and unbounded batches in persistent quote cleanup cron - #333
Merged
rhoerr merged 3 commits intoAug 31, 2026
Merged
Conversation
…cleanup cron The batch cursor (lastProcessedId) only advanced on a successful delete. If every row in a batch failed to delete (e.g. a foreign key from another table blocking the delete), the cursor never moved, the next iteration re-selected the identical set, and the while(true) loop never terminated. The cursor now advances unconditionally after each row is visited. The catch block also logged the full stringified exception (including its stack trace) once per failed row; it now logs only the exception message. Same fix submitted upstream to magento/magento2#41173.
getExpiredPersistentQuotes() applied setOrder()/setPageSize() to an intermediate collection whose Select is only used to build a sub-select of matching entity IDs; the collection actually returned and iterated received neither an ORDER BY nor a LIMIT, so every batch loaded the entire expired-quote backlog for the store regardless of batchSize. The order and limit are now applied to the collection that is returned. A LIMIT cannot live inside the IN (...) union sub-select on MySQL, which is why it is applied to the outer collection. This must ship together with the unconditional cursor advance in the companion commit, and the ORDER BY is not optional: with a real LIMIT and an unconditional cursor but no deterministic order, lastProcessedId becomes the last row visited rather than the true maximum, and rows in between are skipped permanently. Today that cannot happen only because the missing LIMIT makes every batch the whole set. Same fix submitted upstream to magento/magento2#41173.
ExpiredPersistentQuotesCollectionTest configures three consecutive getSelect() return values but never stubs where()/order()/limit() on the third one (the Select for the collection actually returned). That was harmless before this fix because the source only called where() on it and discarded the result; the previous commit now chains order()/limit() onto that same call, so the mock needs behavior configured for all three or the chained calls run against an unconfigured (null) return value.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The
persistent_clear_expiredcron job (Magento\Persistent\Observer\ClearExpiredCronJobObserver→Magento\Persistent\Model\CleanExpiredPersistentQuotes) cleans up expired persistent quotes in batches (defaultbatchSize500, set inMagento_Persistent/etc/di.xml). It has three related defects, all present unchanged in this branch's target.Root cause
Unbounded loop. In
processStoreQuotes(),$lastProcessedIdonly advances whenquoteRepository->delete($quote)succeeds:getExpiredPersistentQuotes()selectsmain_table.entity_id > $lastProcessedId. If every delete in a batch throws (for example, a foreign key from another table blocking the delete), the cursor never advances, the next iteration re-selects the identical batch, andwhile (true)never terminates.batchSizehas no effect on the query. InExpiredPersistentQuotesCollection::getExpiredPersistentQuotes(),setOrder()andsetPageSize()are applied to$additionalQuotes, but that collection'sSelectis only cloned to build a sub-select of matching entity IDs. The collection actually returned and iterated ($quotes) receives only aWHERE ... IN (...)and neither anORDER BYnor aLIMIT:Every batch loads the store's entire expired-quote backlog in one query, regardless of
$batchSize.Log volume. The catch block logs
(string)$e, stringifying the whole exception including its stack trace, once per failed row.Fix
$lastProcessedId = (int)$quote->getId();above thetry, unconditional, so the cursor advances past a row once it has been visited, whether or not the delete succeeded.LIMITcan't live inside theIN (...)union sub-select on MySQL, which is why it sits on the outer collection.)$e->getMessage()instead of(string)$e.ExpiredPersistentQuotesCollectionTestto stub the neworder()/limit()calls on the returned collection'sSelectmock (harmless before this change, since the original code never used that call's return value).D1 (unconditional cursor advance) and D2 (the real
LIMIT) must ship together, and theORDER BYis not optional. With a realLIMITand an unconditional cursor but no deterministic order,$lastProcessedIdbecomes the last row visited in an arbitrary batch rather than the true maximum, and rows in between are skipped permanently. Today that cannot happen only because the missingLIMITmakes every batch the whole remaining set.Testing
This reproduces with core code only. The general condition is anything that makes
QuoteRepository::delete()throw for the highest-id expired persistent quote in a store — for example a foreign key from a custom table referencingquote.entity_idwithON DELETE NO ACTION.is_persistent = 1,updated_atolder than the configured persistence lifetime, matching thecustomer_logconditions ingetExpiredPersistentQuotes(). Note itsentity_id, e.g.123.bin/magento cron:run --group default, or invokeCleanExpiredPersistentQuotes::execute($websiteId)directly).$lastProcessedIdnever advances past it, so the same batch is re-selected forever. The job never terminates and the log fills with a full stack trace per iteration.DROP TABLE quote_reference_test;Related
Same fix submitted upstream to magento/magento2: magento/magento2#41173