Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(baileys): bound the blocklist enrichment separately from the blocklist route

Yudhi Armyndharis40299abeeecc
diff --git a/src/engine/adapters/baileys-contact-blocked.spec.ts b/src/engine/adapters/baileys-contact-blocked.spec.ts
index c67a8fe59..dd6378612 100644
--- a/src/engine/adapters/baileys-contact-blocked.spec.ts
+++ b/src/engine/adapters/baileys-contact-blocked.spec.ts
@@ -207,6 +207,28 @@ describe('the blocklist memo holds under the conditions it exists for', () => {
expect(fetchBlocklist).toHaveBeenCalledTimes(2);
});
+ /**
+ * Deriving isBlocked turned a contact read from an in-memory lookup into one that AWAITS a network
+ * query. Sharing the engine's 30s budget with `/contacts/blocked` meant an unanswered blocklist —
+ * the case `withQueryDeadline` exists for, since Baileys' own query() swallows its timeout — held
+ * `GET /contacts` for half a minute. The enrichment is not the payload: past a short deadline the
+ * contacts are returned with isBlocked at its default, which is the documented degradation.
+ */
+ it('returns the contacts rather than waiting out the engine budget on a hung blocklist', async () => {
+ jest.useFakeTimers();
+ try {
+ const fetchBlocklist = jest.fn().mockImplementation(() => new Promise<string[]>(() => undefined));
+ const contacts = withQuery(fetchBlocklist);
+
+ const reading = contacts.getContacts();
+ await jest.advanceTimersByTimeAsync(6_000); // past the enrichment deadline, far short of 30s
+
+ await expect(reading).resolves.toEqual([expect.objectContaining({ id: '[email protected]', isBlocked: false })]);
+ } finally {
+ jest.useRealTimers();
+ }
+ });
+
/**
* 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
diff --git a/src/engine/adapters/baileys-contacts.ts b/src/engine/adapters/baileys-contacts.ts
index 72cd14867..058692191 100644
--- a/src/engine/adapters/baileys-contacts.ts
+++ b/src/engine/adapters/baileys-contacts.ts
@@ -119,11 +119,11 @@ export class BaileysContacts {
* a claim about the account sold in place of a transport failure. Wire items without a jid attr
* are dropped rather than reported as "undefined".
*/
- async getBlockedContacts(): Promise<string[]> {
+ async getBlockedContacts(budgetMs: number = this.queryBudgetMs): Promise<string[]> {
this.host.ensureReady();
const jids = await withQueryDeadline(
this.sock().fetchBlocklist(),
- this.queryBudgetMs,
+ budgetMs,
'WhatsApp did not answer the blocklist query in time',
);
return (jids ?? []).filter((jid): jid is string => Boolean(jid)).map(jid => this.host.toNeutralJid(jid));
@@ -211,6 +211,15 @@ export class BaileysContacts {
*/
private static readonly BLOCKLIST_MEMO_MS = 5_000;
+ /**
+ * Deadline for the blocklist query when it is ENRICHING a contact read, deliberately far below the
+ * engine-wide budget `/contacts/blocked` uses. There the blocklist is the answer and waiting is
+ * right; here it is one field on rows the caller already has, and a contact read used to be an
+ * in-memory lookup. Past this the read returns with isBlocked at its default and a warning —
+ * the same degradation a failed query already produces.
+ */
+ private static readonly BLOCKLIST_ENRICHMENT_BUDGET_MS = 5_000;
+
/**
* The account's blocked ids, or null when the query failed.
*
@@ -240,7 +249,7 @@ export class BaileysContacts {
const generation = this.blocklistGeneration;
let ids: Set<string> | null;
try {
- ids = new Set(await this.getBlockedContacts());
+ ids = new Set(await this.getBlockedContacts(BaileysContacts.BLOCKLIST_ENRICHMENT_BUDGET_MS));
} catch (error) {
this.host.logger.warn('Blocklist unavailable; contact isBlocked left at its default', {
error: error instanceof Error ? error.message : String(error),