Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(baileys): count changes waiting for an emit apart

Yudhi Armyndharisa06136230800
diff --git a/src/engine/adapters/baileys-events.ts b/src/engine/adapters/baileys-events.ts
index b4af26edd..9479e5db8 100644
--- a/src/engine/adapters/baileys-events.ts
+++ b/src/engine/adapters/baileys-events.ts
@@ -358,11 +358,14 @@ export class BaileysEvents {
{ announce: Array<() => void>; timer: ReturnType<typeof setTimeout> }
>();
+ /** How many changes wait for a delivery or its outcome (see parkUntil); the rest are judged at once. */
+ private changesParked = 0;
+
/**
- * How many changes wait for a delivery, its outcome (see parkUntil) or its emit (see changesAwaitingEmit); the
- * rest are judged, or announced, at once.
+ * How many changes wait in changesAwaitingEmit; the rest are announced at once. Counted apart from the parks,
+ * so changes waiting for their message cannot make a change that has to wait for an outcome be dropped.
*/
- private changesParked = 0;
+ private changesAwaiting = 0;
private static readonly CHANGES_PARKED_LIMIT = 64;
@@ -902,7 +905,8 @@ export class BaileysEvents {
* right after the emit instead, in the order the changes arrived, when the session holds the message. A delete
* (`behindChangesOnly`) never waits for the message: it waits only behind the changes already waiting, so they
* are announced in the order they arrived. A change waits at most the three media deadlines a change aimed at a
- * delivery in flight waits (see targetsForeignMessage), and no more than CHANGES_PARKED_LIMIT wait at once.
+ * delivery in flight waits (see targetsForeignMessage), and no more than CHANGES_PARKED_LIMIT wait at once, the
+ * limit of those changes.
*/
private announceWhenHandedOn(
messageId: string | null | undefined,
@@ -912,11 +916,11 @@ export class BaileysEvents {
): void {
const waiting = messageId && !ticket ? this.changesAwaitingEmit.get(messageId) : undefined;
const waits = !!waiting || (!behindChangesOnly && !ticket && !!messageId && this.inboundInFlight.has(messageId));
- if (!messageId || !waits || this.changesParked >= BaileysEvents.CHANGES_PARKED_LIMIT) {
+ if (!messageId || !waits || this.changesAwaiting >= BaileysEvents.CHANGES_PARKED_LIMIT) {
announce();
return;
}
- this.changesParked++;
+ this.changesAwaiting++;
if (waiting) {
waiting.announce.push(announce);
return;
@@ -932,7 +936,7 @@ export class BaileysEvents {
if (!messageId || !waiting) return;
this.changesAwaitingEmit.delete(messageId);
clearTimeout(waiting.timer);
- this.changesParked -= waiting.announce.length;
+ this.changesAwaiting -= waiting.announce.length;
for (const announce of waiting.announce) {
try {
announce();
diff --git a/src/engine/adapters/inbound-hold.spec.ts b/src/engine/adapters/inbound-hold.spec.ts
index 969885f5d..3a891d90c 100644
--- a/src/engine/adapters/inbound-hold.spec.ts
+++ b/src/engine/adapters/inbound-hold.spec.ts
@@ -1422,6 +1422,32 @@ describe('Baileys inbound messages held for storage', () => {
expect(internals(events)).toEqual({ waiting: 0, queued: 0 });
});
+ // The limit is the one of the changes that wait for an outcome, but counted apart: a change that has to wait for
+ // the answer of the session is held, not dropped, whatever number of others wait for their message.
+ it('keeps a change that waits for the answer of the session held while 64 others wait for their message', async () => {
+ const OTHER = '[email protected]';
+ const h = heldProjector({ unique: true, sqlite: true });
+ h.rows.push({ waMessageId: 'X', chatId: CHAT, direction: 'incoming', body: 'the real one' });
+ const answer = h.stall('X'); // the row of the delivery of X waits, so the session has not answered yet
+ const { events } = build(4, h.callbacks, memoryStore());
+ const release = download();
+ upsert(events, media('M', CHAT), text('X', OTHER));
+ await flush();
+ upsert(events, ...Array.from({ length: 64 }, (_, i) => reaction(`R${i}`, 'M', `r${i}`)));
+ await flush();
+ expect(h.announcedAs('message.reaction')).toEqual([]);
+
+ // The real edit of X reads as foreign to the copy the delivery stored, and waits for the answer.
+ upsert(events, edit('E', 'X', 'fixed'));
+ await flush();
+ answer();
+ release();
+ await flush();
+
+ expect(h.rows.find(r => r.waMessageId === 'X')).toEqual(expect.objectContaining({ chatId: CHAT, body: 'fixed' }));
+ expect(h.announcedAs('message.reaction')).toHaveLength(64);
+ });
+
it('announces the other changes, and does not process the message again, when a consumer throws', async () => {
jest.spyOn(console, 'warn').mockImplementation(() => undefined);
const h = heldProjector({ unique: true, sqlite: true });
@@ -1459,8 +1485,8 @@ describe('Baileys inbound messages held for storage', () => {
/** How many changes wait, and for how many messages. */
const internals = (events: BaileysEvents) => {
- const state = events as unknown as { changesParked: number; changesAwaitingEmit: Map<string, unknown> };
- return { waiting: state.changesParked, queued: state.changesAwaitingEmit?.size ?? 0 };
+ const state = events as unknown as { changesAwaiting: number; changesAwaitingEmit: Map<string, unknown> };
+ return { waiting: state.changesAwaiting, queued: state.changesAwaitingEmit?.size ?? 0 };
};
});
});