changeset 6355:39176cabe65a

Fix a deadlock in ThreadedInstanceLoader
author Alain Mazy <am@orthanc.team>
date Fri, 24 Oct 2025 16:35:47 +0200
parents 36ba64cc1208
children 0d5e5d9df39d
files NEWS OrthancServer/Sources/ServerJobs/ArchiveJob.cpp
diffstat 2 files changed, 21 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/NEWS	Fri Oct 24 15:51:24 2025 +0200
+++ b/NEWS	Fri Oct 24 16:35:47 2025 +0200
@@ -41,6 +41,8 @@
 * Fix issue 252: Disallow colons in HTTP basic usernames
 * Fix: Decoding of LUT with less than 256 entries:
        https://discourse.orthanc-server.org/t/cannot-preview-dicom-file/6275
+* Fix: Possible deadlocks when using ZipLoaderThreads > 1 when the HttpClient disconnects
+       while downloading the response.
 * Upgraded dependencies for static builds:
   - civetweb 1.16, including patch for CVE-2025-55763
 
--- a/OrthancServer/Sources/ServerJobs/ArchiveJob.cpp	Fri Oct 24 15:51:24 2025 +0200
+++ b/OrthancServer/Sources/ServerJobs/ArchiveJob.cpp	Fri Oct 24 16:35:47 2025 +0200
@@ -191,13 +191,15 @@
     boost::mutex                        availableInstancesMutex_;
     SharedMessageQueue                  instancesToPreload_;
     std::vector<boost::thread*>         threads_;
+    bool                                loadersShouldStop_;
 
 
   public:
     ThreadedInstanceLoader(ServerContext& context, size_t threadCount, bool transcode, DicomTransferSyntax transferSyntax, unsigned int lossyQuality)
     : InstanceLoader(context, transcode, transferSyntax, lossyQuality),
       availableInstancesSemaphore_(0),
-      bufferedInstancesSemaphore_(3*threadCount)
+      bufferedInstancesSemaphore_(3*threadCount),
+      loadersShouldStop_(false)
     {
       for (size_t i = 0; i < threadCount; i++)
       {
@@ -215,12 +217,21 @@
       if (threads_.size() > 0)
       {
         LOG(INFO) << "Waiting for loader threads to complete";
+        loadersShouldStop_ = true; // not need to protect this by a mutex.  This is the only "writer" and all loaders are "readers"
 
+        // unlock the loaders if they are waiting on this message queue (this happens when the job completes sucessfully)
         for (size_t i = 0; i < threads_.size(); i++)
         {
           instancesToPreload_.Enqueue(NULL);
         }
 
+        // If the consumer stops e.g. because the HttpClient disconnected, we must make sure the loader threads are not blocked waiting for room in the bufferedInstances.
+        // If the loader threads have completed their jobs, this is harmless to release the bufferedInstances since they won't be used anymore.
+        for (size_t i = 0; i < threads_.size(); i++)
+        {
+          bufferedInstancesSemaphore_.Release();
+        }
+
         for (size_t i = 0; i < threads_.size(); i++)
         {
           if (threads_[i]->joinable())
@@ -247,7 +258,7 @@
       while (true)
       {
         std::unique_ptr<InstanceToPreload> instanceToPreload(dynamic_cast<InstanceToPreload*>(that->instancesToPreload_.Dequeue(0)));
-        if (instanceToPreload.get() == NULL)  // that's the signal to exit the thread
+        if (instanceToPreload.get() == NULL || that->loadersShouldStop_)  // that's the signal to exit the thread
         {
           LOG(INFO) << "Loader thread has completed";
           return;
@@ -1561,6 +1572,12 @@
       
       synchronousTarget_.reset();
       asynchronousTarget_.reset();
+
+      // clear the loader threads
+      if (instanceLoader_.get() != NULL)
+      {
+        instanceLoader_->Clear();
+      }
     }
   }