changeset 6731:d691dafe5ca8

ClearThreadName to reduce the size of threadNames_ map that could contain thousands of loader thread ids or dead thread ids after a /tools/reset
author Alain Mazy <am@orthanc.team>
date Tue, 21 Apr 2026 12:49:25 +0200
parents c225552d8824
children b72dc46eb85c
files OrthancFramework/Sources/DicomNetworking/DicomServer.cpp OrthancFramework/Sources/JobsEngine/JobsEngine.cpp OrthancFramework/Sources/Logging.cpp OrthancFramework/Sources/Logging.h OrthancFramework/Sources/MultiThreading/RunnableWorkersPool.cpp OrthancServer/Sources/LuaScripting.cpp OrthancServer/Sources/OrthancWebDav.cpp OrthancServer/Sources/ServerContext.cpp OrthancServer/Sources/ServerIndex.cpp OrthancServer/Sources/ServerJobs/ThreadedInstancesLoader.cpp OrthancServer/Sources/ServerJobs/ThreadedSetOfInstancesJob.cpp
diffstat 11 files changed, 61 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancFramework/Sources/DicomNetworking/DicomServer.cpp	Tue Apr 21 11:40:30 2026 +0200
+++ b/OrthancFramework/Sources/DicomNetworking/DicomServer.cpp	Tue Apr 21 12:49:25 2026 +0200
@@ -63,7 +63,7 @@
                                  unsigned int maximumPduLength,
                                  bool useDicomTls)
   {
-    Logging::SetCurrentThreadName("DICOM-SERVER");
+    Logging::ScopedThreadNameSetter setter("DICOM-SERVER");
     CLOG(INFO, DICOM) << "DICOM server started";
 
     while (server->continue_)
--- a/OrthancFramework/Sources/JobsEngine/JobsEngine.cpp	Tue Apr 21 11:40:30 2026 +0200
+++ b/OrthancFramework/Sources/JobsEngine/JobsEngine.cpp	Tue Apr 21 12:49:25 2026 +0200
@@ -109,7 +109,7 @@
     
   void JobsEngine::RetryHandler(JobsEngine* engine)
   {
-    Logging::SetCurrentThreadName("JOBS-RETRY");
+    Logging::ScopedThreadNameSetter setter("JOBS-RETRY");
 
     assert(engine != NULL);
 
@@ -125,7 +125,7 @@
                           size_t workerIndex)
   {
     assert(engine != NULL);
-    Logging::SetCurrentThreadName(std::string("JOBS-WORKER-") + boost::lexical_cast<std::string>(workerIndex));
+    Logging::ScopedThreadNameSetter setter(std::string("JOBS-WORKER-") + boost::lexical_cast<std::string>(workerIndex));
     CLOG(INFO, JOBS) << "Worker thread " << workerIndex << " has started";
 
     while (engine->IsRunning())
--- a/OrthancFramework/Sources/Logging.cpp	Tue Apr 21 11:40:30 2026 +0200
+++ b/OrthancFramework/Sources/Logging.cpp	Tue Apr 21 12:49:25 2026 +0200
@@ -693,6 +693,14 @@
       return threadNames_.find(threadId) != threadNames_.end();
     }
 
+    void ClearCurrentThreadName()
+    {
+      boost::thread::id threadId = boost::this_thread::get_id();
+
+      boost::recursive_mutex::scoped_lock lock(threadNamesMutex_);
+      threadNames_.erase(threadId);
+    }
+
     static std::string GetCurrentThreadName()
     {
       boost::thread::id threadId = boost::this_thread::get_id();
@@ -708,6 +716,17 @@
       return threadNames_[threadId];
     }    
 
+    ScopedThreadNameSetter::ScopedThreadNameSetter(const std::string& threadName)
+    {
+      SetCurrentThreadName(threadName);
+    }
+
+    ScopedThreadNameSetter::~ScopedThreadNameSetter()
+    {
+      ClearCurrentThreadName();
+    }
+
+
     void AddLoggingListener(ILoggingListener* listener)
     {
       boost::mutex::scoped_lock lock(loggingListenersMutex_);
--- a/OrthancFramework/Sources/Logging.h	Tue Apr 21 11:40:30 2026 +0200
+++ b/OrthancFramework/Sources/Logging.h	Tue Apr 21 12:49:25 2026 +0200
@@ -75,6 +75,14 @@
       LogCategory_LUA     = (1 << 6)
     };
 
+    class ScopedThreadNameSetter
+    {
+    public:
+      ScopedThreadNameSetter(const std::string& threadName);
+
+      ~ScopedThreadNameSetter();
+    };
+
     class ILoggingListener
     {
     public:
@@ -110,6 +118,8 @@
 
     ORTHANC_PUBLIC bool HasCurrentThreadName();
 
+    ORTHANC_PUBLIC void ClearCurrentThreadName();
+
     ORTHANC_PUBLIC void AddLoggingListener(ILoggingListener* listener);
 
     ORTHANC_PUBLIC void ClearLoggingListeners();
--- a/OrthancFramework/Sources/MultiThreading/RunnableWorkersPool.cpp	Tue Apr 21 11:40:30 2026 +0200
+++ b/OrthancFramework/Sources/MultiThreading/RunnableWorkersPool.cpp	Tue Apr 21 12:49:25 2026 +0200
@@ -48,7 +48,7 @@
  
       static void WorkerThread(Worker* that)
       {
-        Logging::SetCurrentThreadName(that->threadName_);
+        Logging::ScopedThreadNameSetter setter(that->threadName_);
 
         while (that->continue_)
         {
--- a/OrthancServer/Sources/LuaScripting.cpp	Tue Apr 21 11:40:30 2026 +0200
+++ b/OrthancServer/Sources/LuaScripting.cpp	Tue Apr 21 12:49:25 2026 +0200
@@ -856,7 +856,7 @@
 
   void LuaScripting::HeartBeatThread(LuaScripting* that)
   {
-    Logging::SetCurrentThreadName("LUA-HEARTBEAT");
+    Logging::ScopedThreadNameSetter setter("LUA-HEARTBEAT");
 
     static const unsigned int GRANULARITY = 100;  // In milliseconds
     
@@ -894,7 +894,7 @@
 
   void LuaScripting::EventThread(LuaScripting* that)
   {
-    Logging::SetCurrentThreadName("LUA-EVENTS");
+    Logging::ScopedThreadNameSetter setter("LUA-EVENTS");
 
     for (;;)
     {
--- a/OrthancServer/Sources/OrthancWebDav.cpp	Tue Apr 21 11:40:30 2026 +0200
+++ b/OrthancServer/Sources/OrthancWebDav.cpp	Tue Apr 21 12:49:25 2026 +0200
@@ -1163,7 +1163,7 @@
 
   void OrthancWebDav::UploadWorker(OrthancWebDav* that)
   {
-    Logging::SetCurrentThreadName("WEBDAV-UPLOAD");
+    Logging::ScopedThreadNameSetter setter("WEBDAV-UPLOAD");
 
     assert(that != NULL);
 
--- a/OrthancServer/Sources/ServerContext.cpp	Tue Apr 21 11:40:30 2026 +0200
+++ b/OrthancServer/Sources/ServerContext.cpp	Tue Apr 21 12:49:25 2026 +0200
@@ -116,7 +116,7 @@
   void ServerContext::MemoryTrimmingThread(ServerContext* that,
                                            unsigned int intervalInSeconds)
   {
-    Logging::SetCurrentThreadName("MEMORY-TRIM");
+    Logging::ScopedThreadNameSetter setter("MEMORY-TRIM");
 
     boost::posix_time::ptime lastExecution = boost::posix_time::second_clock::universal_time();
 
@@ -146,7 +146,7 @@
   void ServerContext::ChangeThread(ServerContext* that,
                                    unsigned int sleepDelay)
   {
-    Logging::SetCurrentThreadName("CHANGES");
+    Logging::ScopedThreadNameSetter setter("CHANGES");
 
     while (!that->done_)
     {
@@ -190,7 +190,7 @@
   void ServerContext::JobEventsThread(ServerContext* that,
                                       unsigned int sleepDelay)
   {
-    Logging::SetCurrentThreadName("JOB-EVENTS");
+    Logging::ScopedThreadNameSetter setter("JOB-EVENTS");
 
     while (!that->done_)
     {
@@ -234,7 +234,7 @@
   void ServerContext::SaveJobsThread(ServerContext* that,
                                      unsigned int sleepDelay)
   {
-    Logging::SetCurrentThreadName("SAVE-JOBS");
+    Logging::ScopedThreadNameSetter setter("SAVE-JOBS");
 
     static const boost::posix_time::time_duration PERIODICITY =
       boost::posix_time::seconds(10);
--- a/OrthancServer/Sources/ServerIndex.cpp	Tue Apr 21 11:40:30 2026 +0200
+++ b/OrthancServer/Sources/ServerIndex.cpp	Tue Apr 21 12:49:25 2026 +0200
@@ -276,7 +276,7 @@
   void ServerIndex::FlushThread(ServerIndex* that,
                                 unsigned int threadSleepGranularityMilliseconds)
   {
-    Logging::SetCurrentThreadName("DB-FLUSH");
+    Logging::ScopedThreadNameSetter setter("DB-FLUSH");
 
     // By default, wait for 10 seconds before flushing
     static const unsigned int SLEEP_SECONDS = 10;
@@ -458,7 +458,7 @@
   void ServerIndex::UnstableResourcesMonitorThread(ServerIndex* that,
                                                    unsigned int threadSleepGranularityMilliseconds)
   {
-    Logging::SetCurrentThreadName("UNSTABLE-MON");
+    Logging::ScopedThreadNameSetter setter("UNSTABLE-MON");
 
     int stableAge;
     
--- a/OrthancServer/Sources/ServerJobs/ThreadedInstancesLoader.cpp	Tue Apr 21 11:40:30 2026 +0200
+++ b/OrthancServer/Sources/ServerJobs/ThreadedInstancesLoader.cpp	Tue Apr 21 12:49:25 2026 +0200
@@ -30,6 +30,15 @@
 static boost::mutex loaderThreadsCounterMutex;
 static uint32_t loaderThreadsCounter = 0;
 
+static std::string GetLoaderThreadName(const std::string& prefix)
+{
+  boost::mutex::scoped_lock lock(loaderThreadsCounterMutex);
+  std::string threadName = prefix + std::string("-LOAD-") +
+                                boost::lexical_cast<std::string>(loaderThreadsCounter++);
+  loaderThreadsCounter %= 1000000;
+
+  return threadName;
+}
 
 namespace Orthanc
 {
@@ -146,12 +155,7 @@
 
   void ThreadedInstancesLoader::PreloaderWorkerThread(ThreadedInstancesLoader* that)
   {
-    {
-      boost::mutex::scoped_lock lock(loaderThreadsCounterMutex);
-      Logging::SetCurrentThreadName(that->nameForLogs4Char_ + std::string("-LOAD-") +
-                                    boost::lexical_cast<std::string>(loaderThreadsCounter++));
-      loaderThreadsCounter %= 1000000;
-    }
+    Logging::ScopedThreadNameSetter setter(::GetLoaderThreadName(that->nameForLogs4Char_));
 
     LOG(INFO) << "Loader thread has started";
 
--- a/OrthancServer/Sources/ServerJobs/ThreadedSetOfInstancesJob.cpp	Tue Apr 21 11:40:30 2026 +0200
+++ b/OrthancServer/Sources/ServerJobs/ThreadedSetOfInstancesJob.cpp	Tue Apr 21 12:49:25 2026 +0200
@@ -34,6 +34,14 @@
 static boost::mutex instanceWorkerThreadsCounterMutex;
 static uint32_t instanceWorkerThreadsCounter = 0;
 
+static std::string GetInstanceWorkerThreadName()
+{
+  boost::mutex::scoped_lock lock(instanceWorkerThreadsCounterMutex);
+  std::string threadName = std::string("JOB-INS-WORK-") + boost::lexical_cast<std::string>(instanceWorkerThreadsCounter++);
+  instanceWorkerThreadsCounter %= 1000;
+
+  return threadName;
+}
 
 namespace Orthanc
 {
@@ -248,11 +256,7 @@
 
   void ThreadedSetOfInstancesJob::InstanceWorkerThread(ThreadedSetOfInstancesJob* that)
   {
-    {
-      boost::mutex::scoped_lock lock(instanceWorkerThreadsCounterMutex);
-      Logging::SetCurrentThreadName(std::string("JOB-INS-WORK-") + boost::lexical_cast<std::string>(instanceWorkerThreadsCounter++));
-      instanceWorkerThreadsCounter %= 1000;
-    }
+    Logging::ScopedThreadNameSetter setter(::GetInstanceWorkerThreadName());
 
     while (true)
     {