Skip to content

Commit

Permalink
fix uncaught exceptions in Backup Loop for rust sdk (#3907)
Browse files Browse the repository at this point in the history
* fix uncaught exceptions

* Update src/rust-crypto/backup.ts

Co-authored-by: Florian Duros <[email protected]>

---------

Co-authored-by: Florian Duros <[email protected]>
  • Loading branch information
BillCarsonFr and florianduros authored Nov 29, 2023
1 parent 6891152 commit 3f246c6
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions src/rust-crypto/backup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ export class RustBackupManager extends TypedEventEmitter<RustBackupCryptoEvents,
}
this.backupKeysLoopRunning = true;

logger.log(`Starting loop for ${this.activeBackupVersion}.`);
logger.log(`Backup: Starting keys upload loop for backup version:${this.activeBackupVersion}.`);

// wait between 0 and `maxDelay` seconds, to avoid backup
// requests from different clients hitting the server all at
Expand All @@ -273,28 +273,41 @@ export class RustBackupManager extends TypedEventEmitter<RustBackupCryptoEvents,

while (!this.stopped) {
// Get a batch of room keys to upload
const request: RustSdkCryptoJs.KeysBackupRequest | null = await this.olmMachine.backupRoomKeys();
let request: RustSdkCryptoJs.KeysBackupRequest | null = null;
try {
request = await this.olmMachine.backupRoomKeys();
} catch (err) {
logger.error("Backup: Failed to get keys to backup from rust crypto-sdk", err);
}

if (!request || this.stopped || !this.activeBackupVersion) {
logger.log(`Ending loop for ${this.activeBackupVersion}.`);
logger.log(`Backup: Ending loop for version ${this.activeBackupVersion}.`);
return;
}

try {
await this.outgoingRequestProcessor.makeOutgoingRequest(request);
numFailures = 0;

if (this.stopped) break;
const keyCount: RustSdkCryptoJs.RoomKeyCounts = await this.olmMachine.roomKeyCounts();
const remaining = keyCount.total - keyCount.backedUp;
this.emit(CryptoEvent.KeyBackupSessionsRemaining, remaining);
try {
const keyCount = await this.olmMachine.roomKeyCounts();
const remaining = keyCount.total - keyCount.backedUp;
this.emit(CryptoEvent.KeyBackupSessionsRemaining, remaining);
} catch (err) {
logger.error("Backup: Failed to get key counts from rust crypto-sdk", err);
}
} catch (err) {
numFailures++;
logger.error("Error processing backup request for rust crypto-sdk", err);
logger.error("Backup: Error processing backup request for rust crypto-sdk", err);
if (err instanceof MatrixError) {
const errCode = err.data.errcode;
if (errCode == "M_NOT_FOUND" || errCode == "M_WRONG_ROOM_KEYS_VERSION") {
await this.disableKeyBackup();
logger.log(`Backup: Failed to upload keys to current vesion: ${errCode}.`);
try {
await this.disableKeyBackup();
} catch (error) {
logger.error("Backup: An error occurred while disabling key backup:", error);
}
this.emit(CryptoEvent.KeyBackupFailed, err.data.errcode!);
// There was an active backup and we are out of sync with the server
// force a check server side
Expand Down

0 comments on commit 3f246c6

Please sign in to comment.