changeset 2738:3a55b77339ff

fix deadlock in Lua scripts
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 16 Jul 2018 11:19:30 +0200
parents 8ed9893c9230
children 6ad9873b8c04
files OrthancServer/IServerListener.h OrthancServer/LuaScripting.cpp OrthancServer/LuaScripting.h OrthancServer/ServerContext.cpp OrthancServer/ServerContext.h OrthancServer/main.cpp
diffstat 6 files changed, 58 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/IServerListener.h	Sun Jul 15 16:41:43 2018 +0200
+++ b/OrthancServer/IServerListener.h	Mon Jul 16 11:19:30 2018 +0200
@@ -49,8 +49,8 @@
 
     virtual void SignalStoredInstance(const std::string& publicId,
                                       DicomInstanceToStore& instance,
-                                      const Json::Value& simplifiedTags) = 0;                                      
-
+                                      const Json::Value& simplifiedTags) = 0;
+    
     virtual void SignalChange(const ServerIndexChange& change) = 0;
 
     virtual bool FilterIncomingInstance(const DicomInstanceToStore& instance,
--- a/OrthancServer/LuaScripting.cpp	Sun Jul 15 16:41:43 2018 +0200
+++ b/OrthancServer/LuaScripting.cpp	Mon Jul 16 11:19:30 2018 +0200
@@ -582,6 +582,9 @@
     lua_.RegisterFunction("RestApiPut", RestApiPut);
     lua_.RegisterFunction("RestApiDelete", RestApiDelete);
     lua_.RegisterFunction("GetOrthancConfiguration", GetOrthancConfiguration);
+
+    LOG(INFO) << "Initializing Lua for the event handler";
+    LoadGlobalConfiguration();
   }
 
 
--- a/OrthancServer/LuaScripting.h	Sun Jul 15 16:41:43 2018 +0200
+++ b/OrthancServer/LuaScripting.h	Mon Jul 16 11:19:30 2018 +0200
@@ -33,8 +33,7 @@
 
 #pragma once
 
-#include "IServerListener.h"
-
+#include "DicomInstanceToStore.h"
 #include "ServerJobs/LuaJobManager.h"
 
 #include "../Core/MultiThreading/SharedMessageQueue.h"
