Salome HOME
[EDF29150] : Fix bug at shutdown
authorAnthony Geay <anthony.geay@edf.fr>
Mon, 19 Feb 2024 12:31:01 +0000 (13:31 +0100)
committerAnthony Geay <anthony.geay@edf.fr>
Mon, 19 Feb 2024 13:13:46 +0000 (14:13 +0100)
idl/SALOME_LogManager.idl
src/Basics/PythonCppUtils.hxx
src/Container/Container_i.cxx
src/Container/SALOME_Container.py
src/Launcher/SALOME_LogManager.cxx
src/Launcher/SALOME_LogManager.hxx

index d7ef490d47dce86e3df3e9d5e168175c37be09bb..da941f112b9ce83b6020aff35d040d2a212b4417 100644 (file)
@@ -49,6 +49,7 @@ module Engines
     string getContainerEntryInNS();
     ContainerScriptPerfLog addScript(in string name, in string code);
     ListOfContainerScriptPerfLog listOfScripts();
+    void destroy();
   };
 
   typedef sequence<ContainerPerfLog> ListOfContainerPerfLog;
index 6330ae5ed0ac3eca785385915fdd413d711f5e48..9050055668d35be50a9787b9cac813ebfd944faf 100644 (file)
@@ -34,7 +34,7 @@ class AutoPyRef
 {
 public:
   AutoPyRef(PyObject *pyobj=nullptr):_pyobj(pyobj) { }
-  ~AutoPyRef() { release(); }
+  virtual ~AutoPyRef() { release(); }
   AutoPyRef(const AutoPyRef& other):_pyobj(other._pyobj) { if(_pyobj) Py_XINCREF(_pyobj); }
   AutoPyRef(AutoPyRef&& other) = default;
   AutoPyRef& operator=(const AutoPyRef& other) { if(_pyobj==other._pyobj) return *this; release(); _pyobj=other._pyobj; Py_XINCREF(_pyobj); return *this; }
@@ -43,8 +43,15 @@ public:
   PyObject *get() { return _pyobj; }
   bool isNull() const { return _pyobj==0; }
   PyObject *retn() { if(_pyobj) Py_XINCREF(_pyobj); return _pyobj; }
-private:
-  void release() { if(_pyobj) Py_XDECREF(_pyobj); _pyobj=0; }
+protected:
+  void release() { if(_pyobj) Py_XDECREF(_pyobj); _pyobj=nullptr; }
 private:
   PyObject *_pyobj = nullptr;
 };
+
+class AutoPyRefGilSafe : public AutoPyRef
+{
+public:
+  AutoPyRefGilSafe(PyObject *pyobj=nullptr):AutoPyRef(pyobj) { }
+  ~AutoPyRefGilSafe() { AutoGIL agil; release(); }
+};
index 249e9847327ab51f18d9d168ab43b34fa927cda4..872e161ab7c5e9ba19a203975d59d6c4e4cf7166 100644 (file)
@@ -117,7 +117,7 @@ std::map<std::string, void *> Abstract_Engines_Container_i::_library_map;
 std::map<std::string, void *> Abstract_Engines_Container_i::_toRemove_map;
 omni_mutex Abstract_Engines_Container_i::_numInstanceMutex ;
 
-static PyObject* _pyCont;
+static PyObject *_pyCont = nullptr;
 
 int checkifexecutable(const std::string&);
 int findpathof(const std::string& path, std::string&, const std::string&);
@@ -748,6 +748,11 @@ void Abstract_Engines_Container_i::Shutdown()
   //
   this->cleanAllPyScripts();
   //
+  {
+    AutoGIL gstate;
+    AutoPyRef result = PyObject_CallMethod(_pyCont, (char*)"shutdownPy", (char*)"",nullptr);
+  }
+  //
   if(_isServantAloneInProcess)
   {
     MESSAGE("Effective Shutdown of container Begins...");
index 096385d94cf8c6b8a8b99c0166972849fa15b0d9..b4639588c9109065b89d16ced4e68ca118993d33 100644 (file)
@@ -180,6 +180,11 @@ class SALOME_Container_i:
     def monitoringtimeresms(self):
         return self._timeIntervalInMs
     
+    def shutdownPy(self):
+        if getSSLMode():
+           if self._log:
+              self._log.destroy()
+    
     def setLogFileName(self, logFileName):
         logging.debug("setLogFileName {} PID = {}".format(logFileName,os.getpid()))
         if getSSLMode():
index 6e106965e835104b7ae92f6cfb31f7ed0f068e41..f336d56a22c29e7c2b25c8ccbe7238aa4cb68eff 100644 (file)
@@ -193,13 +193,7 @@ Engines::ListOfContainerScriptExecPerfLog *SALOME_ContainerScriptPerfLog::listOf
 
 SALOME_ContainerPerfLog::~SALOME_ContainerPerfLog()
 {
-  for(auto script : _scripts)
-  {
-    PortableServer::ServantBase *serv = getPOA()->reference_to_servant(script);
-    PortableServer::ObjectId_var oid = getPOA()->reference_to_id(script);
-    getPOA()->deactivate_object(oid);
-  }
-  _scripts.clear();
+  this->destroyInternal();
 }
 
 PortableServer::POA_var SALOME_ContainerPerfLog::getPOA()
@@ -239,6 +233,23 @@ Engines::ListOfContainerScriptPerfLog *SALOME_ContainerPerfLog::listOfScripts()
   return ret._retn();
 }
 
