From e8082f641090394cdbd22ff67616f4f3e1b9c476 Mon Sep 17 00:00:00 2001 From: brandonkelly Date: Thu, 28 Nov 2024 05:04:56 -0800 Subject: [PATCH] Try inserting search keywords 3 times fixes #15221 --- CHANGELOG.md | 1 + src/services/Search.php | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a22f132030..7708d2e9564 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased - Fixed an error that could occur when duplicating an element with an Assets field that had a dynamic subpath. ([#16214](https://github.com/craftcms/cms/issues/16214)) +- Reduced the likelihood of a deadlock error occurring when updating search indexes. ([#15221](https://github.com/craftcms/cms/issues/15221)) ## 4.13.3 - 2024-11-22 diff --git a/src/services/Search.php b/src/services/Search.php index 05237a58725..e045e5f6eea 100644 --- a/src/services/Search.php +++ b/src/services/Search.php @@ -27,6 +27,7 @@ use craft\search\SearchQueryTermGroup; use Throwable; use yii\base\Component; +use yii\db\Exception; use yii\db\Expression; use yii\db\Schema; @@ -483,7 +484,20 @@ private function _indexKeywords(ElementInterface $element, string $keywords, ?st } // Insert/update the row in searchindex - Db::insert(Table::SEARCHINDEX, $columns); + for ($try = 0; $try < 3; $try++) { + try { + Db::insert(Table::SEARCHINDEX, $columns); + return; + } catch (Exception $e) { + if (str_contains($e->getPrevious()?->getMessage(), 'deadlock')) { + // A gap lock was probably hit. Try again in one second + // https://github.com/craftcms/cms/issues/15221 + sleep(1); + } else { + throw $e; + } + } + } } /**