From df1e400e03ace976f2c2797cf438c4c74c503bd8 Mon Sep 17 00:00:00 2001 From: HUGEIT <94182206+HUGEIT@users.noreply.github.com> Date: Tue, 25 Jan 2022 13:54:42 +0100 Subject: [PATCH] sendRetryRequest ("Waiting for this message") wrapped in mutex call to avoid crash in #1152 --- src/Socket/messages-recv.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Socket/messages-recv.ts b/src/Socket/messages-recv.ts index 554f7a1..1c5db5b 100644 --- a/src/Socket/messages-recv.ts +++ b/src/Socket/messages-recv.ts @@ -3,7 +3,7 @@ import { proto } from '../../WAProto' import { KEY_BUNDLE_TYPE } from '../Defaults' import { Chat, GroupMetadata, MessageUserReceipt, ParticipantAction, SocketConfig, WAMessageStubType } from '../Types' import { decodeMessageStanza, downloadAndProcessHistorySyncNotification, encodeBigEndian, generateSignalPubKey, toNumber, xmppPreKey, xmppSignedPreKey } from '../Utils' -import { makeKeyedMutex } from '../Utils/make-mutex' +import { makeKeyedMutex, makeMutex } from '../Utils/make-mutex' import { areJidsSameUser, BinaryNode, BinaryNodeAttributes, getAllBinaryNodeChildren, getBinaryNodeChildren, isJidGroup, jidDecode, jidEncode, jidNormalizedUser } from '../WABinary' import { makeChatsSocket } from './chats' import { extractGroupMetadata } from './groups' @@ -38,9 +38,12 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => { resyncMainAppState, } = sock - /** the mutex ensures that the notifications (receipts, messages etc.) are processed in order */ + /** this mutex ensures that the notifications (receipts, messages etc.) are processed in order */ const processingMutex = makeKeyedMutex() + /** this mutex ensures that each retryRequest will wait for the previous one to finish */ + const retryMutex = makeMutex() + const msgRetryMap = config.msgRetryCounterMap || { } const historyCache = new Set() @@ -354,7 +357,9 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => { { msgId: msg.key.id, params: msg.messageStubParameters }, 'failure in decrypting message' ) - await sendRetryRequest(stanza) + retryMutex.mutex( + async () => await sendRetryRequest(stanza) + ) } else { await sendMessageAck(stanza, { class: 'receipt' }) // no type in the receipt => message delivered