From fff4e9e97f3577c89bfa9241cd48d0e1626acf54 Mon Sep 17 00:00:00 2001 From: Fedor Indutny <79877362+indutny-signal@users.noreply.github.com> Date: Wed, 25 Aug 2021 16:10:09 -0700 Subject: [PATCH] Simplify envelope drop due to stopped processing --- ts/test-electron/MessageReceiver_test.ts | 30 +++++++++++++++++------- ts/textsecure/MessageReceiver.ts | 22 +++++++---------- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/ts/test-electron/MessageReceiver_test.ts b/ts/test-electron/MessageReceiver_test.ts index cd02fac80303..8b1d745b8308 100644 --- a/ts/test-electron/MessageReceiver_test.ts +++ b/ts/test-electron/MessageReceiver_test.ts @@ -22,8 +22,16 @@ describe('MessageReceiver', () => { const uuid = 'aaaaaaaa-bbbb-4ccc-9ddd-eeeeeeeeeeee'; const deviceId = 1; + beforeEach(async () => { + await window.storage.protocol.hydrateCaches(); + }); + + afterEach(async () => { + await window.storage.protocol.removeAllUnprocessed(); + }); + describe('connecting', () => { - it('generates decryption-error event when it cannot decrypt', done => { + it('generates decryption-error event when it cannot decrypt', async () => { const messageReceiver = new MessageReceiver({ server: {} as WebAPIType, storage: window.storage, @@ -52,14 +60,18 @@ describe('MessageReceiver', () => { ) ); - messageReceiver.addEventListener( - 'decryption-error', - (error: DecryptionErrorEvent) => { - assert.strictEqual(error.decryptionError.senderUuid, uuid); - assert.strictEqual(error.decryptionError.senderDevice, deviceId); - done(); - } - ); + await new Promise(resolve => { + messageReceiver.addEventListener( + 'decryption-error', + (error: DecryptionErrorEvent) => { + assert.strictEqual(error.decryptionError.senderUuid, uuid); + assert.strictEqual(error.decryptionError.senderDevice, deviceId); + resolve(); + } + ); + }); + + await messageReceiver.drain(); }); }); }); diff --git a/ts/textsecure/MessageReceiver.ts b/ts/textsecure/MessageReceiver.ts index 7b38c456d3ea..4cb1d86f2bba 100644 --- a/ts/textsecure/MessageReceiver.ts +++ b/ts/textsecure/MessageReceiver.ts @@ -795,7 +795,8 @@ export default class MessageReceiver window.log.info( 'MessageReceiver.decryptAndCacheBatch storing ' + - `${decrypted.length} decrypted envelopes` + `${decrypted.length} decrypted envelopes, keeping ` + + `${failed.length} failed envelopes.` ); // Store both decrypted and failed unprocessed envelopes @@ -926,10 +927,6 @@ export default class MessageReceiver const task = createTaskWithTimeout(async (): Promise => { const unsealedEnvelope = await this.unsealEnvelope(stores, envelope); - if (!unsealedEnvelope) { - // Envelope was dropped - return { envelope, plaintext: undefined }; - } logId = this.getEnvelopeId(unsealedEnvelope); @@ -950,10 +947,7 @@ export default class MessageReceiver } else { window.log.error(...args); } - return { - plaintext: undefined, - envelope, - }; + throw error; } } @@ -1002,12 +996,12 @@ export default class MessageReceiver private async unsealEnvelope( stores: LockedStores, envelope: ProcessedEnvelope - ): Promise { + ): Promise { const logId = this.getEnvelopeId(envelope); if (this.stoppingProcessing) { - window.log.info(`MessageReceiver.unsealEnvelope(${logId}): dropping`); - return undefined; + window.log.warn(`MessageReceiver.unsealEnvelope(${logId}): dropping`); + throw new Error('Sealed envelope dropped due to stopping processing'); } if (envelope.type !== Proto.Envelope.Type.UNIDENTIFIED_SENDER) { @@ -1068,10 +1062,10 @@ export default class MessageReceiver const logId = this.getEnvelopeId(envelope); if (this.stoppingProcessing) { - window.log.info( + window.log.warn( `MessageReceiver.decryptEnvelope(${logId}): dropping unsealed` ); - return { plaintext: undefined, envelope }; + throw new Error('Unsealed envelope dropped due to stopping processing'); } if (envelope.type === Proto.Envelope.Type.RECEIPT) {