diff options
author | Joas Schilling <coding@schilljs.com> | 2025-05-30 08:53:18 +0200 |
---|---|---|
committer | backportbot[bot] <backportbot[bot]@users.noreply.github.com> | 2025-05-30 08:00:24 +0000 |
commit | 93c02cbc18fa0321226aec1d510ebb9e09cdc6c6 (patch) | |
tree | 9e8a86b929a13a6ba3974d0311998259f3ea7697 | |
parent | b5aaa4253d51273d16aab9418d0168ec41fecea1 (diff) | |
download | nextcloud-server-backport/53198/stable29.tar.gz nextcloud-server-backport/53198/stable29.zip |
fix(user_status): Avoid unique constraint violations from parallel heartbeats and GET requestsbackport/53198/stable29
Signed-off-by: Joas Schilling <coding@schilljs.com>
-rw-r--r-- | apps/user_status/lib/Service/StatusService.php | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/apps/user_status/lib/Service/StatusService.php b/apps/user_status/lib/Service/StatusService.php index a291b4420c1..287fae34874 100644 --- a/apps/user_status/lib/Service/StatusService.php +++ b/apps/user_status/lib/Service/StatusService.php @@ -186,7 +186,7 @@ class StatusService { $userStatus->setIsBackup(false); if ($userStatus->getId() === null) { - return $this->mapper->insert($userStatus); + return $this->insertWithoutThrowingUniqueConstrain($userStatus); } return $this->mapper->update($userStatus); @@ -230,7 +230,7 @@ class StatusService { $userStatus->setStatusMessageTimestamp($this->timeFactory->now()->getTimestamp()); if ($userStatus->getId() === null) { - return $this->mapper->insert($userStatus); + return $this->insertWithoutThrowingUniqueConstrain($userStatus); } return $this->mapper->update($userStatus); @@ -332,7 +332,7 @@ class StatusService { if ($userStatus->getId() !== null) { return $this->mapper->update($userStatus); } - return $this->mapper->insert($userStatus); + return $this->insertWithoutThrowingUniqueConstrain($userStatus); } /** @@ -379,7 +379,7 @@ class StatusService { $userStatus->setStatusMessageTimestamp($this->timeFactory->now()->getTimestamp()); if ($userStatus->getId() === null) { - return $this->mapper->insert($userStatus); + return $this->insertWithoutThrowingUniqueConstrain($userStatus); } return $this->mapper->update($userStatus); @@ -603,4 +603,16 @@ class StatusService { // For users that matched restore the previous status $this->mapper->restoreBackupStatuses($restoreIds); } + + protected function insertWithoutThrowingUniqueConstrain(UserStatus $userStatus): UserStatus { + try { + return $this->mapper->insert($userStatus); + } catch (Exception $e) { + // Ignore if a parallel request already set the status + if ($e->getReason() !== Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) { + throw $e; + } + } + return $userStatus; + } } |