make flushing room key shares reentrant

as it can be called from Room.start while still running.
This commit is contained in:
Bruno Windels 2020-09-25 10:45:00 +02:00
parent bcca23bac9
commit 134b04c782

View file

@ -54,6 +54,7 @@ export class RoomEncryption {
this._notifyMissingMegolmSession = notifyMissingMegolmSession; this._notifyMissingMegolmSession = notifyMissingMegolmSession;
this._clock = clock; this._clock = clock;
this._disposed = false; this._disposed = false;
this._isFlushingRoomKeyShares = false;
} }
async enableSessionBackup(sessionBackup) { async enableSessionBackup(sessionBackup) {
@ -321,25 +322,34 @@ export class RoomEncryption {
} }
async flushPendingRoomKeyShares(hsApi, operations = null) { async flushPendingRoomKeyShares(hsApi, operations = null) {
if (!operations) { // this has to be reentrant as it can be called from Room.start while still running
const txn = await this._storage.readTxn([this._storage.storeNames.operations]); if (this._isFlushingRoomKeyShares) {
operations = await txn.operations.getAllByTypeAndScope("share_room_key", this._room.id); return;
} }
for (const operation of operations) { this._isFlushingRoomKeyShares = true;
// just to be sure try {
if (operation.type !== "share_room_key") { if (!operations) {
continue; const txn = await this._storage.readTxn([this._storage.storeNames.operations]);
operations = await txn.operations.getAllByTypeAndScope("share_room_key", this._room.id);
} }
const devices = await this._deviceTracker.devicesForRoomMembers(this._room.id, operation.userIds, hsApi); for (const operation of operations) {
await this._sendRoomKey(operation.roomKeyMessage, devices, hsApi); // just to be sure
const removeTxn = await this._storage.readWriteTxn([this._storage.storeNames.operations]); if (operation.type !== "share_room_key") {
try { continue;
removeTxn.operations.remove(operation.id); }
} catch (err) { const devices = await this._deviceTracker.devicesForRoomMembers(this._room.id, operation.userIds, hsApi);
removeTxn.abort(); await this._sendRoomKey(operation.roomKeyMessage, devices, hsApi);
throw err; const removeTxn = await this._storage.readWriteTxn([this._storage.storeNames.operations]);
try {
removeTxn.operations.remove(operation.id);
} catch (err) {
removeTxn.abort();
throw err;
}
await removeTxn.complete();
} }
await removeTxn.complete(); } finally {
this._isFlushingRoomKeyShares = false;
} }
} }