Skip to content

Commit

Permalink
fix: Avoid race condition that may initialize a document twice on the…
Browse files Browse the repository at this point in the history
… clients

Signed-off-by: Julius Härtl <[email protected]>

[skip ci]
  • Loading branch information
juliushaertl authored and backportbot[bot] committed Mar 13, 2024
1 parent 7d90d51 commit 1e2e055
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/Service/ApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,12 @@ public function create($fileId = null, $filePath = null, ?string $token = null,

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());
}
Expand Down
3 changes: 2 additions & 1 deletion lib/Service/DocumentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,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;
Expand Down Expand Up @@ -158,7 +159,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;
Expand Down

0 comments on commit 1e2e055

Please sign in to comment.