Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(infra): refuse a data export and import that overlap

Yudhi Armyndharisc7c431f11a58
diff --git a/src/modules/infra/infra-data.controller.spec.ts b/src/modules/infra/infra-data.controller.spec.ts
index 3640af006..7790b1fa6 100644
--- a/src/modules/infra/infra-data.controller.spec.ts
+++ b/src/modules/infra/infra-data.controller.spec.ts
@@ -355,6 +355,32 @@ describe('InfraDataController.importData round-trips export-data (no silent mess
await expect(controller.importData({ tables: dump.tables })).resolves.toMatchObject({ imported: true });
});
+ // On better-sqlite3 an export's reads share the import's connection, so they would see its
+ // uncommitted, possibly rolled-back tables and archive a state that is neither the old nor the new DB.
+ it('refuses an export while an import is running', async () => {
+ await seedSession('s1');
+ const dump = await controller.exportData();
+
+ const importing = controller.importData({ tables: dump.tables });
+ const refusal = await controller.exportData().catch((e: unknown) => e);
+ expect(refusal).toBeInstanceOf(ConflictException);
+ expect((refusal as ConflictException).getResponse()).toMatchObject({ code: 'IMPORT_ALREADY_RUNNING' });
+ await expect(importing).resolves.toMatchObject({ imported: true });
+ await expect(controller.exportData()).resolves.toMatchObject({ counts: { sessions: 1 } });
+ });
+
+ it('refuses an import while an export is running', async () => {
+ await seedSession('s1');
+ const dump = await controller.exportData();
+
+ const exporting = controller.exportData();
+ const refusal = await controller.importData({ tables: dump.tables }).catch((e: unknown) => e);
+ expect(refusal).toBeInstanceOf(ConflictException);
+ expect((refusal as ConflictException).getResponse()).toMatchObject({ code: 'EXPORT_IN_PROGRESS' });
+ await expect(exporting).resolves.toMatchObject({ counts: { sessions: 1 } });
+ await expect(controller.importData({ tables: dump.tables })).resolves.toMatchObject({ imported: true });
+ });
+
it('releases the loss-detection token even when the transaction never opens', 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 5700d18dd..4ed192b05 100644
--- a/src/modules/infra/infra-data.service.ts
+++ b/src/modules/infra/infra-data.service.ts
@@ -291,6 +291,13 @@ export class InfraDataService {
*/
private importInFlight = false;
+ /**
+ * Exports running in this process. On better-sqlite3 an export's reads share the import's connection,
+ * so an export and an import that overlap would archive the import's uncommitted (possibly rolled-back)
+ * tables. Each refuses while the other runs.
+ */
+ private exportsInFlight = 0;
+
constructor(
private readonly configService: ConfigService,
@InjectDataSource('data')
@@ -355,7 +362,24 @@ export class InfraDataService {
async exportData(): Promise<InfraExportDataResult> {
this.assertExportRegistryMatchesMetadata();
+ if (this.importInFlight) {
+ throw new ConflictException({
+ statusCode: 409,
+ error: 'Conflict',
+ message: 'A data import is running; export after it finishes.',
+ code: 'IMPORT_ALREADY_RUNNING',
+ });
+ }
+ this.exportsInFlight++;
+ try {
+ return await this.runExport();
+ } finally {
+ this.exportsInFlight--;
+ }
+ }
+ /** The table reads behind exportData, entered only through its import guard. */
+ private async runExport(): Promise<InfraExportDataResult> {
// The tables below may legitimately not exist yet (created by migrations an older DB has not run).
// Only a GENUINE missing-table error (isMissingTableError) may be tolerated — anything else (lock,
// I/O, timeout, aborted connection) must FAIL the export. The old blind `catch { debug-log }`
@@ -491,6 +515,14 @@ export class InfraDataService {
code: 'IMPORT_ALREADY_RUNNING',
});
}
+ if (this.exportsInFlight > 0) {
+ throw new ConflictException({
+ statusCode: 409,
+ error: 'Conflict',
+ message: 'A data export is running; import after it finishes.',
+ code: 'EXPORT_IN_PROGRESS',
+ });
+ }
this.importInFlight = true;
try {
return await this.runImport(data);