-
Notifications
You must be signed in to change notification settings - Fork 289
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
Fixed GIL release issue with Python System and Python TestFixture. #2618
Conversation
d11629b
to
c436b61
Compare
…gned-off-by: Amal Dev Haridevan [email protected] Signed-off-by: sdcnlab <[email protected]> Signed-off-by: Amal Dev Haridevan <[email protected]>
c436b61
to
29598fd
Compare
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]>
@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? |
There was a problem hiding this 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
gz-sim/src/SimulationRunner.cc
Lines 69 to 77 in c72be04
// 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; |
There was a problem hiding this comment.
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.
python/test/python_system_TEST.py
Outdated
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
There was a problem hiding this comment.
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.
python/src/gz/sim/TestFixture.cc
Outdated
@@ -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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
…sed the TestModelSystem plugin for testing Signed-off-by: Amal Dev Haridevan <[email protected]>
Signed-off-by: Amal Dev Haridevan <[email protected]>
…ridevan/gz-sim into Amal-python-system-GIL
There was a problem hiding this 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 🙏🏾
…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
codecheck
passed (See contributing)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.