From 9e3c2082de288ad680224e32ae3098cc029b50bf Mon Sep 17 00:00:00 2001 From: Adhiraj Singh Date: Mon, 7 Nov 2022 17:21:32 +0530 Subject: [PATCH] fix: deadlock on processNodeWithBuffer --- src/Socket/messages-recv.ts | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/Socket/messages-recv.ts b/src/Socket/messages-recv.ts index b725ce1..862e1b1 100644 --- a/src/Socket/messages-recv.ts +++ b/src/Socket/messages-recv.ts @@ -546,7 +546,6 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => { await sendReceipt(msg.key.remoteJid!, participant!, [msg.key.id!], type) - // send ack for history message const isAnyHistoryMsg = getHistoryMsg(msg.message!) if(isAnyHistoryMsg) { @@ -619,14 +618,26 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => { /// processes a node with the given function /// and adds the task to the existing buffer if we're buffering events - const processNodeWithBuffer = ( + const processNodeWithBuffer = async( node: BinaryNode, identifier: string, exec: (node: BinaryNode) => Promise ) => { - const task = exec(node) - .catch(err => onUnexpectedError(err, identifier)) - ev.processInBuffer(task) + const started = ev.buffer() + if(started) { + await execTask() + if(started) { + await ev.flush() + } + } else { + const task = execTask() + ev.processInBuffer(task) + } + + function execTask() { + return exec(node) + .catch(err => onUnexpectedError(err, identifier)) + } } // called when all offline notifs are handled