diff OrthancServer/Sources/ServerJobs/ArchiveJob.cpp @ 4641:b02dc8303cf6

Fixed the lifetime of temporary files associated with jobs that create ZIP archive/media
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 27 Apr 2021 09:49:20 +0200
parents f7d5372b59b3
children cdab941fe17d
line wrap: on
line diff
--- a/OrthancServer/Sources/ServerJobs/ArchiveJob.cpp	Mon Apr 26 15:22:44 2021 +0200
+++ b/OrthancServer/Sources/ServerJobs/ArchiveJob.cpp	Tue Apr 27 09:49:20 2021 +0200
@@ -931,7 +931,7 @@
   
   void ArchiveJob::Start()
   {
-    TemporaryFile* target = NULL;
+    TemporaryFile* target = NULL;  // (*)
     
     if (synchronousTarget_.get() == NULL)
     {
@@ -1008,6 +1008,17 @@
     writer_.reset();  // Flush all the results
 
     RefreshArchiveSize();
+
+    if (synchronousTarget_.get() != NULL)
+    {
+      /**
+       * Synchronous behavior: Release the reference to the temporary
+       * file. It is now up to the caller to deal with the shared
+       * pointer. This is a fix in Orthanc 1.9.3.
+       * https://groups.google.com/g/orthanc-users/c/tpP2fkRAd9o/m/5SGpEHbGCQAJ
+       **/
+      synchronousTarget_.reset();
+    }
     
     if (asynchronousTarget_.get() != NULL)
     {
@@ -1055,6 +1066,26 @@
   }
 
 
+  void ArchiveJob::Stop(JobStopReason reason)
+  {
+    /**
+     * New in Orthanc 1.9.3: Remove the temporary file associated with
+     * the job as soon as its job gets canceled (especially visible in
+     * asynchronous mode).
+     **/
+    if (reason == JobStopReason_Canceled ||
+        reason == JobStopReason_Failure ||
+        reason == JobStopReason_Retry)
+    {
+      // First delete the writer, as it holds a reference to "(a)synchronousTarget_", cf. (*)
+      writer_.reset();
+      
+      synchronousTarget_.reset();
+      asynchronousTarget_.reset();
+    }
+  }
+
+
   float ArchiveJob::GetProgress()
   {
     if (writer_.get() == NULL ||