Skip to content

Commit a416a5b

Browse files
juliusknorrmejo-
authored andcommitted
fix: Avoid race condition that may initialize a document twice on the clients
Signed-off-by: Julius Härtl <[email protected]>
1 parent 7d90d51 commit a416a5b

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

lib/Service/ApiService.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use OCP\AppFramework\Http;
3838
use OCP\AppFramework\Http\DataResponse;
3939
use OCP\Constants;
40+
use OCP\Files\AlreadyExistsException;
4041
use OCP\Files\Lock\ILock;
4142
use OCP\Files\NotFoundException;
4243
use OCP\Files\NotPermittedException;
@@ -119,7 +120,12 @@ public function create($fileId = null, $filePath = null, ?string $token = null,
119120

120121
if ($freshSession) {
121122
$this->logger->info('Create new document of ' . $file->getId());
122-
$document = $this->documentService->createDocument($file);
123+
try {
124+
$document = $this->documentService->createDocument($file);
125+
} catch (AlreadyExistsException) {
126+
$freshSession = false;
127+
$document = $this->documentService->getDocument($file->getId());
128+
}
123129
} else {
124130
$this->logger->info('Keep previous document of ' . $file->getId());
125131
}

lib/Service/DocumentService.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
use OCP\Constants;
4141
use OCP\DB\Exception;
4242
use OCP\DirectEditing\IManager;
43+
use OCP\Files\AlreadyExistsException;
4344
use OCP\Files\Config\IUserMountCache;
4445
use OCP\Files\File;
4546
use OCP\Files\Folder;
@@ -158,7 +159,7 @@ public function createDocument(File $file): Document {
158159
} catch (Exception $e) {
159160
if ($e->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
160161
// Document might have been created in the meantime
161-
return $this->documentMapper->find($file->getId());
162+
throw new AlreadyExistsException();
162163
}
163164

164165
throw $e;

0 commit comments

Comments
 (0)