Skip to content

Commit 33ca702

Browse files
Added gil acquire to Configure, Destructor, and Reset methods. Also used the TestModelSystem plugin for testing
Signed-off-by: Amal Dev Haridevan <[email protected]>
1 parent b5f7862 commit 33ca702

File tree

3 files changed

+4
-15
lines changed

3 files changed

+4
-15
lines changed

python/src/gz/sim/TestFixture.cc

-11
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,7 @@ defineSimTestFixture(pybind11::object module)
5656
[](TestFixture* self, std::function<void(
5757
const UpdateInfo &, EntityComponentManager &)> _cb)
5858
{
59-
// Add explicit scoped acquire and release of GIL, so that Python Systems
60-
// can be executed
61-
// This acquire and release is only required from the PythonSystem code
62-
// However, adding this here may prevent undefined or unintended behaviors
63-
// in future
64-
pybind11::gil_scoped_acquire gil;
6559
self->OnPreUpdate(_cb);
66-
pybind11::gil_scoped_release gilr;
6760
}
6861
),
6962
pybind11::return_value_policy::reference,
@@ -74,9 +67,7 @@ defineSimTestFixture(pybind11::object module)
7467
[](TestFixture* self, std::function<void(
7568
const UpdateInfo &, EntityComponentManager &)> _cb)
7669
{
77-
pybind11::gil_scoped_acquire gil;
7870
self->OnUpdate(_cb);
79-
pybind11::gil_scoped_release gilr;
8071
}
8172
),
8273
pybind11::return_value_policy::reference,
@@ -87,9 +78,7 @@ defineSimTestFixture(pybind11::object module)
8778
[](TestFixture* self, std::function<void(
8879
const UpdateInfo &, const EntityComponentManager &)> _cb)
8980
{
90-
pybind11::gil_scoped_acquire gil;
9181
self->OnPostUpdate(_cb);
92-
pybind11::gil_scoped_release gilr;
9382
}
9483
),
9584
pybind11::return_value_policy::reference,

python/test/gravity.sdf

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
</link>
2020
<plugin filename="gz-sim-python-system-loader-system"
2121
name="gz::sim::systems::PythonSystemLoader">
22-
<module_name>python_system_TEST</module_name>
22+
<module_name>plugins.test_model_system</module_name>
2323
</plugin>
2424
</model>
2525

src/systems/python_system_loader/PythonSystemLoader.cc

+3-3
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
{
@@ -199,7 +201,6 @@ void PythonSystemLoader::PreUpdate(const UpdateInfo &_info,
199201
// from the PythonSystem code
200202
py::gil_scoped_acquire gil;
201203
CallPythonMethod(this->preUpdateMethod, _info, &_ecm);
202-
py::gil_scoped_release gilr;
203204
}
204205

205206
//////////////////////////////////////////////////
@@ -208,7 +209,6 @@ void PythonSystemLoader::Update(const UpdateInfo &_info,
208209
{
209210
py::gil_scoped_acquire gil;
210211
CallPythonMethod(this->updateMethod, _info, &_ecm);
211-
py::gil_scoped_release gilr;
212212
}
213213

214214
//////////////////////////////////////////////////
@@ -217,12 +217,12 @@ void PythonSystemLoader::PostUpdate(const UpdateInfo &_info,
217217
{
218218
py::gil_scoped_acquire gil;
219219
CallPythonMethod(this->postUpdateMethod, _info, &_ecm);
220-
py::gil_scoped_release gilr;
221220
}
222221
//////////////////////////////////////////////////
223222
void PythonSystemLoader::Reset(const UpdateInfo &_info,
224223
EntityComponentManager &_ecm)
225224
{
225+
py::gil_scoped_acquire gil;
226226
CallPythonMethod(this->resetMethod, _info, &_ecm);
227227
}
228228

0 commit comments

Comments
 (0)