fix: Remove potential race conditions and database issues with OTK upload

This commit is contained in:
Sorunome 2020-09-10 12:17:38 +02:00
parent b5ac500136
commit 2c7ae759f8
No known key found for this signature in database
GPG key ID: B19471D07FC9BE9C
2 changed files with 63 additions and 52 deletions

View file

@ -68,7 +68,7 @@ class Encryption {
} }
void handleDeviceOneTimeKeysCount(Map<String, int> countJson) { void handleDeviceOneTimeKeysCount(Map<String, int> countJson) {
olmManager.handleDeviceOneTimeKeysCount(countJson); Zone.root.run(() => olmManager.handleDeviceOneTimeKeysCount(countJson));
} }
void onSync() { void onSync() {

View file

@ -130,6 +130,8 @@ class OlmManager {
return isValid; return isValid;
} }
bool _uploadKeysLock = false;
/// Generates new one time keys, signs everything and upload it to the server. /// Generates new one time keys, signs everything and upload it to the server.
Future<bool> uploadKeys( Future<bool> uploadKeys(
{bool uploadDeviceKeys = false, int oldKeyCount = 0}) async { {bool uploadDeviceKeys = false, int oldKeyCount = 0}) async {
@ -137,6 +139,12 @@ class OlmManager {
return true; return true;
} }
if (_uploadKeysLock) {
return false;
}
_uploadKeysLock = true;
try {
// generate one-time keys // generate one-time keys
// we generate 2/3rds of max, so that other keys people may still have can // we generate 2/3rds of max, so that other keys people may still have can
// still be used // still be used
@ -193,6 +201,9 @@ class OlmManager {
_olmAccount.mark_keys_as_published(); _olmAccount.mark_keys_as_published();
await client.database?.updateClientKeys(pickledOlmAccount, client.id); await client.database?.updateClientKeys(pickledOlmAccount, client.id);
return response['signed_curve25519'] == oneTimeKeysCount; return response['signed_curve25519'] == oneTimeKeysCount;
} finally {
_uploadKeysLock = false;
}
} }
void handleDeviceOneTimeKeysCount(Map<String, int> countJson) { void handleDeviceOneTimeKeysCount(Map<String, int> countJson) {