Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(webhook): release unshed event data before awaiting fan-out (#1782)

Yudhi Armyndharise56e0ccd20f8
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a7c767b8e..ea8d67557 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
+- Stop pending webhook deliveries from holding over-cap inline media already omitted from their payload.
- Resolve `./data/...` paths from `.env.generated` under `OPENWA_DATA_DIR` in host-side `backup.sh` and `restore.sh` runs.
- Stop `backup.sh` and `restore.sh` on an env line they cannot parse instead of falling back to the default path.
- Verify the PostgreSQL server certificate in `backup.sh` when `DATABASE_SSL=true`, as the app does.
diff --git a/src/modules/webhook/webhook-delivery.service.ts b/src/modules/webhook/webhook-delivery.service.ts
index 4e2d085b4..a4b6e135f 100644
--- a/src/modules/webhook/webhook-delivery.service.ts
+++ b/src/modules/webhook/webhook-delivery.service.ts
@@ -265,6 +265,10 @@ export class WebhookDeliveryService implements OnModuleInit, OnModuleDestroy {
this.configService.get<number>('webhook.mediaInlineMaxBytes', DEFAULT_WEBHOOK_MEDIA_INLINE_MAX_BYTES),
)
: data;
+ // The await below holds this frame, parameters included, until every delivery settles: point
+ // `data` at the shed copy so parked deliveries pin only that, not the caller's full blob.
+ // eslint-disable-next-line no-useless-assignment
+ data = baseData;
const ctx: DispatchEventContext = { sessionId, event, baseData };
// allSettled preserves the per-webhook isolation: one failing delivery never rejects the others.
diff --git a/src/modules/webhook/webhook.service.spec.ts b/src/modules/webhook/webhook.service.spec.ts
index 1c4e9b12d..c4b835e9a 100644
--- a/src/modules/webhook/webhook.service.spec.ts
+++ b/src/modules/webhook/webhook.service.spec.ts
@@ -11,6 +11,8 @@ jest.mock('undici', () => {
});
import { createHmac } from 'crypto';
+import { setFlagsFromString } from 'v8';
+import { runInNewContext } from 'vm';
import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
import { getQueueToken } from '@nestjs/bullmq';
@@ -568,6 +570,50 @@ describe('WebhookService', () => {
expect(dispatchSpy).toHaveBeenCalledWith('sess-1', 'message.received', { x: 1 });
expect(repository.find).not.toHaveBeenCalled(); // the engine, not the facade, loads webhooks
});
+
+ it('lets go of an over-cap media event while its delivery waits on the receiver', async () => {
+ // Process-wide: later suites in this jest worker also see a global gc in new vm contexts.
+ setFlagsFromString('--expose-gc');
+ const gc = runInNewContext('gc') as () => void;
+ const mockFetch = undiciFetch as jest.Mock;
+ const webhook = createMockWebhook();
+ (repository.find as jest.Mock).mockResolvedValue([webhook]);
+ (repository.findOne as jest.Mock).mockResolvedValue(webhook);
+ (hookManager.execute as jest.Mock).mockResolvedValue({ continue: true, data: {} });
+ let answer!: (response: Response) => void;
+ const posted = new Promise<void>(reached =>
+ mockFetch.mockImplementationOnce(() => {
+ reached();
+ return new Promise<Response>(resolve => (answer = resolve));
+ }),
+ );
+ try {
+ // Over the default 1 MiB inline cap, so the engine sheds it before fan-out.
+ let media: Record<string, unknown> | undefined = {
+ mimetype: 'image/jpeg',
+ data: Buffer.alloc(1024 * 1024 + 1, 5).toString('base64'),
+ };
+ const original = new WeakRef(media);
+ const dispatched = service.dispatch('sess-1', 'message.received', { from: '[email protected]', media });
+ media = undefined;
+
+ await posted;
+ await new Promise(resolve => setImmediate(resolve));
+ gc();
+ // Only the marker copy may stay reachable while the receiver hangs: a frame that still
+ // holds the caller's event keeps every parked delivery's full blob in memory.
+ expect(original.deref() === undefined).toBe(true);
+
+ answer(new Response('ok', { status: 200 }));
+ await dispatched;
+ const body = JSON.parse((mockFetch.mock.calls[0] as [unknown, { body: string }])[1].body) as {
+ data: { media: Record<string, unknown> };
+ };
+ expect(body.data.media).toEqual({ mimetype: 'image/jpeg', omitted: true, sizeBytes: 1024 * 1024 + 1 });
+ } finally {
+ mockFetch.mockReset();
+ }
+ });
});
// ── test probe ────────────────────────────────────────────────────
diff --git a/src/modules/webhook/webhook.service.ts b/src/modules/webhook/webhook.service.ts
index d4085fc23..bd16440e6 100644
--- a/src/modules/webhook/webhook.service.ts
+++ b/src/modules/webhook/webhook.service.ts
@@ -361,8 +361,12 @@ export class WebhookService implements OnModuleInit, OnModuleDestroy {
}
}
- /** Delivery facade: same entry point event producers have always called; the engine lives on WebhookDeliveryService. */
- async dispatch(sessionId: string, event: string, data: Record<string, unknown>): Promise<void> {
- await this.delivery.dispatch(sessionId, event, data);
+ /**
+ * Delivery facade: same entry point event producers have always called; the engine lives on
+ * WebhookDeliveryService. Not async on purpose: a frame suspended on the engine would keep the
+ * unshed event alive until every delivery settles.
+ */
+ dispatch(sessionId: string, event: string, data: Record<string, unknown>): Promise<void> {
+ return this.delivery.dispatch(sessionId, event, data);
}
}