changeset 6264:de176f458e35

protecting HttpServer::threadCounter_ by a mutex
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 11 Aug 2025 09:11:53 +0200
parents fb8bed996341
children ac34dce1af7d
files OrthancFramework/Sources/HttpServer/HttpServer.cpp OrthancFramework/Sources/HttpServer/HttpServer.h
diffstat 2 files changed, 19 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancFramework/Sources/HttpServer/HttpServer.cpp	Mon Aug 11 08:56:23 2025 +0200
+++ b/OrthancFramework/Sources/HttpServer/HttpServer.cpp	Mon Aug 11 09:11:53 2025 +0200
@@ -1192,10 +1192,12 @@
                                struct mg_connection *connection,
                                const struct mg_request_info *request)
   {
-    bool localhost;
+    server.UpdateCurrentThreadName();
 
     std::unique_ptr<MetricsRegistry::AvailableResourcesDecounter> counter(server.CreateAvailableHttpThreadsDecounter());
 
+    bool localhost;
+
 #if ORTHANC_ENABLE_MONGOOSE == 1
     static const long LOCALHOST = (127ll << 24) + 1ll;
     localhost = (request->remote_ip == LOCALHOST);
@@ -1615,8 +1617,6 @@
     }
   }
 
-  static uint16_t threadCounter = 0;
-
 #if MONGOOSE_USE_CALLBACKS == 0
   static void* Callback(enum mg_event event,
                         struct mg_connection *connection,
@@ -1639,12 +1639,6 @@
   static int Callback(struct mg_connection *connection)
   {
     const struct mg_request_info *request = mg_get_request_info(connection);
-
-    if (!Logging::HasCurrentThreadName())
-    {
-      Logging::SetCurrentThreadName(std::string("HTTP-") + boost::lexical_cast<std::string>(threadCounter++));
-    }
-
     ProtectedCallback(connection, request);
 
     return 1;  // Do not let Mongoose handle the request by itself
@@ -1732,7 +1726,7 @@
   void HttpServer::Start()
   {
     // reset thread counter used to generate HTTP thread names.
-    threadCounter = 0;
+    threadCounter_ = 0;
 
 #if ORTHANC_ENABLE_MONGOOSE == 1
     CLOG(INFO, HTTP) << "Starting embedded Web server using Mongoose";
@@ -2315,4 +2309,14 @@
       return NULL;
     }
   }
+
+
+  void HttpServer::UpdateCurrentThreadName()
+  {
+    if (!Logging::HasCurrentThreadName())
+    {
+      boost::mutex::scoped_lock lock(threadCounterMutex_);
+      Logging::SetCurrentThreadName(std::string("HTTP-") + boost::lexical_cast<std::string>(threadCounter_++));
+    }
+  }
 }
--- a/OrthancFramework/Sources/HttpServer/HttpServer.h	Mon Aug 11 08:56:23 2025 +0200
+++ b/OrthancFramework/Sources/HttpServer/HttpServer.h	Mon Aug 11 09:11:53 2025 +0200
@@ -117,6 +117,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_ = 0;       // Introduced as a global, static variable in Orthanc 1.12.2
+
 #if ORTHANC_ENABLE_PUGIXML == 1
     WebDavBuckets webDavBuckets_;
 #endif
@@ -236,5 +239,7 @@
 
     // Can return NULL if SetMetricsRegistry() was not call beforehand
     MetricsRegistry::AvailableResourcesDecounter* CreateAvailableHttpThreadsDecounter();
+
+    void UpdateCurrentThreadName();
   };
 }