Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(engine): stop a whatsapp-web.js media burst shedding past the eighth
Yudhi Armyndharis9b9760694b42
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex b07668a1e..4b7231a5c 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -35,6 +35,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0- `check:sdk-coverage` no longer passes when a client drops a route entirely: a wildcard the client built for a path parameter — `contacts/${contactId}` — was read as a one-builder family and stood in for every concrete sibling under it, so removing `contacts/blocked` from a client left the gate green. A wildcard now counts as a family only where the contract publishes no route of that shape.- The Go SDK no longer approves or rejects every pending join request when handed an empty participant list: `omitempty` dropped the empty slice, so `[]string{}` reached the gateway as the bodyless "act on every request" rather than the `400` the other four clients receive. A nil slice still means every request.- Approving or rejecting a join request by bare phone number works on the whatsapp-web.js engine instead of answering `500`: the participant guard accepts a bare number and the three other participant writes qualify it, but these two routes handed it to the engine unqualified, so it reached the page as an unaddressable id. The same request already worked on Baileys. Refs #1220.+- A burst of inbound media on the whatsapp-web.js engine no longer loses the media of everything past the eighth, which the Baileys repair in this release left in place on the other engine: its download queue was capped at its active slots too, making admission a constant 8 whatever the batch size. A burst now parks rather than shedding, and only the download waits — a text message never enters the limiter.- The chart's optional ServiceMonitor now selects on a new `openwa.io/scrape-target` label, so it scrapes one target per pod instead of two; series previously collected through the headless target stop appearing, so check anything keyed on the `service` label.- The Helm chart's new startup probe stops the kubelet killing a pod that is still booting, allowing 295s (`periodSeconds: 5`, `failureThreshold: 60`) where the liveness settings allowed 50. It fires immediately, so a fast boot goes live sooner than before, not later — and it is not limited to auto-start, since the database connect retry alone can consume 30 of the old 50 seconds.- A Helm upgrade that changes only `env` or `secretEnv` now restarts the pod through ConfigMap and Secret checksums, so the new configuration reaches the running container. With `existingSecret` set the chart renders no Secret, so that path still needs `kubectl rollout restart`; `values.yaml` notes what publishing the Secret checksum as a pod annotation means.diff --git a/src/engine/adapters/whatsapp-web-js.adapter.ts b/src/engine/adapters/whatsapp-web-js.adapter.tsindex 43c95cb33..72828b760 100644--- a/src/engine/adapters/whatsapp-web-js.adapter.ts+++ b/src/engine/adapters/whatsapp-web-js.adapter.ts@@ -322,12 +322,12 @@ export class WhatsAppWebJsAdapter extends EventEmitter implements IWhatsAppEnginprivate readonly logger = createLogger('WhatsAppWebJsAdapter');// Bound concurrent inbound media downloads: downloadMedia() materialises the full base64 blob, so an// unbounded burst could stack many multi-MB allocations.- private readonly inboundLimiter = new ConcurrencyLimiter(- inboundMediaConcurrency(),- // Queue cap == active slots: beyond (active + queued) concurrent media messages, reject instead of- // parking, so a burst can't grow heap without bound (each parked closure holds the message).- inboundMediaConcurrency(),- );+ // The queue is UNBOUNDED. A cap equal to the active slots made admission a constant+ // (active + queued) whatever the batch size, so a burst lost the media of everything past the+ // eighth — the same defect repaired on the Baileys side. Parking costs one held Message per+ // waiting download, and only the download runs inside the limiter: each message awaits its own+ // capInboundMediaFor, so a parked one delays itself and a text message never enters the gate.+ private readonly inboundLimiter = new ConcurrencyLimiter(inboundMediaConcurrency());private readonly host: WwebjsEngineHost;private readonly groups: WwebjsGroups;@@ -392,14 +392,20 @@ export class WhatsAppWebJsAdapter extends EventEmitter implements IWhatsAppEngin() => undefined,);});- // The slot-holder runs in the background. It only rejects when the limiter's waiter queue is- // saturated (queue full) — in which case the download task never ran and boundedReady would hang.- // Resolve null so the caller unblocks and emits the message without media, matching the+ // The slot-holder runs in the background. With an unbounded queue the only rejection left is the+ // limiter CLOSING at teardown — in which case the download task never ran and boundedReady would+ // hang. Resolve null so the caller unblocks and emits the message without media, matching the// timeout/byte-cap no-media path. Never let it surface as an unhandled rejection either.- void slotHeld.catch(() => {- this.logger.warn('Inbound media limiter saturated; emitting message without media', {- msgId: msg.id._serialized,- });+ // Naming the teardown as saturation sent operators to look at concurrency settings for a problem+ // that was never there, so say which one happened.+ void slotHeld.catch((error: unknown) => {+ const closed = error instanceof Error && error.message.startsWith('ConcurrencyLimiter closed');+ this.logger.warn(+ closed+ ? 'Inbound media limiter closed during teardown; emitting message without media'+ : 'Inbound media download could not be admitted; emitting message without media',+ { msgId: msg.id._serialized, ...(closed ? {} : { error: String(error) }) },+ );resolveBounded(null);});const media = await boundedReady;diff --git a/src/engine/adapters/wwebjs-inbound-burst.spec.ts b/src/engine/adapters/wwebjs-inbound-burst.spec.tsnew file mode 100644index 000000000..741d923ca--- /dev/null+++ b/src/engine/adapters/wwebjs-inbound-burst.spec.ts@@ -0,0 +1,32 @@+import { readFileSync } from 'node:fs';+import { join } from 'node:path';++/**+ * The whatsapp-web.js half of the inbound-media burst bound.+ *+ * `baileys-inbound-burst.spec.ts` pins the same property on the other engine, where a queue cap+ * equal to the active slots made admission a constant 8 whatever the batch size, so a burst lost+ * the media of everything past the eighth. The whatsapp-web.js adapter carried the identical+ * construction — `new ConcurrencyLimiter(inboundMediaConcurrency(), inboundMediaConcurrency())` —+ * and kept losing media in exactly the same way after the Baileys side was repaired.+ *+ * Unbounding the queue is safe here for a reason that does not hold on Baileys: only the DOWNLOAD+ * runs inside the limiter, not the whole message pipeline, and each message awaits its own+ * `capInboundMediaFor`. A parked download therefore delays that message alone — a text message+ * never enters the limiter at all.+ */+describe('whatsapp-web.js inbound media burst', () => {+ it('gives the inbound limiter an unbounded queue, so a burst parks instead of shedding', () => {+ const source = readFileSync(join(__dirname, 'whatsapp-web-js.adapter.ts'), 'utf8');+ const construction = source.match(/new ConcurrencyLimiter\(([\s\S]*?)\);/);++ // Guard the parser: a renamed limiter would make this vacuous.+ expect(construction).not.toBeNull();++ const args = (construction?.[1] ?? '')+ .split(',')+ .map(arg => arg.replace(/\/\/[^\n]*/g, '').trim())+ .filter(Boolean);+ expect(args).toEqual(['inboundMediaConcurrency()']);+ });+});