Selaa lähdekoodia

fix: prevent memory leak

Ryotaro Nagahara 1 kuukausi sitten
vanhempi
sitoutus
46d77879c9

+ 14 - 8
apps/app/src/features/page-bulk-export/server/service/page-bulk-export-job-cron/steps/compress-and-upload.ts

@@ -76,27 +76,33 @@ export async function compressAndUpload(
 
 
   const fileUploadService: FileUploader = this.crowi.fileUploadService;
   const fileUploadService: FileUploader = this.crowi.fileUploadService;
 
 
-  pageArchiver.directory(this.getTmpOutputDir(pageBulkExportJob), false);
-  pageArchiver.finalize();
-
   // Wrap with Node.js native PassThrough so that AWS SDK recognizes the stream as a native Readable
   // Wrap with Node.js native PassThrough so that AWS SDK recognizes the stream as a native Readable
   const uploadStream = new PassThrough();
   const uploadStream = new PassThrough();
+
+  // Establish pipe before finalize to ensure data flows correctly
   pageArchiver.pipe(uploadStream);
   pageArchiver.pipe(uploadStream);
   pageArchiver.on('error', (err) => {
   pageArchiver.on('error', (err) => {
     uploadStream.destroy(err);
     uploadStream.destroy(err);
+    pageArchiver.destroy();
   });
   });
 
 
+  pageArchiver.directory(this.getTmpOutputDir(pageBulkExportJob), false);
+  pageArchiver.finalize();
+
   this.setStreamsInExecution(pageBulkExportJob._id, pageArchiver, uploadStream);
   this.setStreamsInExecution(pageBulkExportJob._id, pageArchiver, uploadStream);
 
 
   try {
   try {
     await fileUploadService.uploadAttachment(uploadStream, attachment);
     await fileUploadService.uploadAttachment(uploadStream, attachment);
+    await postProcess.bind(this)(
+      pageBulkExportJob,
+      attachment,
+      pageArchiver.pointer(),
+    );
   } catch (e) {
   } catch (e) {
     logger.error(e);
     logger.error(e);
     this.handleError(e, pageBulkExportJob);
     this.handleError(e, pageBulkExportJob);
+  } finally {
+    pageArchiver.destroy();
+    uploadStream.destroy();
   }
   }
-  await postProcess.bind(this)(
-    pageBulkExportJob,
-    attachment,
-    pageArchiver.pointer(),
-  );
 }
 }