Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(webhook): retire the outbox row when a redrive delivers (#1794)
Yudhi Armyndharisc38752ca8b46
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex af29b165f..e5ab61ad1 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Fixed+- Retire the pending webhook outbox record when a redrive delivers, so the sweep does not send the event again.- Stop dashboard multi-page loads and a pending chat mark-as-read from sending requests after logout.- Keep a new dashboard login signed in when a request sent with the previous API key fails afterwards.- Enforce the plugin package size limit before reading `manifest.json`, and reject archive entries whose path starts with `/`.diff --git a/docs/11-operational-runbooks.md b/docs/11-operational-runbooks.mdindex f83b1ab6c..490326c3f 100644--- a/docs/11-operational-runbooks.md+++ b/docs/11-operational-runbooks.md@@ -338,8 +338,9 @@ curl -X PUT http://localhost:2785/api/sessions/{sessionId}/webhooks/{webhookId}# Rows stay inside the ADMIN key's allowedSessions; sessionId, webhookId and ids only narrow# the batch. The webhook must be active and still subscribed to the event (re-enable it if# step 5C left it off). Each row gets one direct POST to the webhook's current URL with its-# stored X-OpenWA-Idempotency-Key; a delivered row is removed, a failed one stays. If the sweep-# still holds the event it can POST it again under the same key, so receivers must deduplicate.+# stored X-OpenWA-Idempotency-Key; a delivered row is removed, along with the copy the sweep+# still held for that event, and a failed one stays. A sweep replay already under way can still+# POST the event again under the same key, so receivers must deduplicate.curl -X POST http://localhost:2785/api/webhooks/delivery-failures/redrive \-H "X-API-Key: $ADMIN_API_KEY" \-H "Content-Type: application/json" \diff --git a/src/modules/webhook/webhook-delivery.service.ts b/src/modules/webhook/webhook-delivery.service.tsindex a4b6e135f..7cafdb162 100644--- a/src/modules/webhook/webhook-delivery.service.ts+++ b/src/modules/webhook/webhook-delivery.service.ts@@ -925,12 +925,16 @@ export class WebhookDeliveryService implements OnModuleInit, OnModuleDestroy {options: { singleAttempt?: boolean } = {},): Promise<WebhookDeliveryOutcome> {const deliveryId = generateDeliveryId();- return this.deliverOne(webhook, deliveryId, idempotencyKey, {+ const outcome = await this.deliverOne(webhook, deliveryId, idempotencyKey, {sessionId,event,baseData: data,singleAttempt: options.singleAttempt,});+ // The receiver has the event. An operator redrive of a failure row can find the same delivery+ // still pending in the outbox, which the next sweep would otherwise POST again.+ if (outcome === 'delivered') await this.outbox.close(webhook.id, idempotencyKey, 'dispatched');+ return outcome;}/** A live or unreadable job still owns its queued delivery; never replay alongside it. */diff --git a/src/modules/webhook/webhook-recovery-boundaries.spec.ts b/src/modules/webhook/webhook-recovery-boundaries.spec.tsindex 3c4e78080..c9ed09035 100644--- a/src/modules/webhook/webhook-recovery-boundaries.spec.ts+++ b/src/modules/webhook/webhook-recovery-boundaries.spec.ts@@ -143,6 +143,44 @@ describe('webhook recovery across live ownership and configuration changes', ()},);+ it('retires the outbox copy when a redrive delivers, so the next sweep sends nothing', async () => {+ const config = new ConfigService({ webhook: { failurePayloadRetentionHours: 24, timeout: 1000, retryDelay: 1 } });+ const repository = ds.getRepository(WebhookOutboxEvent);+ const outbox = new WebhookOutboxService(repository);+ const delivery = new WebhookDeliveryService(webhooks, failures, config, new HookManager(), outbox);+ const reconciler = new WebhookReconcilerService(webhooks, outbox, delivery);+ const redrive = new WebhookRedriveService(webhooks, failures, delivery, config);+ const options = { intervalMs: 1000, graceMs: 0, batchSize: 50, maxAttempts: 5 };+ // A dispatch interrupted before it settled leaves its row pending for the sweep.+ await outbox.open({+ webhookId: primary.id,+ sessionId: primary.sessionId,+ event: 'message.received',+ idempotencyKey: 'stranded-key',+ deliveryId: 'job0',+ payload: { id: 'MSG1' },+ });+ await repository.update({ idempotencyKey: 'stranded-key' }, { createdAt: new Date(Date.now() - 60_000) });+ let status = 503;+ const post = jest+ .spyOn(ssrf, 'withSafeFetch')+ .mockImplementation((_url, _init, use) => Promise.resolve(use(new Response(null, { status }))));++ // The sweep's replay fails: the row stays pending and a redrivable failure row is filed.+ expect(await reconciler.sweep(options)).toMatchObject({ scanned: 1, failed: 1 });+ expect(await failures.count({ where: { idempotencyKey: 'stranded-key' } })).toBe(1);++ status = 200;+ expect(await redrive.redrive({})).toMatchObject({ delivered: 1, failed: 0 });+ expect(await failures.count()).toBe(0);+ const row = await repository.findOneByOrFail({ idempotencyKey: 'stranded-key' });+ expect(row.state).toBe('dispatched');+ expect(row.payload).toBeNull();++ expect(await reconciler.sweep(options)).toMatchObject({ scanned: 0 });+ expect(post).toHaveBeenCalledTimes(2);+ });+it('rechecks the receiver after a held webhook hook before the first POST', async () => {const config = new ConfigService({ webhook: { failurePayloadRetentionHours: 24 } });const hooks = new HookManager();