@@ -44,7 +43,7 @@
 {
   class ServerContext;
 
-  class LuaScripting : public IServerListener
+  class LuaScripting : public boost::noncopyable
   {
   private:
     enum State
@@ -88,6 +87,8 @@
 
     static void EventThread(LuaScripting* that);
 
+    void LoadGlobalConfiguration();
+
   public:
     class Lock : public boost::noncopyable
     {
@@ -116,19 +117,17 @@
 
     void Stop();
     
-    virtual void SignalStoredInstance(const std::string& publicId,
-                                      DicomInstanceToStore& instance,
-                                      const Json::Value& simplifiedTags);
+    void SignalStoredInstance(const std::string& publicId,
+                              DicomInstanceToStore& instance,
+                              const Json::Value& simplifiedTags);
 
-    virtual void SignalChange(const ServerIndexChange& change);
+    void SignalChange(const ServerIndexChange& change);
 
-    virtual bool FilterIncomingInstance(const DicomInstanceToStore& instance,
-                                        const Json::Value& simplifiedTags);
+    bool FilterIncomingInstance(const DicomInstanceToStore& instance,
+                                const Json::Value& simplifiedTags);
 
     void Execute(const std::string& command);
 
-    void LoadGlobalConfiguration();
-
     void SignalJobSubmitted(const std::string& jobId);
 
     void SignalJobSuccess(const std::string& jobId);
--- a/OrthancServer/ServerContext.cpp	Sun Jul 15 16:41:43 2018 +0200
+++ b/OrthancServer/ServerContext.cpp	Mon Jul 16 11:19:30 2018 +0200
@@ -135,21 +135,21 @@
   void ServerContext::SignalJobSubmitted(const std::string& jobId)
   {
     haveJobsChanged_ = true;
-    lua_.SignalJobSubmitted(jobId);
+    mainLua_.SignalJobSubmitted(jobId);
   }
   
 
   void ServerContext::SignalJobSuccess(const std::string& jobId)
   {
     haveJobsChanged_ = true;
-    lua_.SignalJobSuccess(jobId);
+    mainLua_.SignalJobSuccess(jobId);
   }
 
   
   void ServerContext::SignalJobFailure(const std::string& jobId)
   {
     haveJobsChanged_ = true;
-    lua_.SignalJobFailure(jobId);
+    mainLua_.SignalJobFailure(jobId);
   }
 
 
@@ -225,7 +225,9 @@
     storeMD5_(true),
     provider_(*this),
     dicomCache_(provider_, DICOM_CACHE_SIZE),
-    lua_(*this),
+    mainLua_(*this),
+    filterLua_(*this),
+    luaListener_(*this),
 #if ORTHANC_ENABLE_PLUGINS == 1
     plugins_(NULL),
 #endif
@@ -234,7 +236,7 @@
     queryRetrieveArchive_(Configuration::GetGlobalUnsignedIntegerParameter("QueryRetrieveSize", 10)),
     defaultLocalAet_(Configuration::GetGlobalStringParameter("DicomAet", "ORTHANC"))
   {
-    listeners_.push_back(ServerListener(lua_, "Lua"));
+    listeners_.push_back(ServerListener(luaListener_, "Lua"));
 
     SetupJobsEngine(unitTesting, loadJobsFromDatabase);
 
@@ -699,7 +701,7 @@
 
     // TODO REFACTOR THIS
     listeners_.clear();
-    listeners_.push_back(ServerListener(lua_, "Lua"));
+    listeners_.push_back(ServerListener(luaListener_, "Lua"));
     listeners_.push_back(ServerListener(plugins, "plugin"));
   }
 
@@ -712,7 +714,7 @@
 
     // TODO REFACTOR THIS
     listeners_.clear();
-    listeners_.push_back(ServerListener(lua_, "Lua"));
+    listeners_.push_back(ServerListener(luaListener_, "Lua"));
   }
 
 
--- a/OrthancServer/ServerContext.h	Sun Jul 15 16:41:43 2018 +0200
+++ b/OrthancServer/ServerContext.h	Mon Jul 16 11:19:30 2018 +0200
@@ -63,6 +63,36 @@
   class ServerContext : private JobsRegistry::IObserver
   {
   private:
+    class LuaServerListener : public IServerListener
+    {
+    private:
+      ServerContext& context_;
+
+    public:
+      LuaServerListener(ServerContext& context) :
+        context_(context)
+      {
+      }
+
+      virtual void SignalStoredInstance(const std::string& publicId,
+                                        DicomInstanceToStore& instance,
+                                        const Json::Value& simplifiedTags)
+      {
+        context_.mainLua_.SignalStoredInstance(publicId, instance, simplifiedTags);
+      }
+    
+      virtual void SignalChange(const ServerIndexChange& change)
+      {
+        context_.mainLua_.SignalChange(change);
+      }
+
+      virtual bool FilterIncomingInstance(const DicomInstanceToStore& instance,
+                                          const Json::Value& simplified)
+      {
+        return context_.filterLua_.FilterIncomingInstance(instance, simplified);
+      }
+    };
+    
     class DicomCacheProvider : public ICachePageProvider
     {
     private:
@@ -135,7 +165,9 @@
     MemoryCache dicomCache_;
     JobsEngine jobsEngine_;
 
-    LuaScripting lua_;
+    LuaScripting mainLua_;
+    LuaScripting filterLua_;
+    LuaServerListener  luaListener_;
 
 #if ORTHANC_ENABLE_PLUGINS == 1
     OrthancPlugins* plugins_;
@@ -279,7 +311,7 @@
 
     LuaScripting& GetLuaScripting()
     {
-      return lua_;
+      return mainLua_;
     }
 
     OrthancHttpHandler& GetHttpHandler()
--- a/OrthancServer/main.cpp	Sun Jul 15 16:41:43 2018 +0200
+++ b/OrthancServer/main.cpp	Mon Jul 16 11:19:30 2018 +0200
@@ -995,9 +995,6 @@
     context.GetIndex().SetMaximumStorageSize(0);
   }
 
-  LOG(INFO) << "Initializing Lua for the event handler";
-  context.GetLuaScripting().LoadGlobalConfiguration();
-
   context.GetJobsEngine().GetRegistry().SetMaxCompletedJobs
     (Configuration::GetGlobalUnsignedIntegerParameter("JobsHistorySize", 10));