From 13f20cdd26dd47cc0ccd38aa3d71667ee2bfb2e9 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Fri, 9 Apr 2021 10:37:43 +0200 Subject: [PATCH 1/2] wait for running key share operation in encrypt --- src/matrix/e2ee/RoomEncryption.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/matrix/e2ee/RoomEncryption.js b/src/matrix/e2ee/RoomEncryption.js index ddb6e4c4..88bacbf4 100644 --- a/src/matrix/e2ee/RoomEncryption.js +++ b/src/matrix/e2ee/RoomEncryption.js @@ -46,6 +46,7 @@ export class RoomEncryption { this._clock = clock; this._isFlushingRoomKeyShares = false; this._lastKeyPreShareTime = null; + this._keySharePromise = null; this._disposed = false; } @@ -265,13 +266,26 @@ export class RoomEncryption { return; } this._lastKeyPreShareTime = this._clock.createMeasure(); - const roomKeyMessage = await this._megolmEncryption.ensureOutboundSession(this._room.id, this._encryptionParams); - if (roomKeyMessage) { - await log.wrap("share key", log => this._shareNewRoomKey(roomKeyMessage, hsApi, log)); + try { + this._keySharePromise = (async () => { + const roomKeyMessage = await this._megolmEncryption.ensureOutboundSession(this._room.id, this._encryptionParams); + if (roomKeyMessage) { + await log.wrap("share key", log => this._shareNewRoomKey(roomKeyMessage, hsApi, log)); + } + })(); + await this._keySharePromise; + } finally { + this._keySharePromise = null; } } async encrypt(type, content, hsApi, log) { + // ensureMessageKeyIsShared is still running, + // wait for it to create and share a key if needed + if (this._keySharePromise) { + log.set("waitForRunningKeyShare", true); + await this._keySharePromise; + } const megolmResult = await log.wrap("megolm encrypt", () => this._megolmEncryption.encrypt(this._room.id, type, content, this._encryptionParams)); if (megolmResult.roomKeyMessage) { log.wrapDetached("share key", log => this._shareNewRoomKey(megolmResult.roomKeyMessage, hsApi, log)); From e17fb24bd8a5b8c964e413d4a678fe5f53ca9320 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Fri, 9 Apr 2021 10:37:55 +0200 Subject: [PATCH 2/2] also await sharing the key in encrypt --- src/matrix/e2ee/RoomEncryption.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/matrix/e2ee/RoomEncryption.js b/src/matrix/e2ee/RoomEncryption.js index 88bacbf4..721be2d0 100644 --- a/src/matrix/e2ee/RoomEncryption.js +++ b/src/matrix/e2ee/RoomEncryption.js @@ -288,7 +288,7 @@ export class RoomEncryption { } const megolmResult = await log.wrap("megolm encrypt", () => this._megolmEncryption.encrypt(this._room.id, type, content, this._encryptionParams)); if (megolmResult.roomKeyMessage) { - log.wrapDetached("share key", log => this._shareNewRoomKey(megolmResult.roomKeyMessage, hsApi, log)); + await log.wrap("share key", log => this._shareNewRoomKey(megolmResult.roomKeyMessage, hsApi, log)); } return { type: ENCRYPTED_TYPE,