Skip to content

Commit f81d898

Browse files
authored
Merge pull request #7736 from nextcloud/backport/7703/stable31
[stable31] fix(DocumentService): Return 200 steps before saved version in SyncStep2
2 parents a875143 + 0aac11c commit f81d898

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

lib/Db/StepMapper.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,25 @@ public function getLatestVersion(int $documentId): ?int {
5555
return $data['id'];
5656
}
5757

58+
public function getBeforeVersion(int $documentId, int $version, int $offset): int {
59+
$qb = $this->db->getQueryBuilder();
60+
$result = $qb->select('id')
61+
->from($this->getTableName())
62+
->where($qb->expr()->eq('document_id', $qb->createNamedParameter($documentId)))
63+
->andWhere($qb->expr()->lt('id', $qb->createNamedParameter($version)))
64+
->setMaxResults($offset)
65+
->orderBy('id', 'DESC')
66+
->executeQuery();
67+
68+
$rows = $result->fetchAll();
69+
$data = end($rows);
70+
if ($data === false) {
71+
return $version;
72+
}
73+
74+
return $data['id'];
75+
}
76+
5877
public function deleteAll(int $documentId): void {
5978
$qb = $this->db->getQueryBuilder();
6079
$qb->delete($this->getTableName())

lib/Service/DocumentService.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,9 @@ public function addStep(Document $document, Session $session, array $steps, int
239239
$stateFile = $this->getStateFile($documentId);
240240
$documentState = $stateFile->getContent();
241241
$this->logger->debug('Existing document, state file loaded ' . $documentId);
242-
// If there were any queries in the steps, send all steps since last save.
243-
$getStepsSinceVersion = $document->getLastSavedVersion();
242+
// If there were any queries in the steps, send all steps starting 200 steps before last save.
243+
// Adding 200 previous steps to workaround race conditions where state with missing step got persisted in the document state. See #7692
244+
$getStepsSinceVersion = $this->stepMapper->getBeforeVersion($documentId, $document->getLastSavedVersion(), 200);
244245
} catch (NotFoundException $e) {
245246
$this->logger->debug('Existing document, but no state file found for ' . $documentId);
246247
// If there is no state file, include all the steps.

0 commit comments

Comments
 (0)