diff --git a/src/crypto/backup.ts b/src/crypto/backup.ts index d240bdab83c..fb4d6bd6e94 100644 --- a/src/crypto/backup.ts +++ b/src/crypto/backup.ts @@ -118,12 +118,21 @@ export class BackupManager { public checkedForBackup: boolean; // Have we checked the server for a backup we can use? private sendingBackups: boolean; // Are we currently sending backups? private sessionLastCheckAttemptedTime: Record = {}; // When did we last try to check the server for a given session id? + // The backup manager will schedule backup of keys when active (`scheduleKeyBackupSend`), this allows cancel when client is stopped + private clientRunning = true; public constructor(private readonly baseApis: MatrixClient, public readonly getKey: GetKey) { this.checkedForBackup = false; this.sendingBackups = false; } + /** + * Stop the backup manager from backing up keys and allow a clean shutdown. + */ + public stop(): void { + this.clientRunning = false; + } + public get version(): string | undefined { return this.backupInfo && this.backupInfo.version; } @@ -439,6 +448,10 @@ export class BackupManager { // the same time when a new key is sent const delay = Math.random() * maxDelay; await sleep(delay); + if (!this.clientRunning) { + logger.debug("Key backup send aborted, client stopped"); + return; + } let numFailures = 0; // number of consecutive failures for (;;) { if (!this.algorithm) { @@ -473,6 +486,11 @@ export class BackupManager { // exponential backoff if we have failures await sleep(1000 * Math.pow(2, Math.min(numFailures - 1, 4))); } + + if (!this.clientRunning) { + logger.debug("Key backup send loop aborted, client stopped"); + return; + } } } finally { this.sendingBackups = false; diff --git a/src/crypto/index.ts b/src/crypto/index.ts index ff3410dea07..6610f8621e2 100644 --- a/src/crypto/index.ts +++ b/src/crypto/index.ts @@ -1832,6 +1832,7 @@ export class Crypto extends TypedEventEmitter