Skip to content

Commit

Permalink
Acquire a lock when updating elements via GraphQL
Browse files Browse the repository at this point in the history
Fixes #14113
  • Loading branch information
brandonkelly committed Jan 9, 2024
1 parent 45519e2 commit bb12adc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Fixed a bug where the “Save and add another” element action wasn’t redirecting to a new element edit page, when editing an unpublished draft.
- Fixed a bug where `craft\helpers\DateTimeHelper::nextYear()` and `lastYear()` weren’t returning the correct dates. ([#14109](https://github.com/craftcms/cms/issues/14109))
- Fixed a bug where image transforms weren’t getting created for remote assets, if `maxCachedCloudImageSize` was set to `0`. ([#14100](https://github.com/craftcms/cms/issues/14100))
- Fixed a SQL error that could occur when updating existing elements via GraphQL mutations. ([#14113](https://github.com/craftcms/cms/issues/14113))

## 4.5.14 - 2024-01-02

Expand Down
18 changes: 17 additions & 1 deletion src/gql/base/ElementMutationResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\WrappingType;
use yii\web\ServerErrorHttpException;

/**
* Class MutationResolver
Expand Down Expand Up @@ -160,7 +161,22 @@ protected function saveElement(ElementInterface $element): ElementInterface
$element->setScenario(Element::SCENARIO_LIVE);
}

Craft::$app->getElements()->saveElement($element);
$isNotNew = $element->id;
if ($isNotNew) {
$lockKey = "element:$element->id";
$mutex = Craft::$app->getMutex();
if (!$mutex->acquire($lockKey, 15)) {
throw new ServerErrorHttpException('Could not acquire a lock to save the element.');
}
}

try {
Craft::$app->getElements()->saveElement($element);
} finally {
if ($isNotNew) {
$mutex->release($lockKey);
}
}

if ($element->hasErrors()) {
$validationErrors = [];
Expand Down

0 comments on commit bb12adc

Please sign in to comment.