Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(infra): remove the partial archive of a failed storage export

Yudhi Armyndharis51c0b29965fa
diff --git a/src/modules/infra/infra-storage.controller.spec.ts b/src/modules/infra/infra-storage.controller.spec.ts
index 346fe0302..518ee4d2f 100644
--- a/src/modules/infra/infra-storage.controller.spec.ts
+++ b/src/modules/infra/infra-storage.controller.spec.ts
@@ -258,9 +258,12 @@ describe('InfraStorageController storage stream failures surface as request erro
try {
// pipe() never forwards source errors: without a listener on the source this would be an
// unhandled 'error' event and kill the process instead of failing the request.
+ // Some bytes land first, so the failure leaves a partial archive behind unless it is removed.
+ let reads = 0;
const errStream = new Readable({
read() {
- this.destroy(new Error('archive boom'));
+ if (reads++ === 0) this.push(Buffer.alloc(1000));
+ else this.destroy(new Error('archive boom'));
},
});
const storage = {
@@ -269,6 +272,7 @@ describe('InfraStorageController storage stream failures surface as request erro
};
const controller = new InfraStorageController(storage as never);
await expect(controller.exportStorage()).rejects.toThrow(/archive boom/);
+ expect(fs.readdirSync(path.join(cwd, 'data', 'exports'))).toEqual([]);
} finally {
cwdSpy.mockRestore();
fs.rmSync(cwd, { recursive: true, force: true });
diff --git a/src/modules/infra/infra-storage.controller.ts b/src/modules/infra/infra-storage.controller.ts
index c157eca9f..3742f4e17 100644
--- a/src/modules/infra/infra-storage.controller.ts
+++ b/src/modules/infra/infra-storage.controller.ts
@@ -178,22 +178,31 @@ export class InfraStorageController implements OnApplicationBootstrap {
const writeStream = fs.createWriteStream(exportPath);
stream.pipe(writeStream);
- await new Promise<void>((resolve, reject) => {
- writeStream.on('finish', resolve);
- // The archive is filled while it is written: destroying the source stops the export from
- // opening further files once the sink has failed.
- writeStream.on('error', (err: Error) => {
- stream.destroy();
- reject(err);
- });
- // pipe() does NOT forward source errors: an archiver/gzip failure surfaces as an 'error' event on
- // the source stream, which without a listener crashes the process. Fail the request instead and
- // tear down the sink so its fd isn't held open waiting for a 'finish' that never comes.
- stream.on('error', (err: Error) => {
- writeStream.destroy();
- reject(err);
+ try {
+ await new Promise<void>((resolve, reject) => {
+ writeStream.on('finish', resolve);
+ // The archive is filled while it is written: destroying the source stops the export from
+ // opening further files once the sink has failed.
+ writeStream.on('error', (err: Error) => {
+ stream.destroy();
+ reject(err);
+ });
+ // pipe() does NOT forward source errors: an archiver/gzip failure surfaces as an 'error' event on
+ // the source stream, which without a listener crashes the process. Fail the request instead and
+ // tear down the sink so its fd isn't held open waiting for a 'finish' that never comes.
+ stream.on('error', (err: Error) => {
+ writeStream.destroy();
+ reject(err);
+ });
});
- });
+ } catch (error) {
+ // The TTL sweep below only covers a finished archive; a failed one would otherwise stay on the
+ // data volume until a restart a day later. Wait for the sink to close so the unlink cannot race
+ // an open that is still pending.
+ if (!writeStream.closed) await new Promise(resolve => writeStream.once('close', resolve));
+ await fs.promises.unlink(exportPath).catch(() => undefined);
+ throw error;
+ }
// Sweep the throwaway archive so repeated exports don't accumulate on the data volume.
const ttlRaw = Number.parseInt(process.env.STORAGE_EXPORT_TTL_MS ?? '', 10);