Skip to content

Commit 4df5170

Browse files
AmalDevHaridevanahcordeazeey
authored
Fixed GIL release issue with Python System and Python TestFixture. (#2618)
--------- Signed-off-by: sdcnlab <haridevanamaldev@gmail.com> Signed-off-by: Amal Dev Haridevan <haridevanamaldev@gmail.com> Signed-off-by: AmaldevHaridevan <160554626+AmalDevHaridevan@users.noreply.github.com> Co-authored-by: Alejandro Hernández Cordero <ahcorde@gmail.com> Co-authored-by: Addisu Z. Taddese <addisu@openrobotics.org>
1 parent 036515f commit 4df5170

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

python/test/gravity.sdf

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
<mass>1.0</mass>
1818
</inertial>
1919
</link>
20+
<plugin filename="gz-sim-python-system-loader-system"
21+
name="gz::sim::systems::PythonSystemLoader">
22+
<module_name>plugins.test_model_system</module_name>
23+
</plugin>
2024
</model>
25+
2126
</world>
2227
</sdf>

src/systems/python_system_loader/PythonSystemLoader.cc

+8
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ PythonSystemLoader::~PythonSystemLoader()
4343
{
4444
if (this->pythonSystem)
4545
{
46+
py::gil_scoped_acquire gil;
4647
if (py::hasattr(this->pythonSystem, "shutdown"))
4748
{
4849
this->pythonSystem.attr("shutdown")();
@@ -54,6 +55,7 @@ void PythonSystemLoader::Configure(
5455
const Entity &_entity, const std::shared_ptr<const sdf::Element> &_sdf,
5556
EntityComponentManager &_ecm, EventManager &_eventMgr)
5657
{
58+
py::gil_scoped_acquire gil;
5759
auto [moduleName, hasModule] = _sdf->Get<std::string>("module_name", "");
5860
if (!hasModule)
5961
{
@@ -194,13 +196,18 @@ void PythonSystemLoader::CallPythonMethod(py::object _method, Args &&..._args)
194196
void PythonSystemLoader::PreUpdate(const UpdateInfo &_info,
195197
EntityComponentManager &_ecm)
196198
{
199+
// Add explicit scoped acquire and release of GIL, so that Python
200+
// Systems can be executed.This acquire and release is only required
201+
// from the PythonSystem code
202+
py::gil_scoped_acquire gil;
197203
CallPythonMethod(this->preUpdateMethod, _info, &_ecm);
198204
}
199205

200206
//////////////////////////////////////////////////
201207
void PythonSystemLoader::Update(const UpdateInfo &_info,
202208
EntityComponentManager &_ecm)
203209
{
210+
py::gil_scoped_acquire gil;
204211
CallPythonMethod(this->updateMethod, _info, &_ecm);
205212
}
206213

@@ -215,6 +222,7 @@ void PythonSystemLoader::PostUpdate(const UpdateInfo &_info,
215222
void PythonSystemLoader::Reset(const UpdateInfo &_info,
216223
EntityComponentManager &_ecm)
217224
{
225+
py::gil_scoped_acquire gil;
218226
CallPythonMethod(this->resetMethod, _info, &_ecm);
219227
}
220228

0 commit comments

Comments
 (0)