-
Notifications
You must be signed in to change notification settings - Fork 302
Fix for deadlock in python callback #3073
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
base: master
Are you sure you want to change the base?
Fix for deadlock in python callback #3073
Conversation
4b7cd4f to
6d3fa90
Compare
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.
Pull request overview
This PR fixes a deadlock issue in Python callbacks by ensuring proper GIL (Global Interpreter Lock) management during callback lifecycle. The fix addresses the problem of Python objects being destroyed without holding the GIL, which can cause deadlocks in multi-threaded scenarios.
Key changes:
- Updated callback signature to use
py::strinstead ofstd::stringfor better Python integration - Added custom deleters to shared pointers that acquire GIL before destroying Python objects
- Improved error handling in UTF-8 string decoding with null pointer checks
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/python/py_utils.hpp | Changed callback signature from std::string to py::str parameter |
| src/python/py_utils.cpp | Added GIL-aware custom deleters for Python callbacks and improved UTF-8 decoding error handling |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/python/py_utils.cpp
Outdated
| if (Py_IsInitialized()) { | ||
| PyGILState_STATE gstate = PyGILState_Ensure(); | ||
| delete f; | ||
| PyGILState_Release(gstate); |
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.
What are the results of running tests without acquired GIL? Do the really fail and this code is required?
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.
Yes, if GIL is not there it means we have undefined behaviour, it fails from time to time.
src/python/py_utils.cpp
Outdated
| if (Py_IsInitialized()) { | ||
| PyGILState_STATE gstate = PyGILState_Ensure(); | ||
| delete f; | ||
| PyGILState_Release(gstate); |
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.
Why not gil_scoped_acquire?
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.
Replaced it with gil_scoped_acquire - I've tried using C-api to check how it works. But looks like it doesn't make any difference.
c83d010 to
4ffbb64
Compare
4ffbb64 to
06d0693
Compare
Fix update from release branch