From c3754fdbd4ef8b1019b6ac17736ffab087f33844 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Wed, 2 Jun 2021 12:34:02 +0200 Subject: [PATCH] fix the race, make test succeed --- src/matrix/room/sending/SendQueue.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/matrix/room/sending/SendQueue.js b/src/matrix/room/sending/SendQueue.js index 446e0ce8..908de836 100644 --- a/src/matrix/room/sending/SendQueue.js +++ b/src/matrix/room/sending/SendQueue.js @@ -31,6 +31,7 @@ export class SendQueue { this._isSending = false; this._offline = false; this._roomEncryption = null; + this._currentQueueIndex = 0; } _createPendingEvent(data, attachments = null) { @@ -55,6 +56,7 @@ export class SendQueue { await log.wrap("send event", async log => { log.set("queueIndex", pendingEvent.queueIndex); try { + this._currentQueueIndex = pendingEvent.queueIndex; await this._sendEvent(pendingEvent, log); } catch(err) { if (err instanceof ConnectionError) { @@ -75,6 +77,8 @@ export class SendQueue { pendingEvent.setError(err); } } + } finally { + this._currentQueueIndex = 0; } }); } @@ -274,7 +278,11 @@ export class SendQueue { let pendingEvent; try { const pendingEventsStore = txn.pendingEvents; - const maxQueueIndex = await pendingEventsStore.getMaxQueueIndex(this._roomId) || 0; + const maxStorageQueueIndex = await pendingEventsStore.getMaxQueueIndex(this._roomId) || 0; + // don't use the queueIndex of the pendingEvent currently waiting for /send to return + // if the remote echo already removed the pendingEvent in storage, as the send loop + // wouldn't be able to detect the remote echo already arrived and end up overwriting the new event + const maxQueueIndex = Math.max(maxStorageQueueIndex, this._currentQueueIndex); const queueIndex = maxQueueIndex + 1; const needsEncryption = eventType !== REDACTION_TYPE && !!this._roomEncryption; pendingEvent = this._createPendingEvent({