Skip to content

Commit 4b0ce86

Browse files
authored
Merge pull request #432 from fredroy/test_nonstatic_init
[Bindings] Remove static SofaInitializer, call unload into each respective module and clear cache
2 parents bb0a7a7 + 88bda2d commit 4b0ce86

9 files changed

Lines changed: 57 additions & 31 deletions

File tree

.github/workflows/ci.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,12 @@ jobs:
106106
echo "$WORKSPACE_ARTIFACT_PATH/lib" >> $GITHUB_PATH
107107
echo "$WORKSPACE_ARTIFACT_PATH/bin" >> $GITHUB_PATH
108108
elif [[ "$RUNNER_OS" == "macOS" ]]; then
109+
echo "SOFA_PLUGIN_PATH=$WORKSPACE_ARTIFACT_PATH/lib" | tee -a $GITHUB_ENV
109110
echo "DYLD_LIBRARY_PATH=$WORKSPACE_ARTIFACT_PATH/lib:$SOFA_ROOT/lib:$DYLD_LIBRARY_PATH" | tee -a $GITHUB_ENV
111+
else # Linux
112+
echo "SOFA_PLUGIN_PATH=$WORKSPACE_ARTIFACT_PATH/lib" | tee -a $GITHUB_ENV
113+
echo "LD_LIBRARY_PATH=$WORKSPACE_ARTIFACT_PATH/lib:$SOFA_ROOT/lib:$LD_LIBRARY_PATH" | tee -a $GITHUB_ENV
110114
fi
111-
echo "LD_LIBRARY_PATH=$WORKSPACE_ARTIFACT_PATH/lib:$SOFA_ROOT/lib:$LD_LIBRARY_PATH" | tee -a $GITHUB_ENV
112115
echo "PYTHONPATH=$WORKSPACE_ARTIFACT_PATH/lib/python3/site-packages" | tee -a $GITHUB_ENV
113116
# Add execution right on the tests
114117
chmod +x $WORKSPACE_ARTIFACT_PATH/bin/*.Tests${{ steps.sofa.outputs.exe }}

Plugin/src/SofaPython3/DataHelper.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,17 @@ py::slice toSlice(const py::object& o)
249249

250250
std::map<void*, std::pair<int, py::array>>& getObjectCache()
251251
{
252-
static std::map<void*, std::pair<int, py::array>> s_objectcache {} ;
252+
static std::map<void*, std::pair<int, py::array>> s_objectcache{};
253253
return s_objectcache;
254254
}
255255

256+
257+
void clearCache()
258+
{
259+
msg_info("SofaPython3") << "Clearing Sofa.Core cache...";
260+
getObjectCache().clear();
261+
}
262+
256263
void trimCache()
257264
{
258265
auto& memcache = getObjectCache();

Plugin/src/SofaPython3/DataHelper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ SOFAPYTHON3_API std::string getPathTo(Base* b);
194194
SOFAPYTHON3_API const char* getFormat(const AbstractTypeInfo& nfo);
195195

196196
SOFAPYTHON3_API std::map<void*, std::pair<int, pybind11::array>>& getObjectCache();
197+
SOFAPYTHON3_API void clearCache();
197198
SOFAPYTHON3_API void trimCache();
198199

199200
SOFAPYTHON3_API bool hasArrayFor(BaseData* d);

Plugin/src/SofaPython3/PythonEnvironment.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,10 @@ void PythonEnvironment::Release()
324324
{
325325
/// Finish the Python Interpreter
326326
/// obviously can't use raii here
327-
if( Py_IsInitialized() ) {
327+
static bool isReleased = false;
328+
if( Py_IsInitialized() && !isReleased) {
329+
isReleased = true;
330+
328331
PyGILState_Ensure();
329332
py::finalize_interpreter();
330333
getStaticData()->reset();
@@ -480,6 +483,12 @@ void PythonEnvironment::addPluginManagerCallback()
480483
}
481484
}
482485
);
486+
PluginManager::getInstance().addOnPluginCleanupCallbacks(pluginLibraryPath,
487+
[]()
488+
{
489+
PythonEnvironment::Release();
490+
}
491+
);
483492
}
484493

485494
void PythonEnvironment::removePluginManagerCallback()

Plugin/src/SofaPython3/PythonEnvironment.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <vector>
2424
#include <string>
2525
#include <sofa/helper/logging/FileInfo.h>
26+
#include <SofaPython3/DataHelper.h>
2627

2728
/// Fixes compile errors:
2829
/// removing all slots macros is necessary if embedded in a Qt project

bindings/Sofa/src/SofaPython3/Sofa/Core/Submodule_Core.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ using sofa::helper::logging::Message;
5151
#include <SofaPython3/Sofa/Core/Data/Binding_DataVectorString.h>
5252
#include <SofaPython3/Sofa/Core/Data/Binding_DataContainer.h>
5353

54+
#include <sofa/core/init.h>
55+
5456
namespace sofapython3
5557
{
5658

@@ -130,6 +132,18 @@ PYBIND11_MODULE(Core, core)
130132
moduleAddBaseMeshTopology(core);
131133
moduleAddPointSetTopologyModifier(core);
132134
moduleAddTaskScheduler(core);
135+
136+
// called when the module is unloaded
137+
auto atexit = py::module_::import("atexit");
138+
atexit.attr("register")(py::cpp_function([]() {
139+
140+
clearCache();
141+
142+
sofa::core::cleanup();
143+
144+
msg_info("SofaPython3.Core") << "Sofa.Core unload()";
145+
}));
146+
133147
}
134148

135149
} ///namespace sofapython3

bindings/Sofa/src/SofaPython3/Sofa/Helper/Submodule_Helper.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
* Contact information: contact@sofa-framework.org *
1919
******************************************************************************/
2020

