From 07701117cd6f71a8f69de2ec57e65a5bf5adb7a1 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Wed, 14 Oct 2020 12:43:35 +0200 Subject: [PATCH 1/2] reduce megolm sync cache size to 1 session as this is kept around for every e2ee room, and we only have limited olm memory --- src/matrix/e2ee/RoomEncryption.js | 2 +- src/matrix/e2ee/megolm/Decryption.js | 4 ++-- src/matrix/e2ee/megolm/decryption/SessionCache.js | 11 ++++++----- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/matrix/e2ee/RoomEncryption.js b/src/matrix/e2ee/RoomEncryption.js index 69a93e57..f47b92ed 100644 --- a/src/matrix/e2ee/RoomEncryption.js +++ b/src/matrix/e2ee/RoomEncryption.js @@ -41,7 +41,7 @@ export class RoomEncryption { this._encryptionParams = encryptionParams; this._megolmBackfillCache = this._megolmDecryption.createSessionCache(); - this._megolmSyncCache = this._megolmDecryption.createSessionCache(); + this._megolmSyncCache = this._megolmDecryption.createSessionCache(1); // session => event ids of messages we tried to decrypt and the session was missing this._missingSessions = new SessionToEventIdsMap(); // sessions that may or may not be missing, but that while diff --git a/src/matrix/e2ee/megolm/Decryption.js b/src/matrix/e2ee/megolm/Decryption.js index 09662e96..b2221028 100644 --- a/src/matrix/e2ee/megolm/Decryption.js +++ b/src/matrix/e2ee/megolm/Decryption.js @@ -41,8 +41,8 @@ export class Decryption { this._olmWorker = olmWorker; } - createSessionCache(fallback) { - return new SessionCache(fallback); + createSessionCache(size) { + return new SessionCache(size); } /** diff --git a/src/matrix/e2ee/megolm/decryption/SessionCache.js b/src/matrix/e2ee/megolm/decryption/SessionCache.js index efb7ef54..d6482a01 100644 --- a/src/matrix/e2ee/megolm/decryption/SessionCache.js +++ b/src/matrix/e2ee/megolm/decryption/SessionCache.js @@ -14,13 +14,14 @@ See the License for the specific language governing permissions and limitations under the License. */ -const CACHE_MAX_SIZE = 10; +const DEFAULT_CACHE_SIZE = 10; /** * Cache of unpickled inbound megolm session. */ export class SessionCache { - constructor() { + constructor(size) { + this._size = typeof size === "number" ? size : DEFAULT_CACHE_SIZE; this._sessions = []; } @@ -51,12 +52,12 @@ export class SessionCache { sessionInfo.retain(); // add new at top this._sessions.unshift(sessionInfo); - if (this._sessions.length > CACHE_MAX_SIZE) { + if (this._sessions.length > this._size) { // free sessions we're about to remove - for (let i = CACHE_MAX_SIZE; i < this._sessions.length; i += 1) { + for (let i = this._size; i < this._sessions.length; i += 1) { this._sessions[i].release(); } - this._sessions = this._sessions.slice(0, CACHE_MAX_SIZE); + this._sessions = this._sessions.slice(0, this._size); } } From b00db1af3e82d70eb60b95d67a0a15b1e84f4e5c Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Wed, 14 Oct 2020 12:44:27 +0200 Subject: [PATCH 2/2] only keep 20 outbound olm sessions in memory at once to prevent OOM --- src/matrix/e2ee/olm/Encryption.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/matrix/e2ee/olm/Encryption.js b/src/matrix/e2ee/olm/Encryption.js index ff5f1a8f..18bbc5fa 100644 --- a/src/matrix/e2ee/olm/Encryption.js +++ b/src/matrix/e2ee/olm/Encryption.js @@ -31,7 +31,9 @@ function findFirstSessionId(sessionIds) { const OTK_ALGORITHM = "signed_curve25519"; // only encrypt this amount of olm messages at once otherwise we run out of wasm memory // with all the sessions loaded at the same time -const MAX_BATCH_SIZE = 50; +// See https://github.com/vector-im/hydrogen-web/issues/150 as well, which indicates the limit is 44, +// but let's take a conservative limit as the megolm session cache also takes space +const MAX_BATCH_SIZE = 20; export class Encryption { constructor({account, olm, olmUtil, ownUserId, storage, now, pickleKey, senderKeyLock}) {