From e51d2dd36abd7325c9358d0136a91b093e77a564 Mon Sep 17 00:00:00 2001 From: David Baker Date: Tue, 30 Oct 2018 11:45:19 +0000 Subject: [PATCH] Fix a few e2e backup bits * Don't _maybeSendKeyBackup() as soon as we enable them: we shouldn't have anything to send anyway until we mark all sessions for backup, which we do just afterwards, so leave that to trigger the upload (otherwise the uploading triggered by backupAll just returns straight away because a backup is already in progress). * Pass delay & retry params to _maybeSendKeyBackup(): we want the all-key upload to happen straight away so pass in delay=0, and we also don't want to retry on a timer if the the user is waiting. * If we fail due to an HTTP 400 or similar, don't swallow the error. * Use the right indexeddb store --- src/client.js | 2 -- src/crypto/index.js | 28 +++++++++++++++++----------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/client.js b/src/client.js index eb964650a..9bb259038 100644 --- a/src/client.js +++ b/src/client.js @@ -839,8 +839,6 @@ MatrixClient.prototype.enableKeyBackup = function(info) { this._crypto.backupKey.set_recipient_key(info.auth_data.public_key); this.emit('keyBackupStatus', true); - - this._crypto._maybeSendKeyBackup(); }; /** diff --git a/src/crypto/index.js b/src/crypto/index.js index 655a801dc..7f2bfdb16 100644 --- a/src/crypto/index.js +++ b/src/crypto/index.js @@ -970,16 +970,21 @@ Crypto.prototype.importRoomKeys = function(keys) { ); }; -Crypto.prototype._maybeSendKeyBackup = async function() { +Crypto.prototype._maybeSendKeyBackup = async function(delay, retry) { + if (retry === undefined) retry = true; + if (!this._sendingBackups) { this._sendingBackups = true; try { - // wait between 0 and 10 seconds, to avoid backup requests from - // different clients hitting the server all at the same time when a - // new key is sent - await new Promise((resolve, reject) => { - setTimeout(resolve, Math.random() * 10000); - }); + if (delay === undefined) { + // by default, wait between 0 and 10 seconds, to avoid backup + // requests from different clients hitting the server all at + // the same time when a new key is sent + delay = Math.random() * 10000; + } + if (delay > 0) { + await Promise.delay(delay); + } let numFailures = 0; // number of consecutive failures while (1) { if (!this.backupKey) { @@ -1034,10 +1039,11 @@ Crypto.prototype._maybeSendKeyBackup = async function() { console.log("send failed", err); if (err.httpStatus === 400 || err.httpStatus === 403 - || err.httpStatus === 401) { + || err.httpStatus === 401 + || !retry) { // retrying probably won't help much, so we should give up // FIXME: disable backups? - return; + throw err; } } if (numFailures) { @@ -1076,7 +1082,7 @@ Crypto.prototype.backupGroupSession = async function( Crypto.prototype.backupAllGroupSessions = async function(version) { await this._cryptoStore.doTxn( 'readwrite', - [IndexedDBCryptoStore.STORE_SESSIONS, IndexedDBCryptoStore.STORE_BACKUP], + [IndexedDBCryptoStore.STORE_INBOUND_GROUP_SESSIONS, IndexedDBCryptoStore.STORE_BACKUP], (txn) => { this._cryptoStore.getAllEndToEndInboundGroupSessions(txn, (session) => { if (session !== null) { @@ -1086,7 +1092,7 @@ Crypto.prototype.backupAllGroupSessions = async function(version) { }, ); - await this._maybeSendKeyBackup(); + await this._maybeSendKeyBackup(0, false); }; /* eslint-disable valid-jsdoc */ //https://github.com/eslint/eslint/issues/7307