Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add stop() api to BackupManager for clean shutdown #3553

Merged
merged 1 commit into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/crypto/backup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, number> = {}; // 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;
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/crypto/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1832,6 +1832,7 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
this.outgoingRoomKeyRequestManager.stop();
this.deviceList.stop();
this.dehydrationManager.stop();
this.backupManager.stop();
}

/**
Expand Down