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

Conversation

AmalDevHaridevan
Copy link
Contributor

…gned-off-by: Amal Dev Haridevan [email protected]

🦟 Bug fix

Fixes #
Add explicit scoped acquire and release of GIL, so that Python Systems can be executed from a Python Server (run using TestFixture for example).

Summary

Python system plugin, which is attached to my model cannot be run using TestFixture in Python. This is due to GIL not being explicitly released in the PythonSystemLoader.
To overcome this issue, we need to do a scoped_acquire of GIL explicitly and then perform scoped_release after each of the system methods, namely, PreUpdate, Update, PostUpdate of the PythonSystem, so the GIL can be accessed by the TestFixture after.
As a safety mechanism, for future, I also added the scoped_acquire and release of GIL within the pybind code for TestFixture.

Checklist

  • Signed all commits for DCO
  • Added tests
  • Updated documentation (as needed)
  • Updated migration guide (as needed)
  • Consider updating Python bindings (if the library has them)
  • codecheck passed (See contributing)
  • All tests passed (See test coverage)
  • While waiting for a review on your PR, please help review another open pull request to support the maintainers

Note to maintainers: Remember to use Squash-Merge and edit the commit message to match the pull request summary while retaining Signed-off-by messages.

🔸🔸🔸🔸🔸🔸🔸🔸🔸🔸🔸🔸🔸🔸🔸🔸🔸🔸🔸🔸🔸🔸🔸🔸🔸🔸🔸🔸🔸🔸🔸🔸🔸🔸🔸🔸🔸

Note to maintainers: Remember to use Squash-Merge and edit the commit message to match the pull request summary while retaining Signed-off-by messages.

…gned-off-by: Amal Dev Haridevan [email protected]

Signed-off-by: sdcnlab <[email protected]>
Signed-off-by: Amal Dev Haridevan <[email protected]>
AmalDevHaridevan and others added 4 commits September 12, 2024 10:28
Co-authored-by: Alejandro Hernández Cordero <[email protected]>
Signed-off-by: AmaldevHaridevan <[email protected]>
Signed-off-by: AmaldevHaridevan <[email protected]>
Signed-off-by: AmaldevHaridevan <[email protected]>
@azeey
Copy link
Contributor

azeey commented Oct 14, 2024

@AmalDevHaridevan just wanted to give you a heads-up that I won't be able to review this for the next couple of weeks due to ROSCon.

@AmalDevHaridevan
Copy link
Contributor Author

@AmalDevHaridevan just wanted to give you a heads-up that I won't be able to review this for the next couple of weeks due to ROSCon.

Hi Azeey, any updates on the pull request?

Copy link
Contributor

@azeey azeey left a comment

Choose a reason for hiding this comment

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

Thanks for your patience. I can see the issue now and I can reproduce it with

from gz_test_deps.sim import Server, ServerConfig
from gz_test_deps.common import set_verbosity

set_verbosity(4)
config  = ServerConfig()
config.set_sdf_file('gravity.sdf')
server = Server(config)
server.run(True, 1000, False)

So it has nothing to do with TestFixture, but with how Server is being loaded from python. As mentioned in

// Helper struct to maybe do a scoped release of the Python GIL. There are a
// number of ways where releasing and acquiring the GIL is not necessary:
// 1. Gazebo is built without Pybind11
// 2. The python interpreter is not initialized. This could happen in tests that
// create a SimulationRunner without sim::Server where the interpreter is
// intialized.
// 3. sim::Server was instantiated by a Python module. In this case, there's a
// chance that this would be called with the GIL already released.
struct MaybeGilScopedRelease

When Server is instantiated from a python interpreter, pybind11 automatically releases the GIL before calling the C++ code (Server()). When PythonSystemLoader then tries to load the python system, it fails since any access to Python state requires locking the GIL. The solution you proposed works pretty well I think. My only comment is that we limit the changes to PythonSystemLoader and acquire the GIL in every System interface call.

CallPythonMethod(this->preUpdateMethod, _info, &_ecm);
py::gil_scoped_release gilr;
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not necessary since py::gil_scoped_acquire will release the gil when going out of scope.

Copy link
Contributor

Choose a reason for hiding this comment

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

Would it be possible to use the TestModelSystem plugin that's already in python/test/plugin?

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you remove this file then?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

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.

@@ -56,7 +56,14 @@ defineSimTestFixture(pybind11::object module)
[](TestFixture* self, std::function<void(
const UpdateInfo &, EntityComponentManager &)> _cb)
{
// Add explicit scoped acquire and release of GIL, so that Python Systems
Copy link
Contributor

Choose a reason for hiding this comment

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

If we apply the changes in PythonSystemLoader, we shouldn't need any change in this file. Can you revert?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi azeey,
I addressed all your comments on the latest commit.
Hope this can be merged now.

Copy link
Contributor

@azeey azeey left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution and for your patience 🙏🏾

@azeey azeey dismissed ahcorde’s stale review February 22, 2025 02:09

All comments have been addressed

@azeey azeey merged commit 4df5170 into gazebosim:gz-sim9 Feb 22, 2025
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏛️ ionic Gazebo Ionic
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants