Skip to content

Commit

Permalink
Merge pull request #5470 from nextcloud/fix/duplicate-document-init-race
Browse files Browse the repository at this point in the history
fix: Avoid race condition that may initialize a document twice on the clients
  • Loading branch information
juliusknorr authored Mar 13, 2024
2 parents 1a433fd + 9412ae1 commit e2ad77e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/Service/ApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
Expand Down
3 changes: 2 additions & 1 deletion lib/Service/DocumentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit e2ad77e

Please sign in to comment.