Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(integration): keep an instance delete's transaction its own

Yudhi Armyndharis4a2ec2459d56
diff --git a/docs/06-api-specification.md b/docs/06-api-specification.md
index afdc2d439..240a603cd 100644
--- a/docs/06-api-specification.md
+++ b/docs/06-api-specification.md
@@ -7014,7 +7014,7 @@ Delete the instance and tear down its session-scope binding.
**Response** `204`
-**Errors:** `401` · `403` · `404` unknown instance, or one outside the key's scope
+**Errors:** `401` · `403` · `404` unknown instance, or one outside the key's scope · `503` on SQLite, another database transaction stayed open for 10 s (nothing is deleted; retry)
#### POST /api/integration/plugins/:pluginId/instances/:instanceId/regenerate-secret
diff --git a/openapi.json b/openapi.json
index dd4991684..2615eee8c 100644
--- a/openapi.json
+++ b/openapi.json
@@ -17822,6 +17822,16 @@
}
}
}
+ },
+ "503": {
+ "description": "On SQLite, another database transaction stayed open for 10 s; nothing was deleted, so retry.",
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/ErrorResponse"
+ }
+ }
+ }
}
},
"tags": [
diff --git a/src/modules/integration/integration-instance.controller.ts b/src/modules/integration/integration-instance.controller.ts
index 65105c2ad..37838f979 100644
--- a/src/modules/integration/integration-instance.controller.ts
+++ b/src/modules/integration/integration-instance.controller.ts
@@ -187,6 +187,10 @@ export class IntegrationInstanceController {
@HttpCode(204)
@ApiResponse({ status: 204, description: 'Instance deleted and its session scope torn down.' })
@ApiResponse({ status: 404, description: "Unknown instance, or one outside the key's allowedSessions." })
+ @ApiResponse({
+ status: 503,
+ description: 'On SQLite, another database transaction stayed open for 10 s; nothing was deleted, so retry.',
+ })
async remove(
@Param('pluginId') pluginId: string,
@Param('instanceId') instanceId: string,
diff --git a/src/modules/integration/plugin-instance.service.spec.ts b/src/modules/integration/plugin-instance.service.spec.ts
index f5351be69..8623225c3 100644
--- a/src/modules/integration/plugin-instance.service.spec.ts
+++ b/src/modules/integration/plugin-instance.service.spec.ts
@@ -1,4 +1,6 @@
+import { ServiceUnavailableException } from '@nestjs/common';
import { DataSource } from 'typeorm';
+import { FOREIGN_TRANSACTION_WAIT_MS } from '../../common/utils/shared-connection';
import { PluginInstance } from './entities/plugin-instance.entity';
import { ConversationMapping } from './entities/conversation-mapping.entity';
import { ConversationMappingService } from './conversation-mapping.service';
@@ -205,6 +207,90 @@ describe('PluginInstanceService provisioning', () => {
expect(await service.remove('chatwoot', 'acct1')).toBe(false);
});
+ // On SQLite every caller shares one connection. A transaction begun in the same turn of the event loop as the
+ // remove's would nest in it or hold it, so either could fail on the other's state or undo the other's writes.
+ describe('a remove beside another transaction begun in the same turn', () => {
+ const settle = (result: Promise<unknown>) => result.catch((error: unknown) => String(error));
+ const after = <T>(steps: number, start: () => Promise<T>) =>
+ (async () => {
+ for (let i = 0; i < steps; i += 1) await Promise.resolve();
+ return start();
+ })();
+ async function provision(instanceId: string) {
+ await service.create('chatwoot', instanceId, {});
+ const mappings = ds.getRepository(ConversationMapping);
+ await mappings.save(
+ mappings.create({
+ sessionId: 'sess-1',
+ chatId: `${instanceId}@c.us`,
+ pluginId: 'chatwoot',
+ instanceId,
+ providerConversationId: instanceId,
+ handoverState: 'human',
+ }),
+ );
+ }
+ const left = async (instanceId: string) => ({
+ rows: await ds.getRepository(PluginInstance).countBy({ instanceId }),
+ handover: (await ds.getRepository(ConversationMapping).findOneByOrFail({ instanceId })).handoverState,
+ });
+ const gone = { rows: 0, handover: 'bot' };
+
+ // A write that fails on the primary key and rolls its transaction back.
+ const failing = () =>
+ ds.transaction(manager => manager.query('INSERT INTO plugin_instances SELECT * FROM plugin_instances'));
+
+ it.each(['before', 'after'])('removes the instance when a transaction that fails begins %s it', async order => {
+ for (let steps = 0; steps <= 40; steps += 1) {
+ const instanceId = `acct-${steps}`;
+ await provision(instanceId);
+ const remove = () => service.remove('chatwoot', instanceId);
+ const [removed] =
+ order === 'after'
+ ? await Promise.all([settle(remove()), settle(after(steps, failing))])
+ : await Promise.all([settle(after(steps, remove)), settle(failing())]);
+ expect({ steps, removed, ...(await left(instanceId)) }).toEqual({ steps, removed: true, ...gone });
+ }
+ });
+
+ it('removes both instances of two removes', async () => {
+ for (let steps = 0; steps <= 40; steps += 1) {
+ await provision(`first-${steps}`);
+ await provision(`second-${steps}`);
+ const removed = await Promise.all([
+ settle(service.remove('chatwoot', `first-${steps}`)),
+ settle(after(steps, () => service.remove('chatwoot', `second-${steps}`))),
+ ]);
+ expect({ steps, removed, first: await left(`first-${steps}`), second: await left(`second-${steps}`) }).toEqual({
+ steps,
+ removed: [true, true],
+ first: gone,
+ second: gone,
+ });
+ }
+ });
+
+ it('refuses with 503 and removes nothing when a transaction beside it stays open', async () => {
+ await provision('held');
+ const other = ds.createQueryRunner();
+ await other.connect();
+ await other.startTransaction();
+ const start = Date.now();
+ let calls = 0;
+ const clock = jest.spyOn(Date, 'now').mockImplementation(() => start + FOREIGN_TRANSACTION_WAIT_MS * calls++);
+
+ try {
+ await expect(service.remove('chatwoot', 'held')).rejects.toThrow(ServiceUnavailableException);
+ } finally {
+ clock.mockRestore();
+ }
+ await other.commitTransaction();
+ await other.release();
+
+ expect(await left('held')).toEqual({ rows: 1, handover: 'human' });
+ });
+ });
+
it('update and regenerateSecret return the stored updatedAt, not the one read before the write', async () => {
await service.create('chatwoot', 'acct1', {});
const backdate = () => ds.query(`UPDATE plugin_instances SET "updatedAt" = '2000-01-01 00:00:00'`);
diff --git a/src/modules/integration/plugin-instance.service.ts b/src/modules/integration/plugin-instance.service.ts
index 06a4a01df..103ba88e0 100644
--- a/src/modules/integration/plugin-instance.service.ts
+++ b/src/modules/integration/plugin-instance.service.ts
@@ -1,10 +1,11 @@
import { randomBytes } from 'node:crypto';
-import { BadRequestException, Injectable } from '@nestjs/common';
+import { BadRequestException, Injectable, ServiceUnavailableException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { In, QueryDeepPartialEntity, Repository } from 'typeorm';
import { PluginInstance } from './entities/plugin-instance.entity';
import { ConversationMapping } from './entities/conversation-mapping.entity';
import { isUniqueViolation } from '../../common/utils/db-errors';
+import { awaitNoOpenTransaction } from '../../common/utils/shared-connection';
import type { PluginConfigSchema } from '../../core/plugins/plugin.interfaces';
import { redactSecretConfig, restoreSecretConfig, SECRET_SENTINEL } from '../plugins/redact-config';
// Type-only: the module binds this class to PLUGIN_INSTANCE_PORT with a `useExisting` alias, which
@@ -140,6 +141,13 @@ export class PluginInstanceService implements PluginInstancePort {
}
async remove(pluginId: string, instanceId: string): Promise<boolean> {
+ // On SQLite every caller shares one connection: opened beside another caller's transaction, this one would be a
+ // savepoint in it or carry that one's writes, and either rollback could undo what the other had answered for.
+ if (!(await awaitNoOpenTransaction(this.repo.manager.dataSource))) {
+ throw new ServiceUnavailableException(
+ `Instance ${instanceId} was not deleted: another database transaction stayed open. Retry the delete.`,
+ );
+ }
return this.repo.manager.transaction(async manager => {
const result = await manager.delete(PluginInstance, { id: `${pluginId}:${instanceId}` });
// Its conversation mappings stay, so the same instance provisioned again keeps its provider