# HG changeset patch # User Alain Mazy # Date 1761316547 -7200 # Node ID 39176cabe65a170dbdb94c1658687c73f5395f30 # Parent 36ba64cc12082bce7b5c0d81ce21922a5aa27a98 Fix a deadlock in ThreadedInstanceLoader diff -r 36ba64cc1208 -r 39176cabe65a NEWS --- 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 diff -r 36ba64cc1208 -r 39176cabe65a OrthancServer/Sources/ServerJobs/ArchiveJob.cpp --- 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 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(dynamic_cast(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(); + } } }