From bf637648a815c03f19b1c3584b6e924343aa6387 Mon Sep 17 00:00:00 2001 From: Anthony Geay Date: Mon, 19 Feb 2024 13:31:01 +0100 Subject: [PATCH] [EDF29150] : Fix bug at shutdown --- idl/SALOME_LogManager.idl | 1 + src/Basics/PythonCppUtils.hxx | 13 +++++++--- src/Container/Container_i.cxx | 7 +++++- src/Container/SALOME_Container.py | 5 ++++ src/Launcher/SALOME_LogManager.cxx | 39 ++++++++++++++++++++++++------ src/Launcher/SALOME_LogManager.hxx | 11 ++++++--- 6 files changed, 62 insertions(+), 14 deletions(-) diff --git a/idl/SALOME_LogManager.idl b/idl/SALOME_LogManager.idl index d7ef490d4..da941f112 100644 --- a/idl/SALOME_LogManager.idl +++ b/idl/SALOME_LogManager.idl @@ -49,6 +49,7 @@ module Engines string getContainerEntryInNS(); ContainerScriptPerfLog addScript(in string name, in string code); ListOfContainerScriptPerfLog listOfScripts(); + void destroy(); }; typedef sequence ListOfContainerPerfLog; diff --git a/src/Basics/PythonCppUtils.hxx b/src/Basics/PythonCppUtils.hxx index 6330ae5ed..905005566 100644 --- a/src/Basics/PythonCppUtils.hxx +++ b/src/Basics/PythonCppUtils.hxx @@ -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(); } +}; diff --git a/src/Container/Container_i.cxx b/src/Container/Container_i.cxx index 249e98473..872e161ab 100644 --- a/src/Container/Container_i.cxx +++ b/src/Container/Container_i.cxx @@ -117,7 +117,7 @@ std::map Abstract_Engines_Container_i::_library_map; std::map 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..."); diff --git a/src/Container/SALOME_Container.py b/src/Container/SALOME_Container.py index 096385d94..b4639588c 100644 --- a/src/Container/SALOME_Container.py +++ b/src/Container/SALOME_Container.py @@ -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(): diff --git a/src/Launcher/SALOME_LogManager.cxx b/src/Launcher/SALOME_LogManager.cxx index 6e106965e..f336d56a2 100644 --- a/src/Launcher/SALOME_LogManager.cxx +++ b/src/Launcher/SALOME_LogManager.cxx @@ -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(serv); + if( servCand==child ) + { + _containers.erase( cont ); + break; + } + } +} + /////////////////////// #include diff --git a/src/Launcher/SALOME_LogManager.hxx b/src/Launcher/SALOME_LogManager.hxx index 89735aa3f..44948e4aa 100644 --- a/src/Launcher/SALOME_LogManager.hxx +++ b/src/Launcher/SALOME_LogManager.hxx @@ -70,7 +70,7 @@ public: AutoPyRef end(); void clear() { _data.clear(); } private: - AutoPyRef _pyExecutionLog; + AutoPyRefGilSafe _pyExecutionLog; SALOME_ContainerScriptPerfLog *_father = nullptr; std::vector _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: -- 2.39.2