From 9412ae1f2afb6db81c4465a81032e6697bf7cf73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Wed, 13 Mar 2024 09:25:40 +0100 Subject: [PATCH] fix: Avoid race condition that may initialize a document twice on the clients MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- lib/Service/ApiService.php | 8 +++++++- lib/Service/DocumentService.php | 3 ++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/Service/ApiService.php b/lib/Service/ApiService.php index 436ddc73eee..bcd89e4ded6 100644 --- a/lib/Service/ApiService.php +++ b/lib/Service/ApiService.php @@ -37,6 +37,7 @@ use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; use OCP\Constants; +use OCP\Files\AlreadyExistsException; use OCP\Files\InvalidPathException; use OCP\Files\Lock\ILock; use OCP\Files\NotFoundException; @@ -117,7 +118,12 @@ public function create(?int $fileId = null, ?string $filePath = null, ?string $t if ($freshSession) { $this->logger->info('Create new document of ' . $file->getId()); - $document = $this->documentService->createDocument($file); + try { + $document = $this->documentService->createDocument($file); + } catch (AlreadyExistsException) { + $freshSession = false; + $document = $this->documentService->getDocument($file->getId()); + } } else { $this->logger->info('Keep previous document of ' . $file->getId()); } diff --git a/lib/Service/DocumentService.php b/lib/Service/DocumentService.php index 4784f1b32ae..5b1cb09d251 100644 --- a/lib/Service/DocumentService.php +++ b/lib/Service/DocumentService.php @@ -41,6 +41,7 @@ use OCP\Constants; use OCP\DB\Exception; use OCP\DirectEditing\IManager; +use OCP\Files\AlreadyExistsException; use OCP\Files\Config\IUserMountCache; use OCP\Files\File; use OCP\Files\Folder; @@ -157,7 +158,7 @@ public function createDocument(File $file): Document { } catch (Exception $e) { if ($e->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) { // Document might have been created in the meantime - return $this->documentMapper->find($file->getId()); + throw new AlreadyExistsException(); } throw $e;