Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(baileys): an overtaken blocklist query must not clear the successor's handle
Yudhi Armyndharisc4fe4356a2d4
diff --git a/src/engine/adapters/baileys-contact-blocked.spec.ts b/src/engine/adapters/baileys-contact-blocked.spec.tsindex a1564ad83..c67a8fe59 100644--- a/src/engine/adapters/baileys-contact-blocked.spec.ts+++ b/src/engine/adapters/baileys-contact-blocked.spec.ts@@ -207,6 +207,37 @@ describe('the blocklist memo holds under the conditions it exists for', () => {expect(fetchBlocklist).toHaveBeenCalledTimes(2);});+ /**+ * The in-flight handle is a single field, so the query that clears it must be the one that owns+ * it. After an invalidation the field belongs to the query started AFTER the block — an earlier+ * query settling later cleared that newer handle, and the next reader opened a third query while+ * the second was still open. The leak is precisely in the window the in-flight sharing created.+ */+ it('an overtaken query does not clear the handle of the one that replaced it', async () => {+ const gates: Array<() => void> = [];+ const openGate = (): Promise<string[]> =>+ new Promise<string[]>(resolve => {+ gates.push(() => resolve([]));+ });+ const fetchBlocklist = jest.fn().mockImplementation(openGate);+ const contacts = withQuery(fetchBlocklist);++ const first = contacts.getContactById('[email protected]'); // Q1 owns the handle+ await contacts.blockContact('[email protected]'); // invalidates: handle cleared, generation bumped+ const second = contacts.getContactById('[email protected]'); // Q2 takes the handle+ expect(fetchBlocklist).toHaveBeenCalledTimes(2);++ gates[0](); // Q1 settles late — it must not touch Q2's handle+ await first;++ // Q2 is still open, so this reader has to JOIN it rather than open a third query.+ const third = contacts.getContactById('[email protected]');+ expect(fetchBlocklist).toHaveBeenCalledTimes(2);++ gates[1]();+ await Promise.all([second, third]);+ });+it('collapses a burst of concurrent reads onto one query', async () => {let release: () => void = () => undefined;const gate = new Promise<void>(resolve => {diff --git a/src/engine/adapters/baileys-contacts.ts b/src/engine/adapters/baileys-contacts.tsindex 5c5644631..8448769a6 100644--- a/src/engine/adapters/baileys-contacts.ts+++ b/src/engine/adapters/baileys-contacts.ts@@ -245,7 +245,12 @@ export class BaileysContacts {});ids = null;} finally {- this.blocklistInFlight = undefined;+ // Only if the handle is still this query's. After an invalidation the field belongs to a query+ // started later, and clearing it there would send the next reader into a third query while the+ // second was still open — defeating the sharing exactly inside the window it introduced.+ if (generation === this.blocklistGeneration) {+ this.blocklistInFlight = undefined;+ }}// Stamped with the time the ANSWER arrived, not the time the query started. Stamping the start// wrote an already-expired memo for any query slower than the window — so the memo absorbed