+void SALOME_ContainerPerfLog::destroy()
+{
+  this->destroyInternal();
+}
+
+void SALOME_ContainerPerfLog::destroyInternal()
+{
+  _father->removeEntryBeforeDying( this );
+  for(auto script : _scripts)
+  {
+    PortableServer::ServantBase *serv = getPOA()->reference_to_servant(script);
+    PortableServer::ObjectId_var oid = getPOA()->reference_to_id(script);
+    getPOA()->deactivate_object(oid);
+  }
+  _scripts.clear();
+}
+
 void SALOME_ContainerPerfLog::accept(SALOME_VisitorContainerLog &visitor)
 {
   visitor.enterContainerPerfLog( *this );
@@ -400,6 +411,20 @@ char *SALOME_LogManager::getLastVersionOfFileNameLogger()
   return CORBA::string_dup( _safe_logger_file_holder.getLastVersionOfFileNameLogger().c_str() );
 }
 
+void SALOME_LogManager::removeEntryBeforeDying(SALOME_ContainerPerfLog *child)
+{
+  for(auto cont = _containers.begin() ; cont != _containers.end() ; ++cont )
+  {
+    PortableServer::ServantBase *serv = getPOA()->reference_to_servant(*cont);
+    SALOME_ContainerPerfLog *servCand = dynamic_cast<SALOME_ContainerPerfLog *>(serv);
+    if( servCand==child )
+      {
+        _containers.erase( cont );
+        break;
+      }
+  }
+}
+
 ///////////////////////
  
  #include <cstdint>
index 89735aa3fb26bb49e60a887be9de1133d08f0a2d..44948e4aa638faa4220863514e599c5ee700ceeb 100644 (file)
@@ -70,7 +70,7 @@ public:
   AutoPyRef end();
   void clear() { _data.clear(); }
 private:
-  AutoPyRef _pyExecutionLog;
+  AutoPyRefGilSafe _pyExecutionLog;
   SALOME_ContainerScriptPerfLog *_father = nullptr;
   std::vector<char> _data;
 };
@@ -96,7 +96,7 @@ public:
 public:
   void accept(SALOME_VisitorContainerLog &visitor);
 private:
-  AutoPyRef _pyScriptLog;
+  AutoPyRefGilSafe _pyScriptLog;
   SALOME_ContainerPerfLog *_father = nullptr;
   std::string _name;
   std::string _code;
@@ -116,6 +116,7 @@ public:
   char *getContainerEntryInNS() override;
   Engines::ContainerScriptPerfLog_ptr addScript(const char *name, const char *code) override;
   Engines::ListOfContainerScriptPerfLog *listOfScripts() override;
+  void destroy() override;
   const std::string& nameInNS() const { return _name_in_ns; }
   const std::string& logFile() const { return _log_file; }
   void setNameInNS(const std::string& name) { _name_in_ns = name; }
@@ -124,7 +125,9 @@ public:
 public:
   void accept(SALOME_VisitorContainerLog &visitor);
 private:
-  AutoPyRef _pyContLog;
+  void destroyInternal();
+private:
+  AutoPyRefGilSafe _pyContLog;
   SALOME_LogManager *_father = nullptr;
   std::string _name_in_ns;
   std::string _log_file;
@@ -166,6 +169,8 @@ class SALOMELAUNCHER_EXPORT SALOME_LogManager : public POA_Engines::LogManager
   void versionA_IsTheLatestValidVersion() override { _safe_logger_file_holder.versionA_IsTheLatestValidVersion(); }
   void versionB_IsTheLatestValidVersion() override { _safe_logger_file_holder.versionB_IsTheLatestValidVersion(); }
   char *getLastVersionOfFileNameLogger() override;
+ public:
+  void removeEntryBeforeDying(SALOME_ContainerPerfLog *child);
  public:
   std::size_t getNumberOfContainers() const { return _containers.size(); }
  public: