changeset 7041:dbad0dbcbbc3

make sure we don't reused HTTP-XX thread names after a call to /tools/reset
author Alain Mazy <am@orthanc.team>
date Mon, 10 Aug 2026 16:53:01 +0200
parents dca83277bdde
children de713a31cbd3
files OrthancFramework/Sources/HttpServer/HttpServer.cpp OrthancFramework/Sources/HttpServer/HttpServer.h
diffstat 2 files changed, 25 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancFramework/Sources/HttpServer/HttpServer.cpp	Mon Aug 10 15:33:26 2026 +0200
+++ b/OrthancFramework/Sources/HttpServer/HttpServer.cpp	Mon Aug 10 16:53:01 2026 +0200
@@ -1280,7 +1280,10 @@
                                struct mg_connection *connection,
                                const struct mg_request_info *request)
   {
-    server.UpdateCurrentThreadName();
+    // Since we don't control the thread creation ourselves, we set and clear 
+    // the thread name everytime otherwise, the thread name will persist even 
+    // after a call to /tools/reset and Orthanc would stop because the same thread name is used multiple times
+    Logging::ScopedCurrentThreadNameSetter setter(server.GetCurrentThreadName()); 
 
     std::unique_ptr<MetricsRegistry::AvailableResourcesDecounter> counter(server.CreateAvailableHttpThreadsDecounter());
 
@@ -2467,13 +2470,25 @@
   }
 
 
-  void HttpServer::UpdateCurrentThreadName()
+  std::string HttpServer::GetCurrentThreadName()
   {
     // threads are created in CivetWeb -> assign them a name the first time they are used
-    if (!Logging::HasCurrentThreadName())
+    boost::thread::id threadId = boost::this_thread::get_id();
+
+    boost::upgrade_lock<boost::shared_mutex> readerLock(threadCounterMutex_); // reader lock to check if the threadId has already been registered
+
+    if (threadNames_.find(threadId) == threadNames_.end())
     {
-      boost::mutex::scoped_lock lock(threadCounterMutex_);
-      Logging::SetCurrentThreadName(std::string("HTTP-") + boost::lexical_cast<std::string>(threadCounter_++));
+      boost::upgrade_to_unique_lock<boost::shared_mutex> writerLock(readerLock);
+      
+      std::string thisThreadName = std::string("HTTP-") + boost::lexical_cast<std::string>(threadCounter_++);
+      threadNames_[threadId] = thisThreadName;
+      
+      return thisThreadName;
+    }
+    else
+    {
+      return threadNames_[threadId];
     }
   }
 
--- a/OrthancFramework/Sources/HttpServer/HttpServer.h	Mon Aug 10 15:33:26 2026 +0200
+++ b/OrthancFramework/Sources/HttpServer/HttpServer.h	Mon Aug 10 16:53:01 2026 +0200
@@ -54,6 +54,7 @@
 #include <stdint.h>
 #include <boost/shared_ptr.hpp>
 #include <boost/filesystem.hpp>
+#include <boost/thread.hpp>
 
 
 namespace Orthanc
@@ -118,8 +119,9 @@
     unsigned int requestTimeout_;  // In seconds
     std::unique_ptr<MetricsRegistry::SharedMetrics> availableHttpThreadsMetrics_;  // New in Orthanc 1.12.9
 
-    boost::mutex threadCounterMutex_;  // New in Orthanc 1.12.9
-    uint16_t threadCounter_;           // Introduced as a global, static variable in Orthanc 1.12.2
+    boost::shared_mutex threadCounterMutex_;  // protects threadCounter_ and threadNames_;
+    uint16_t threadCounter_;
+    std::map<boost::thread::id, std::string> threadNames_;
 
     // New in Orthanc 1.12.11
     bool    hasMaxBodySize_;
@@ -247,7 +249,7 @@
     // Can return NULL if SetMetricsRegistry() was not call beforehand
     MetricsRegistry::AvailableResourcesDecounter* CreateAvailableHttpThreadsDecounter();
 
-    void UpdateCurrentThreadName();
+    std::string GetCurrentThreadName();
 
     void SetMaxBodySize(uint64_t size);