1
0
mirror of https://github.com/etesync/android synced 2024-11-22 07:58:09 +00:00

Add a lock when pushing changes.

This is not needed for proper deployments where the server properly locks
the database tables. But in some older etesync server versions and possibly
bad databases, this is not the case.
OpenTasks causes EteSync to concurrently try to sync, so this at least makes
sure this race condition doesn't happen for these rare cases.
This commit is contained in:
Tom Hacohen 2019-11-26 12:47:45 +02:00
parent e83c95e8ec
commit 63cbaddfc6

View File

@ -33,8 +33,10 @@ import java.io.Closeable
import java.io.FileNotFoundException import java.io.FileNotFoundException
import java.io.IOException import java.io.IOException
import java.util.* import java.util.*
import java.util.concurrent.locks.ReentrantLock
import java.util.logging.Level import java.util.logging.Level
import javax.net.ssl.SSLHandshakeException import javax.net.ssl.SSLHandshakeException
import kotlin.concurrent.withLock
abstract class SyncManager<T: LocalResource<*>> @Throws(Exceptions.IntegrityException::class, Exceptions.GenericCryptoException::class) abstract class SyncManager<T: LocalResource<*>> @Throws(Exceptions.IntegrityException::class, Exceptions.GenericCryptoException::class)
constructor(protected val context: Context, protected val account: Account, protected val settings: AccountSettings, protected val extras: Bundle, protected val authority: String, protected val syncResult: SyncResult, journalUid: String, protected val serviceType: CollectionInfo.Type, accountName: String): Closeable { constructor(protected val context: Context, protected val account: Account, protected val settings: AccountSettings, protected val extras: Bundle, protected val authority: String, protected val syncResult: SyncResult, journalUid: String, protected val serviceType: CollectionInfo.Type, accountName: String): Closeable {
@ -393,8 +395,11 @@ constructor(protected val context: Context, protected val account: Account, prot
// FIXME: Deal with failure (someone else uploaded before we go here) // FIXME: Deal with failure (someone else uploaded before we go here)
try { try {
if (!localEntries!!.isEmpty()) { if (!localEntries!!.isEmpty()) {
val entries = localEntries val entries = localEntries!!
journal!!.create(entries!!, remoteCTag) pushLock.withLock {
journal!!.create(entries, remoteCTag)
}
// Persist the entries after they've been pushed // Persist the entries after they've been pushed
for (entry in entries) { for (entry in entries) {
val cEntry = SyncEntry.fromJournalEntry(crypto, entry) val cEntry = SyncEntry.fromJournalEntry(crypto, entry)
@ -577,5 +582,7 @@ constructor(protected val context: Context, protected val account: Account, prot
companion object { companion object {
private val MAX_FETCH = 50 private val MAX_FETCH = 50
private val MAX_PUSH = 30 private val MAX_PUSH = 30
private val pushLock = ReentrantLock()
} }
} }