Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed GIL release issue with Python System and Python TestFixture. #2618

Merged
merged 13 commits into from
Feb 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions python/test/gravity.sdf
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<mass>1.0</mass>
</inertial>
</link>
<plugin filename="gz-sim-python-system-loader-system"
name="gz::sim::systems::PythonSystemLoader">
<module_name>plugins.test_model_system</module_name>
</plugin>
</model>

</world>
</sdf>
8 changes: 8 additions & 0 deletions src/systems/python_system_loader/PythonSystemLoader.cc
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think need more py::gil_scoped_acquire gil; calls in PythonSystemLoader::Configure, PythonSystemLoader::Reset(), and PythonSystemLoader::~PythonSystemLoader(). Basically, all System interfaces since they will be accessing python state.

Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ PythonSystemLoader::~PythonSystemLoader()
{
if (this->pythonSystem)
{
py::gil_scoped_acquire gil;
if (py::hasattr(this->pythonSystem, "shutdown"))
{
this->pythonSystem.attr("shutdown")();
Expand All @@ -54,6 +55,7 @@ void PythonSystemLoader::Configure(
const Entity &_entity, const std::shared_ptr<const sdf::Element> &_sdf,
EntityComponentManager &_ecm, EventManager &_eventMgr)
{
py::gil_scoped_acquire gil;
auto [moduleName, hasModule] = _sdf->Get<std::string>("module_name", "");
if (!hasModule)
{
Expand Down Expand Up @@ -194,13 +196,18 @@ void PythonSystemLoader::CallPythonMethod(py::object _method, Args &&..._args)
void PythonSystemLoader::PreUpdate(const UpdateInfo &_info,
EntityComponentManager &_ecm)
{
// Add explicit scoped acquire and release of GIL, so that Python
// Systems can be executed.This acquire and release is only required
// from the PythonSystem code
py::gil_scoped_acquire gil;
CallPythonMethod(this->preUpdateMethod, _info, &_ecm);
}

//////////////////////////////////////////////////
void PythonSystemLoader::Update(const UpdateInfo &_info,
EntityComponentManager &_ecm)
{
py::gil_scoped_acquire gil;
CallPythonMethod(this->updateMethod, _info, &_ecm);
}

Expand All @@ -215,6 +222,7 @@ void PythonSystemLoader::PostUpdate(const UpdateInfo &_info,
void PythonSystemLoader::Reset(const UpdateInfo &_info,
EntityComponentManager &_ecm)
{
py::gil_scoped_acquire gil;
CallPythonMethod(this->resetMethod, _info, &_ecm);
}

Expand Down