Skip to content

Commit

Permalink
Merge pull request #1585 from famedly/krille/fix-upload-keys
Browse files Browse the repository at this point in the history
refactor: Trigger upload keys on sync and not in background job and u…
  • Loading branch information
krille-chan authored Oct 20, 2023
2 parents 31a52cb + 5c3c85b commit be1f125
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 27 deletions.
22 changes: 2 additions & 20 deletions lib/encryption/encryption.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,8 @@ class Encryption {
olmAccount: olmAccount,
deviceId: isDehydratedDevice ? deviceId : ourDeviceId,
pickleKey: pickleKey);
_backgroundTasksRunning = ourDeviceId ==
client.deviceID; // Don't run tasks for dehydrated devices
_backgroundTasks(); // start the background tasks

if (!isDehydratedDevice) keyManager.startAutoUploadKeys();

This comment has been minimized.

Copy link
@TheOneWithTheBraid

TheOneWithTheBraid Nov 24, 2023

Contributor

This change is breaking our automated key upload iirc. Could you elaborate on how we are best supposed to workarround this ? Should we just manually start the auto key upload ?

}

bool isMinOlmVersion(int major, int minor, int patch) {
Expand Down Expand Up @@ -420,24 +419,7 @@ class Encryption {
}
}

// this method is responsible for all background tasks, such as uploading online key backups
bool _backgroundTasksRunning = true;
void _backgroundTasks() {
if (!_backgroundTasksRunning || !client.isLogged()) {
return;
}

keyManager.backgroundTasks();

// autovalidateMasterOwnKey();

if (_backgroundTasksRunning) {
Timer(Duration(seconds: 10), _backgroundTasks);
}
}

Future<void> dispose() async {
_backgroundTasksRunning = false;
keyManager.dispose();
await olmManager.dispose();
keyVerificationManager.dispose();
Expand Down
32 changes: 27 additions & 5 deletions lib/encryption/key_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -747,15 +747,34 @@ class KeyManager {
}
}

bool _isUploadingKeys = false;
Future<void>? _uploadingFuture;

Future<void> backgroundTasks() async {
void startAutoUploadKeys() {
_uploadKeysOnSync = encryption.client.onSync.stream
.listen((_) => uploadInboundGroupSessions(skipIfInProgress: true));
}

/// This task should be performed after sync processing but should not block
/// the sync. To make sure that it never gets executed multiple times, it is
/// skipped when an upload task is already in progress. Set `skipIfInProgress`
/// to `false` to await the pending upload task instead.
Future<void> uploadInboundGroupSessions(
{bool skipIfInProgress = false}) async {
final database = client.database;
final userID = client.userID;
if (_isUploadingKeys || database == null || userID == null) {
if (database == null || userID == null) {
return;
}
_isUploadingKeys = true;

// Make sure to not run in parallel
if (_uploadingFuture != null) {
if (skipIfInProgress) return;
await _uploadingFuture;
}
final completer = Completer<void>();
_uploadingFuture = completer.future;

await client.userDeviceKeysLoading;
try {
if (!(await isCached())) {
return; // we can't backup anyways
Expand Down Expand Up @@ -817,7 +836,7 @@ class KeyManager {
} catch (e, s) {
Logs().e('[Key Manager] Error uploading room keys', e, s);
} finally {
_isUploadingKeys = false;
completer.complete();
}
}

Expand Down Expand Up @@ -1017,7 +1036,10 @@ class KeyManager {
}
}

StreamSubscription<SyncUpdate>? _uploadKeysOnSync;

void dispose() {
_uploadKeysOnSync?.cancel();
for (final sess in _outboundGroupSessions.values) {
sess.dispose();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/encryption/utils/bootstrap.dart
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ class Bootstrap {
'And finally set all megolm keys as needing to be uploaded again...');
await client.database?.markInboundGroupSessionsAsNeedingUpload();
Logs().v('And uploading keys...');
await client.encryption?.keyManager.backgroundTasks();
await client.encryption?.keyManager.uploadInboundGroupSessions();
} catch (e, s) {
Logs().e('[Bootstrapping] Error setting up online key backup', e, s);
state = BootstrapState.error;
Expand Down
6 changes: 6 additions & 0 deletions lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,9 @@ class Client extends MatrixApi {
/// including all persistent data from the store.
@override
Future<void> logout() async {
// Upload keys to make sure all are cached on the next login.
await encryption?.keyManager.uploadInboundGroupSessions();

try {
await super.logout();
} catch (e, s) {
Expand All @@ -567,6 +570,9 @@ class Client extends MatrixApi {
/// including all persistent data from the store.
@override
Future<void> logoutAll() async {
// Upload keys to make sure all are cached on the next login.
await encryption?.keyManager.uploadInboundGroupSessions();

final futures = <Future>[];
futures.add(super.logoutAll());
futures.add(clear());
Expand Down
2 changes: 1 addition & 1 deletion test/encryption/online_key_backup_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ void main() {
forwarded: true);
var dbSessions = await client.database!.getInboundGroupSessionsToUpload();
expect(dbSessions.isNotEmpty, true);
await client.encryption!.keyManager.backgroundTasks();
await client.encryption!.keyManager.uploadInboundGroupSessions();
await FakeMatrixApi.firstWhereValue(
'/client/v3/room_keys/keys?version=5');
final payload = FakeMatrixApi
Expand Down

0 comments on commit be1f125

Please sign in to comment.