Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(infra): release the import runner when BEGIN fails

Yudhi Armyndharisa4a9a121f430
diff --git a/src/modules/infra/infra-data.controller.spec.ts b/src/modules/infra/infra-data.controller.spec.ts
index 99d51d927..22829f6df 100644
--- a/src/modules/infra/infra-data.controller.spec.ts
+++ b/src/modules/infra/infra-data.controller.spec.ts
@@ -498,6 +498,62 @@ describe('InfraDataController.importData round-trips export-data (no silent mess
expect(held).toBe(0);
});
+ describe('gives the query runner back when the transaction never opens', () => {
+ const watchRunners = (prepare: (runner: ReturnType<DataSource['createQueryRunner']>) => void) => {
+ const releases: jest.SpyInstance[] = [];
+ const realCreate = ds.createQueryRunner.bind(ds);
+ jest.spyOn(ds, 'createQueryRunner').mockImplementation((...args: Parameters<typeof realCreate>) => {
+ const runner = realCreate(...args);
+ prepare(runner);
+ releases.push(jest.spyOn(runner, 'release'));
+ return runner;
+ });
+ return releases;
+ };
+
+ it('when BEGIN fails', async () => {
+ await seedSession('s1');
+ const dump = await controller.exportData();
+ const releases = watchRunners(runner => {
+ runner.startTransaction = () => Promise.reject(new Error('BEGIN failed'));
+ });
+
+ await expect(controller.importData({ tables: dump.tables })).rejects.toThrow('BEGIN failed');
+
+ expect(releases).toHaveLength(1);
+ expect(releases[0]).toHaveBeenCalledTimes(1);
+ jest.restoreAllMocks();
+ });
+
+ it('when another transaction already holds the connection', async () => {
+ await seedSession('s1');
+ const dump = await controller.exportData();
+ const releases = watchRunners(runner => {
+ Object.defineProperty(runner, 'isTransactionActive', { value: true });
+ });
+
+ await expect(controller.importData({ tables: dump.tables })).rejects.toMatchObject({
+ response: { code: 'IMPORT_NESTED_TRANSACTION' },
+ });
+
+ expect(releases).toHaveLength(1);
+ expect(releases[0]).toHaveBeenCalledTimes(1);
+ jest.restoreAllMocks();
+ });
+
+ it('and still reports the failure of BEGIN when giving the runner back fails too', async () => {
+ await seedSession('s1');
+ const dump = await controller.exportData();
+ watchRunners(runner => {
+ runner.startTransaction = () => Promise.reject(new Error('BEGIN failed'));
+ runner.release = () => Promise.reject(new Error('release failed'));
+ });
+
+ await expect(controller.importData({ tables: dump.tables })).rejects.toThrow('BEGIN failed');
+ jest.restoreAllMocks();
+ });
+ });
+
it('does not suspend loss detection on a dialect where each query runner has its own connection', async () => {
await seedSession('s1');
const dump = await controller.exportData();
diff --git a/src/modules/infra/infra-data.service.ts b/src/modules/infra/infra-data.service.ts
index cd60807a2..c16332ef3 100644
--- a/src/modules/infra/infra-data.service.ts
+++ b/src/modules/infra/infra-data.service.ts
@@ -738,21 +738,27 @@ export class InfraDataService {
// deleted every row — including any the enclosing transaction just wrote. Refusing up front is
// the only answer that is both honest and non-destructive. On Postgres each runner gets its own
// pooled client, so this is always false there and the check costs nothing.
- if (queryRunner.isTransactionActive) {
- throw new ConflictException({
- statusCode: 409,
- error: 'Conflict',
- message:
- 'Another database transaction is in progress on this connection, so a restore could not be ' +
- 'made durable. Retry with no other data operation in flight.',
- code: 'IMPORT_NESTED_TRANSACTION',
- });
+ //
+ // The try/finally that releases the runner opens only after BEGIN, so the refusal and a failing
+ // BEGIN give the runner back here: a pooled PostgreSQL client would otherwise stay checked out for
+ // the life of the process.
+ try {
+ if (queryRunner.isTransactionActive) {
+ throw new ConflictException({
+ statusCode: 409,
+ error: 'Conflict',
+ message:
+ 'Another database transaction is in progress on this connection, so a restore could not be ' +
+ 'made durable. Retry with no other data operation in flight.',
+ code: 'IMPORT_NESTED_TRANSACTION',
+ });
+ }
+ await queryRunner.startTransaction();
+ } catch (error) {
+ await queryRunner.release().catch(() => undefined);
+ throw error;
}
- // startTransaction is inside the runner's own try/finally, so a throw from it still releases
- // the runner instead of stranding it for the life of the process.
- await queryRunner.startTransaction();
-
try {
const isPostgres = this.dataDataSource.options.type === 'postgres';
if (isPostgres) {