Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(db): save webhooks and automation rules without a transaction

Yudhi Armyndharis9ca274fcd997
diff --git a/src/modules/automation/automation-rules.service.spec.ts b/src/modules/automation/automation-rules.service.spec.ts
index 437bade85..7fef0661a 100644
--- a/src/modules/automation/automation-rules.service.spec.ts
+++ b/src/modules/automation/automation-rules.service.spec.ts
@@ -185,6 +185,34 @@ describe('AutomationRulesService', () => {
await service.create('sessB', { name: 'b', replyText: 'y' });
expect((await service.findAll('sessA')).map(r => r.name)).toEqual(['a']);
});
+
+ // SQLite serves every repository from one connection, so a transaction the save opened would hold what other
+ // callers wrote while it was open, and the rollback of its failed insert would undo those writes.
+ it('keeps a write made beside a create whose session is deleted after the check', async () => {
+ const sessions = ds.getRepository(Session);
+ jest.spyOn(sessions, 'exists').mockResolvedValue(true);
+ let steps = 0;
+ let beside: Promise<unknown> = Promise.resolve();
+ ds.subscribers.push({
+ listenTo: () => AutomationRule,
+ beforeInsert: () => {
+ beside = (async () => {
+ for (let i = 0; i < steps; i += 1) await Promise.resolve();
+ await sessions.update('sessB', { status: SessionStatus.FAILED });
+ })();
+ },
+ });
+
+ for (; steps <= 10; steps += 1) {
+ await sessions.update('sessB', { status: SessionStatus.READY });
+ await expect(service.create('gone', { name: 'r', replyText: 'hi' })).rejects.toThrow(/FOREIGN KEY/);
+ await beside;
+ expect({ steps, status: (await sessions.findOneByOrFail({ id: 'sessB' })).status }).toEqual({
+ steps,
+ status: SessionStatus.FAILED,
+ });
+ }
+ });
});
describe('evaluateInbound', () => {
diff --git a/src/modules/automation/automation-rules.service.ts b/src/modules/automation/automation-rules.service.ts
index 5727c1a2b..372f2f251 100644
--- a/src/modules/automation/automation-rules.service.ts
+++ b/src/modules/automation/automation-rules.service.ts
@@ -103,7 +103,9 @@ export class AutomationRulesService {
cooldownSeconds: dto.cooldownSeconds ?? 60,
enabled: dto.enabled ?? true,
});
- return this.ruleRepository.save(rule);
+ // No transaction of its own: on SQLite's single shared connection the rollback of a foreign key failure (the
+ // session deleted since the check above) would discard what other callers wrote while it was open.
+ return this.ruleRepository.save(rule, { transaction: false });
}
async findAll(sessionId: string): Promise<AutomationRule[]> {
diff --git a/src/modules/webhook/webhook.service.spec.ts b/src/modules/webhook/webhook.service.spec.ts
index 1ed0e54ce..1883f73bf 100644
--- a/src/modules/webhook/webhook.service.spec.ts
+++ b/src/modules/webhook/webhook.service.spec.ts
@@ -16,7 +16,7 @@ import { runInNewContext } from 'vm';
import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
import { getQueueToken } from '@nestjs/bullmq';
-import { FindOperator, In, Repository } from 'typeorm';
+import { DataSource, FindOperator, In, Repository } from 'typeorm';
import { NotFoundException } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { fetch as undiciFetch } from 'undici';
@@ -30,7 +30,7 @@ import { LidMappingStoreService } from '../../engine/identity/lid-mapping-store.
import { userPart } from '../../engine/identity/wa-id';
import { HookManager } from '../../core/hooks';
import { QUEUE_NAMES } from '../queue/queue-names';
-import { Session } from '../session/entities/session.entity';
+import { Session, SessionStatus } from '../session/entities/session.entity';
function createMockWebhook(overrides: Partial<Webhook> = {}): Webhook {
return {
@@ -779,3 +779,58 @@ describe('WebhookService', () => {
});
});
});
+
+// SQLite serves every repository from one connection, so a transaction the save opened would hold what other callers
+// wrote while it was open, and the rollback of its failed insert would undo those writes.
+describe('WebhookService.create beside other writes on one connection', () => {
+ let ds: DataSource;
+ let service: WebhookService;
+
+ beforeEach(async () => {
+ ds = new DataSource({
+ type: 'better-sqlite3',
+ database: ':memory:',
+ entities: [Session, Webhook],
+ synchronize: true,
+ });
+ await ds.initialize();
+ await ds.getRepository(Session).save({ id: 'sessB', name: 'sessB', status: SessionStatus.READY, config: {} });
+ service = new WebhookService(
+ ds.getRepository(Webhook),
+ {} as never,
+ ds.getRepository(Session),
+ new ConfigService({}),
+ { invalidateOptInSubscriptions: jest.fn() } as never,
+ );
+ });
+
+ afterEach(async () => {
+ await ds.destroy();
+ });
+
+ it('keeps a write made beside a create whose session is deleted after the check', async () => {
+ const sessions = ds.getRepository(Session);
+ jest.spyOn(sessions, 'exists').mockResolvedValue(true);
+ let steps = 0;
+ let beside: Promise<unknown> = Promise.resolve();
+ ds.subscribers.push({
+ listenTo: () => Webhook,
+ beforeInsert: () => {
+ beside = (async () => {
+ for (let i = 0; i < steps; i += 1) await Promise.resolve();
+ await sessions.update('sessB', { status: SessionStatus.FAILED });
+ })();
+ },
+ });
+
+ for (; steps <= 10; steps += 1) {
+ await sessions.update('sessB', { status: SessionStatus.READY });
+ await expect(service.create('gone', { url: 'https://example.com/webhook' })).rejects.toThrow(/FOREIGN KEY/);
+ await beside;
+ expect({ steps, status: (await sessions.findOneByOrFail({ id: 'sessB' })).status }).toEqual({
+ steps,
+ status: SessionStatus.FAILED,
+ });
+ }
+ });
+});
diff --git a/src/modules/webhook/webhook.service.ts b/src/modules/webhook/webhook.service.ts
index 36d28de91..9c98f21fc 100644
--- a/src/modules/webhook/webhook.service.ts
+++ b/src/modules/webhook/webhook.service.ts
@@ -204,7 +204,9 @@ export class WebhookService implements OnModuleInit, OnModuleDestroy {
retryCount: dto.retryCount ?? 3,
});
- const saved = await this.webhookRepository.save(webhook);
+ // No transaction of its own: the session can be deleted while the URL is checked, and on SQLite's single shared
+ // connection the rollback of the foreign key failure would discard what other callers wrote while it was open.
+ const saved = await this.webhookRepository.save(webhook, { transaction: false });
this.delivery.invalidateOptInSubscriptions(sessionId);
return saved;
}