From 975ed55b5ecea9aacff900ca889b51e43284e753 Mon Sep 17 00:00:00 2001 From: Jonas Date: Wed, 4 Nov 2026 12:19:58 +0100 Subject: [PATCH 1/3] fix(sync): avoid false conflicts between overlapping saves A save request loads the document before checking for outside changes. If another save by Text finished in between, the file checksum no longer matched the loaded document and the save was rejected with 409, which now shows the conflict dialog. Before reporting a conflict, reload the document and only treat the change as outside if the stored checksum still differs from the file. Continue with the reloaded document so the following checks see the version saved in the meantime. Signed-off-by: Jonas Assisted-by: ClaudeCode:claude-opus-5-5 --- lib/Service/DocumentService.php | 23 +++++++---- tests/unit/Service/DocumentServiceTest.php | 48 ++++++++++++++++++++++ 2 files changed, 63 insertions(+), 8 deletions(-) diff --git a/lib/Service/DocumentService.php b/lib/Service/DocumentService.php index 43533caafb1..a6c15a7f042 100644 --- a/lib/Service/DocumentService.php +++ b/lib/Service/DocumentService.php @@ -341,27 +341,34 @@ public function getSteps(int $documentId, int $lastVersion): array { * @throws InvalidPathException * @throws NotFoundException */ - public function assertNoOutsideConflict(Document $document, File $file, bool $force = false, ?string $shareToken = null): void { + public function assertNoOutsideConflict(Document $document, File $file, bool $force = false, ?string $shareToken = null): Document { $documentId = $document->getId(); - $lastMTime = $document->getLastSavedVersionTime(); - $lastEtag = $document->getLastSavedVersionEtag(); - if ($lastMTime <= 0 || $force || $this->isReadOnly($file, $shareToken) || $this->cache->get('document-save-lock-' . $documentId)) { - return; + if ($document->getLastSavedVersionTime() <= 0 || $force || $this->isReadOnly($file, $shareToken) || $this->cache->get('document-save-lock-' . $documentId)) { + return $document; } $fileChecksum = self::computeCheckSum($file->getContent()); if ($document->getChecksum() !== $fileChecksum) { - throw new DocumentSaveConflictException('File changed in the meantime from outside'); + // Another save by Text may have finished after this request loaded the document. + $stored = $this->getDocument($documentId); + if ($stored === null) { + throw new NotFoundException('Document was reset in the meantime'); + } + if ($stored->getChecksum() !== $fileChecksum) { + throw new DocumentSaveConflictException('File changed in the meantime from outside'); + } + $document = $stored; } $fileMtime = $file->getMtime(); $fileEtag = $file->getEtag(); - if ($lastEtag !== $fileEtag || $lastMTime !== $fileMtime) { + if ($document->getLastSavedVersionEtag() !== $fileEtag || $document->getLastSavedVersionTime() !== $fileMtime) { $document->setLastSavedVersionTime($fileMtime); $document->setLastSavedVersionEtag($fileEtag); $this->documentMapper->update($document); } + return $document; } /** @@ -387,7 +394,7 @@ public function autosave(Document $document, File $file, int $version, string $a throw new NotPermittedException('Read-only permission cannot save document changes. Please reload the page.'); } - $this->assertNoOutsideConflict($document, $file, $force); + $document = $this->assertNoOutsideConflict($document, $file, $force); // Abort autosave if already saving. if ($this->cache->get('document-save-lock-' . $documentId) && $manualSave === false) { diff --git a/tests/unit/Service/DocumentServiceTest.php b/tests/unit/Service/DocumentServiceTest.php index 2e0adc402f1..9bd6e2c4e95 100644 --- a/tests/unit/Service/DocumentServiceTest.php +++ b/tests/unit/Service/DocumentServiceTest.php @@ -7,7 +7,9 @@ use OCA\Text\Db\Session; use OCA\Text\Db\SessionMapper; use OCA\Text\Db\StepMapper; +use OCA\Text\Exception\DocumentSaveConflictException; use OCA\Text\Service\DocumentService; +use OCP\AppFramework\Db\DoesNotExistException; use OCP\Constants; use OCP\Files\Config\IUserMountCache; use OCP\Files\File; @@ -15,6 +17,7 @@ use OCP\Files\IAppData; use OCP\Files\IRootFolder; use OCP\Files\Lock\ILockManager; +use OCP\Files\NotFoundException; use OCP\Files\NotPermittedException; use OCP\ICacheFactory; use OCP\IConfig; @@ -203,6 +206,7 @@ private function createDocumentWithContent(string $content): Document { private function createFile(string $content): File { $file = $this->createMock(File::class); $file->method('getId')->willReturn(42); + $file->method('isUpdateable')->willReturn(true); $file->method('getContent')->willReturn($content); $file->method('getMTime')->willReturn(1000); $file->method('getEtag')->willReturn('etag'); @@ -239,4 +243,48 @@ public function testResetsDocumentWhenFileChangedOutside(): void { self::assertNotSame($loaded, $document); self::assertSame(DocumentService::computeCheckSum('changed outside'), $document->getChecksum()); } + + public function testNoConflictIfTextSavedInTheMeantime(): void { + $document = $this->createDocumentWithContent('old content'); + $document->setLastSavedVersionTime(1000); + $document->setLastSavedVersionEtag('etag'); + $document->resetUpdatedFields(); + $stored = $this->createDocumentWithContent('saved by text'); + $stored->setLastSavedVersion(5); + $stored->setLastSavedVersionTime(1000); + $stored->setLastSavedVersionEtag('etag'); + $file = $this->createFile('saved by text'); + $this->documentMapper->method('find')->with(42)->willReturn($stored); + $this->setpMapper->method('getLatestVersion')->willReturn(5); + $file->expects(self::never())->method('putContent'); + + $result = $this->documentService->autosave($document, $file, 4, 'new content', 'doc state'); + self::assertSame($stored, $result); + } + + public function testConflictIfFileChangedOutside(): void { + $document = $this->createDocumentWithContent('old content'); + $document->setLastSavedVersionTime(1000); + $document->resetUpdatedFields(); + $stored = $this->createDocumentWithContent('old content'); + $file = $this->createFile('outside content'); + $this->documentMapper->method('find')->with(42)->willReturn($stored); + $file->expects(self::never())->method('putContent'); + + $this->expectException(DocumentSaveConflictException::class); + $this->documentService->autosave($document, $file, 1, 'new content', 'doc state'); + } + + public function testNotFoundIfDocumentWasResetInTheMeantime(): void { + $document = $this->createDocumentWithContent('old content'); + $document->setLastSavedVersionTime(1000); + $document->resetUpdatedFields(); + $file = $this->createFile('outside content'); + $this->documentMapper->method('find')->with(42) + ->willThrowException(new DoesNotExistException('Document was reset')); + $file->expects(self::never())->method('putContent'); + + $this->expectException(NotFoundException::class); + $this->documentService->autosave($document, $file, 1, 'new content', 'doc state'); + } } From 98d667529ba2fca5babf0f7fc895e2201199899c Mon Sep 17 00:00:00 2001 From: Jonas Date: Wed, 4 Nov 2026 22:54:52 +0100 Subject: [PATCH 2/3] fix(sync): store the checksum of the saved content The checksum of the saved content was stored only after writing the document state. A lock or a failure while writing the state left the previous checksum behind, so every following save was rejected as a conflict. Store the checksum right after writing the file. Signed-off-by: Jonas Assisted-by: ClaudeCode:claude-opus-5-5 --- lib/Service/DocumentService.php | 10 +++++---- tests/unit/Service/DocumentServiceTest.php | 26 ++++++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/lib/Service/DocumentService.php b/lib/Service/DocumentService.php index a6c15a7f042..caef5eea21c 100644 --- a/lib/Service/DocumentService.php +++ b/lib/Service/DocumentService.php @@ -449,15 +449,17 @@ public function autosave(Document $document, File $file, int $version, string $a $file, ILock::TYPE_APP, Application::APP_NAME - ), function () use ($file, $autoSaveDocument, $documentState) { + ), function () use ($document, $file, $autoSaveDocument, $documentState) { $this->saveFromText = true; $file->putContent($autoSaveDocument); + // Store the new checksum first so a failure below cannot leave it outdated. + $document->setLastSavedVersionTime($file->getMTime()); + $document->setLastSavedVersionEtag($file->getEtag()); + $document->setChecksum(self::computeCheckSum($autoSaveDocument)); + $this->documentMapper->update($document); $this->writeDocumentState($file->getId(), $documentState); }); $document->setLastSavedVersion($version); - $document->setLastSavedVersionTime($file->getMTime()); - $document->setLastSavedVersionEtag($file->getEtag()); - $document->setChecksum(self::computeCheckSum($autoSaveDocument)); $this->documentMapper->update($document); } catch (LockedException $e) { // Ignore lock since it might occur when multiple people save at the same time diff --git a/tests/unit/Service/DocumentServiceTest.php b/tests/unit/Service/DocumentServiceTest.php index 9bd6e2c4e95..b77b981bb38 100644 --- a/tests/unit/Service/DocumentServiceTest.php +++ b/tests/unit/Service/DocumentServiceTest.php @@ -17,11 +17,15 @@ use OCP\Files\IAppData; use OCP\Files\IRootFolder; use OCP\Files\Lock\ILockManager; +use OCP\Files\Lock\LockContext; use OCP\Files\NotFoundException; use OCP\Files\NotPermittedException; +use OCP\Files\SimpleFS\ISimpleFile; +use OCP\Files\SimpleFS\ISimpleFolder; use OCP\ICacheFactory; use OCP\IConfig; use OCP\IRequest; +use OCP\Lock\LockedException; use OCP\Share\IManager; use OCP\Share\IShare; use Psr\Log\LoggerInterface; @@ -287,4 +291,26 @@ public function testNotFoundIfDocumentWasResetInTheMeantime(): void { $this->expectException(NotFoundException::class); $this->documentService->autosave($document, $file, 1, 'new content', 'doc state'); } + + public function testStoresChecksumIfWritingTheStateFails(): void { + $document = $this->createDocumentWithContent('old content'); + $document->setLastSavedVersionTime(1000); + $document->setLastSavedVersionEtag('etag'); + $document->resetUpdatedFields(); + $file = $this->createFile('old content'); + $this->setpMapper->method('getLatestVersion')->willReturn(4); + $this->lockManager->method('runInScope')->willReturnCallback( + static fn (LockContext $lock, callable $callback) => $callback() + ); + $stateFile = $this->createMock(ISimpleFile::class); + $stateFile->method('putContent')->willThrowException(new LockedException('documents/42.yjs')); + $folder = $this->createMock(ISimpleFolder::class); + $folder->method('getFile')->willReturn($stateFile); + $this->appData->method('getFolder')->willReturn($folder); + $file->expects(self::once())->method('putContent')->with('new content'); + $this->documentMapper->expects(self::once())->method('update')->with($document); + + $this->documentService->autosave($document, $file, 4, 'new content', 'doc state'); + self::assertSame(DocumentService::computeCheckSum('new content'), $document->getChecksum()); + } } From b2c6cfe789cb131fbc13570836a15dc44a06bda8 Mon Sep 17 00:00:00 2001 From: Jonas Date: Wed, 4 Nov 2026 12:42:04 +0100 Subject: [PATCH 3/3] perf(sync): throttle autosaves before checking for conflicts The conflict check reads the whole file to compute its checksum, also for autosaves that are then dropped because the last save was less than AUTOSAVE_MINIMUM_DELAY seconds ago. Return early for those autosaves before checking for conflicts. They do not write anything, so skipping the check cannot overwrite outside changes. Manual saves are not throttled and are still checked. Signed-off-by: Jonas Assisted-by: ClaudeCode:claude-opus-5-5 --- lib/Service/DocumentService.php | 12 +++++------ tests/unit/Service/DocumentServiceTest.php | 25 ++++++++++++++++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/lib/Service/DocumentService.php b/lib/Service/DocumentService.php index caef5eea21c..ba4a3452f69 100644 --- a/lib/Service/DocumentService.php +++ b/lib/Service/DocumentService.php @@ -394,6 +394,12 @@ public function autosave(Document $document, File $file, int $version, string $a throw new NotPermittedException('Read-only permission cannot save document changes. Please reload the page.'); } + // Only save once every AUTOSAVE_MINIMUM_DELAY seconds + $lastMTime = $document->getLastSavedVersionTime(); + if ($lastMTime > time() - self::AUTOSAVE_MINIMUM_DELAY && $manualSave === false) { + return $document; + } + $document = $this->assertNoOutsideConflict($document, $file, $force); // Abort autosave if already saving. @@ -411,12 +417,6 @@ public function autosave(Document $document, File $file, int $version, string $a return $document; } - // Only save once every AUTOSAVE_MINIMUM_DELAY seconds - $lastMTime = $document->getLastSavedVersionTime(); - if ($file->getMTime() === $lastMTime && $lastMTime > time() - self::AUTOSAVE_MINIMUM_DELAY && $manualSave === false) { - return $document; - } - if (empty($autoSaveDocument)) { $this->logger->warning('Saving empty document', [ 'requestVersion' => $version, diff --git a/tests/unit/Service/DocumentServiceTest.php b/tests/unit/Service/DocumentServiceTest.php index b77b981bb38..71c10fa3033 100644 --- a/tests/unit/Service/DocumentServiceTest.php +++ b/tests/unit/Service/DocumentServiceTest.php @@ -313,4 +313,29 @@ public function testStoresChecksumIfWritingTheStateFails(): void { $this->documentService->autosave($document, $file, 4, 'new content', 'doc state'); self::assertSame(DocumentService::computeCheckSum('new content'), $document->getChecksum()); } + + public function testThrottledAutosaveSkipsConflictCheck(): void { + $document = $this->createDocumentWithContent('content'); + $document->setLastSavedVersionTime(time()); + $document->resetUpdatedFields(); + $file = $this->createFile('outside content'); + $file->expects(self::never())->method('getContent'); + $file->expects(self::never())->method('putContent'); + + $result = $this->documentService->autosave($document, $file, 3, 'new content', 'doc state'); + self::assertSame($document, $result); + } + + public function testManualSaveChecksForConflictsWithinAutosaveDelay(): void { + $document = $this->createDocumentWithContent('old content'); + $document->setLastSavedVersionTime(time()); + $document->resetUpdatedFields(); + $stored = $this->createDocumentWithContent('old content'); + $file = $this->createFile('outside content'); + $this->documentMapper->method('find')->with(42)->willReturn($stored); + $file->expects(self::never())->method('putContent'); + + $this->expectException(DocumentSaveConflictException::class); + $this->documentService->autosave($document, $file, 3, 'new content', 'doc state', false, true); + } }