[db] Fix concurrent index updates

If we are used to update the same repo at almost the same time, a race-condition can happen that tries to apply a diff to an already updated DB. We don't download anything while holding a DB transaction, so the download and check for the repo timestamp happens before we enter the transaction. However, we forgot to re-check the timestamp again within the transaction to be sure the DB state is still as expected.
This commit is contained in:
Torsten Grote
2024-09-26 17:36:22 -03:00
committed by Torsten Grote
parent 632f55eb15
commit cfaf9a5ad2
2 changed files with 69 additions and 2 deletions

View File

@@ -94,6 +94,12 @@ public class IndexV2Updater(
file.inputStream().use { inputStream ->
val repoDao = db.getRepositoryDao()
db.runInTransaction {
// ensure somebody else hasn't updated the repo in the meantime
val currentTimestamp = repoDao.getRepository(repo.repoId)?.timestamp
if (currentTimestamp != repo.timestamp) throw ConcurrentModificationException(
"Repo timestamp expected ${repo.timestamp}, but was $currentTimestamp"
)
// still the expected timestamp, so go on processing...
streamProcessor.process(repoVersion, inputStream) { i ->
listener?.onUpdateProgress(repo, i, entryFile.numPackages)
}