21-
#include <sofa/core/init.h>
2221
#include <sofa/helper/init.h>
2322
#include <sofa/helper/logging/Messaging.h>
2423
#include <SofaPython3/PythonEnvironment.h>
@@ -108,7 +107,6 @@ static void parse_emitter_message_then(py::args args, const Action& action) {
108107
PYBIND11_MODULE(Helper, helper)
109108
{
110109
// These are needed to force the dynamic loading of module dependencies (found in CMakeLists.txt)
111-
sofa::core::init();
112110
sofa::helper::init();
113111

114112
helper.doc() = R"doc(
@@ -155,6 +153,12 @@ PYBIND11_MODULE(Helper, helper)
155153
moduleAddMessageHandler(helper);
156154
moduleAddVector(helper);
157155
moduleAddSystem(helper);
156+
157+
auto atexit = py::module_::import("atexit");
158+
atexit.attr("register")(py::cpp_function([]() {
159+
sofa::helper::cleanup();
160+
msg_info("SofaPython3.Helper") << "Sofa.Helper unload()";
161+
}));
158162
}
159163

160164
} ///namespace sofapython3

bindings/Sofa/src/SofaPython3/Sofa/Simulation/Submodule_Simulation.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ using sofa::simulation::Simulation;
4242

4343
#include <sofa/core/init.h>
4444
#include <sofa/simulation/init.h>
45+
#include <sofa/simulation/common/init.h>
4546
#include <sofa/simulation/graph/init.h>
4647

4748
namespace py = pybind11;
@@ -83,6 +84,17 @@ PYBIND11_MODULE(Simulation, simulation)
8384
{
8485
sofa::simulation::node::initTextures(n);
8586
});
87+
88+
// called when the module is unloaded
89+
auto atexit = py::module_::import("atexit");
90+
atexit.attr("register")(py::cpp_function([]() {
91+
92+
sofa::simulation::core::cleanup();
93+
sofa::simulation::common::cleanup();
94+
sofa::simulation::graph::cleanup();
95+
96+
msg_info("SofaPython3.Simulation") << "Sofa.Simulation unload()";
97+
}));
8698
}
8799

88100
} /// namespace sofapython3

bindings/SofaRuntime/src/SofaPython3/SofaRuntime/Module_SofaRuntime.cpp

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -72,23 +72,6 @@ using sofa::helper::logging::MainConsoleMessageHandler;
7272
namespace sofapython3
7373
{
7474

75-
76-
class SofaInitializer
77-
{
78-
public:
79-
// TODO, ces trucs sont fort laid. Normalement ce devrait être une joli plugin qui
80-
// appelle le init.
81-
SofaInitializer(){
82-
sofa::simulation::common::init();
83-
sofa::simulation::graph::init();
84-
}
85-
86-
~SofaInitializer(){
87-
sofa::simulation::common::cleanup();
88-
sofa::simulation::graph::cleanup();
89-
}
90-
};
91-
9275
static std::vector<std::string> getCategories(const std::string& className)
9376
{
9477
std::vector<std::string> categories;
@@ -109,8 +92,6 @@ static std::vector<std::string> getCategories(const std::string& className)
10992
return categories ;
11093
}
11194

112-
static SofaInitializer s;
113-
11495
/// The first parameter must be named the same as the module file to load.
11596
PYBIND11_MODULE(SofaRuntime, m) {
11697

@@ -132,13 +113,6 @@ PYBIND11_MODULE(SofaRuntime, m) {
132113
133114
)doc";
134115

135-
// These are needed to force the dynamic loading of module dependencies (found in CMakeLists.txt)
136-
sofa::core::init();
137-
sofa::helper::init();
138-
sofa::simulation::core::init();
139-
sofa::simulation::graph::init();
140-
sofa::simulation::common::init();
141-
142116
// Add the plugin directory to PluginRepository
143117
const std::string& pluginDir = Utils::getExecutableDirectory();
144118
PluginRepository.addFirstPath(pluginDir);
@@ -198,6 +172,7 @@ PYBIND11_MODULE(SofaRuntime, m) {
198172
m.def("getCategories", &getCategories);
199173

200174
addSubmoduleTimer(m);
175+
201176
}
202177

203178
} // namespace sofapython3

0 commit comments

Comments
 (0)