Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(db): insert without a transaction where a conflict is expected
Yudhi Armyndharis1eea6da1f671
diff --git a/src/modules/integration/conversation-mapping.service.spec.ts b/src/modules/integration/conversation-mapping.service.spec.tsindex 580ce99c2..8a67bb117 100644--- a/src/modules/integration/conversation-mapping.service.spec.ts+++ b/src/modules/integration/conversation-mapping.service.spec.ts@@ -207,11 +207,11 @@ describe('ConversationMappingService', () => {it('a hand-back that lands while a row under the other form is being linked still reaches it', async () => {await human('111@lid');const repo = ds.getRepository(ConversationMapping);- const save = repo.save.bind(repo);- jest.spyOn(repo, 'save').mockImplementationOnce(async (entity: unknown) => {+ const insert = repo.insert.bind(repo);+ jest.spyOn(repo, 'insert').mockImplementationOnce(async (entity: Parameters<typeof insert>[0]) => {// The agent hands back on the lid conversation after the new row read the takeover.await service.setHandover((await row('111@lid'))!.id, 'bot');- return save(entity as ConversationMapping);+ return insert(entity);});await bot('[email protected]', 'convC');expect(await service.findHandoverForChat('sess-1', '[email protected]')).toBeNull();@@ -232,6 +232,30 @@ describe('ConversationMappingService', () => {});});+ // SQLite serves every repository from one connection, so a transaction the insert opens is shared with+ // the writes made while it is open, and the rollback of the conflict it expects would undo them too.+ it('does not undo a write made while an upsert is refused for a bound conversation', async () => {+ await service.upsert(key, 'conv-1');+ const repo = ds.getRepository(ConversationMapping);+ const other = { ...key, instanceId: 'acct2', chatId: 'chat-9' };+ let concurrent: Promise<unknown> | undefined;+ ds.subscribers.push({+ listenTo: () => ConversationMapping,+ beforeInsert: event => {+ if ((event.entity as ConversationMapping).chatId === 'chat-3') {+ concurrent = repo.insert({ ...other, providerConversationId: 'conv-9' });+ }+ },+ });++ await expect(service.upsert({ ...key, chatId: 'chat-3' }, 'conv-1')).rejects.toBeInstanceOf(+ ConversationMappingConflict,+ );+ await concurrent;++ expect(await service.get(other)).not.toBeNull();+ });+it('delete removes the row so a later reverse-key insert no longer conflicts', async () => {await service.upsert(key, 'conv-1');const row = await service.get(key);diff --git a/src/modules/integration/conversation-mapping.service.ts b/src/modules/integration/conversation-mapping.service.tsindex 1b51ac79f..4cc7420a8 100644--- a/src/modules/integration/conversation-mapping.service.ts+++ b/src/modules/integration/conversation-mapping.service.ts@@ -49,7 +49,15 @@ export class ConversationMappingService implements PluginConversationMappingPort}try {const handoverState = await this.inheritedHandover(key);- await this.repo.save(this.repo.create({ ...key, providerConversationId, handoverState, ...patch }));+ // A bare INSERT, not save(): save() wraps it in a transaction, and on SQLite's single shared+ // connection the rollback of the unique violation handled below would also discard any other+ // write made while it was open.+ await this.repo.insert({+ ...key,+ providerConversationId,+ handoverState,+ ...patch,+ } as QueryDeepPartialEntity<ConversationMapping>);// A hand-back on the other form between the read above and this insert did not reach the new row;// read again now that the row exists (a later decision updates it directly).if (!patch?.handoverState && handoverState !== 'bot') {diff --git a/src/modules/webhook/webhook-outbox.service.spec.ts b/src/modules/webhook/webhook-outbox.service.spec.tsindex dcad5b1ef..3203af704 100644--- a/src/modules/webhook/webhook-outbox.service.spec.ts+++ b/src/modules/webhook/webhook-outbox.service.spec.ts@@ -12,13 +12,12 @@ const base = {};describe('WebhookOutboxService', () => {- let repo: { save: jest.Mock; create: jest.Mock; update: jest.Mock; find: jest.Mock };+ let repo: { insert: jest.Mock; update: jest.Mock; find: jest.Mock };let service: WebhookOutboxService;beforeEach(() => {repo = {- create: jest.fn((v: unknown) => v),- save: jest.fn().mockResolvedValue(undefined),+ insert: jest.fn().mockResolvedValue(undefined),update: jest.fn().mockResolvedValue(undefined),find: jest.fn().mockResolvedValue([]),};@@ -28,12 +27,12 @@ describe('WebhookOutboxService', () => {it('records the delivery as pending, carrying the payload a replay needs', async () => {await service.open(base);- expect(repo.save).toHaveBeenCalledWith(expect.objectContaining({ ...base, state: 'pending', attempts: 0 }));+ expect(repo.insert).toHaveBeenCalledWith(expect.objectContaining({ ...base, state: 'pending', attempts: 0 }));});it('treats a duplicate as the replay it is, keeping the existing row', async () => {const conflict = Object.assign(new Error('UNIQUE constraint failed'), { code: 'SQLITE_CONSTRAINT_UNIQUE' });- repo.save.mockRejectedValue(conflict);+ repo.insert.mockRejectedValue(conflict);// A conflict on (webhookId, idempotencyKey) means this delivery is already recorded. Throwing// would surface as an unhandled rejection on a fire-and-forget path.@@ -41,7 +40,7 @@ describe('WebhookOutboxService', () => {});it('never lets a repository failure reach a fire-and-forget caller', async () => {- repo.save.mockRejectedValue(new Error('disk full'));+ repo.insert.mockRejectedValue(new Error('disk full'));await expect(service.open(base)).resolves.toBeUndefined();repo.update.mockRejectedValue(new Error('disk full'));diff --git a/src/modules/webhook/webhook-outbox.service.sqlite.spec.ts b/src/modules/webhook/webhook-outbox.service.sqlite.spec.tsnew file mode 100644index 000000000..b693dda1d--- /dev/null+++ b/src/modules/webhook/webhook-outbox.service.sqlite.spec.ts@@ -0,0 +1,55 @@+import 'reflect-metadata';+import { DataSource } from 'typeorm';+import { Session } from '../session/entities/session.entity';+import { Webhook } from './entities/webhook.entity';+import { WebhookDeliveryFailure } from './entities/webhook-delivery-failure.entity';+import { WebhookOutboxEvent } from './entities/webhook-outbox-event.entity';+import { WebhookOutboxService } from './webhook-outbox.service';+import { recordWebhookDeliveryFailure } from './utils/record-delivery-failure';++// SQLite serves every repository from one connection, so a transaction a write opens is shared with the+// writes that run while it is open. A re-dispatched key makes open() hit the unique violation it expects.+describe('WebhookOutboxService.open() against a concurrent write (SQLite)', () => {+ let ds: DataSource;++ beforeEach(async () => {+ ds = await new DataSource({+ type: 'better-sqlite3',+ database: ':memory:',+ entities: [Session, Webhook, WebhookDeliveryFailure, WebhookOutboxEvent],+ synchronize: true,+ }).initialize();+ });+ afterEach(() => ds.destroy());++ it('does not undo a delivery failure recorded while a duplicate key is rejected', async () => {+ const outbox = new WebhookOutboxService(ds.getRepository(WebhookOutboxEvent));+ const failures = ds.getRepository(WebhookDeliveryFailure);+ const logger = { error: jest.fn() };+ const lost: number[] = [];++ for (let i = 0; i < 20; i++) {+ const row = { webhookId: 'w1', sessionId: 's1', event: 'message.ack', deliveryId: `d${i}`, payload: { i } };+ await outbox.open({ ...row, idempotencyKey: `ack_${i}` });++ const duplicate = outbox.open({ ...row, idempotencyKey: `ack_${i}`, deliveryId: `d${i}b` });+ const recorded = recordWebhookDeliveryFailure(failures, logger, {+ webhookId: 'w1',+ sessionId: 's1',+ event: 'message.received',+ url: 'https://a.example',+ idempotencyKey: `other_${i}`,+ deliveryId: `x${i}`,+ attempts: 3,+ lastStatusCode: 503,+ lastError: 'HTTP 503',+ });+ const [, result] = await Promise.all([duplicate, recorded]);++ // The recorder answered that the row is durable, so it must be there.+ if (result !== null && (await failures.count({ where: { idempotencyKey: `other_${i}` } })) === 0) lost.push(i);+ }++ expect(lost).toEqual([]);+ });+});diff --git a/src/modules/webhook/webhook-outbox.service.ts b/src/modules/webhook/webhook-outbox.service.tsindex c23bb23ce..a7c97553b 100644--- a/src/modules/webhook/webhook-outbox.service.ts+++ b/src/modules/webhook/webhook-outbox.service.ts@@ -1,6 +1,6 @@import { Injectable, OnModuleDestroy, OnModuleInit } from '@nestjs/common';import { InjectRepository } from '@nestjs/typeorm';-import { And, In, IsNull, LessThan, Not, Raw, Repository } from 'typeorm';+import { And, In, IsNull, LessThan, Not, QueryDeepPartialEntity, Raw, Repository } from 'typeorm';import { WebhookOutboxEvent, WebhookOutboxState } from './entities/webhook-outbox-event.entity';import { createLogger } from '../../common/services/logger.service';import { isUniqueViolation } from '../../common/utils/db-errors';@@ -92,18 +92,19 @@ export class WebhookOutboxService implements OnModuleInit, OnModuleDestroy {*/async open(row: Omit<ReplayableDelivery, 'id' | 'attempts'> & { deliveryId: string }): Promise<void> {try {- await this.outbox.save(- this.outbox.create({- webhookId: row.webhookId,- sessionId: row.sessionId,- event: row.event,- idempotencyKey: row.idempotencyKey,- deliveryId: row.deliveryId,- payload: row.payload,- state: 'pending',- attempts: 0,- }),- );+ // A bare INSERT, not save(): save() wraps it in a transaction, and on SQLite's single shared+ // connection the rollback of the unique violation expected here would also discard any other+ // write made while it was open.+ await this.outbox.insert({+ webhookId: row.webhookId,+ sessionId: row.sessionId,+ event: row.event,+ idempotencyKey: row.idempotencyKey,+ deliveryId: row.deliveryId,+ payload: row.payload,+ state: 'pending',+ attempts: 0,+ } as QueryDeepPartialEntity<WebhookOutboxEvent>);} catch (error) {// A conflict means this exact delivery is already recorded, which is what a replay looks// like: keep the existing row rather than duplicating it.