Skip to content

fix usage of Python GIL to make thread safe#60

Open
Vipul-Cariappa wants to merge 1 commit intowlav:masterfrom
Vipul-Cariappa:wlav/dev/GIL
Open

fix usage of Python GIL to make thread safe#60
Vipul-Cariappa wants to merge 1 commit intowlav:masterfrom
Vipul-Cariappa:wlav/dev/GIL

Conversation

@Vipul-Cariappa
Copy link

reference: root-project/root#19436
fixes/closes: wlav/cppyy#310

@wlav
Copy link
Owner

wlav commented Jul 23, 2025

The DispatchPtr is only used internally from C++ by the Dispatcher. I wonder whether it wouldn't be more efficient to put the protections around its use rather than inside its methods.

void CPyCppyy::DispatchPtr::CppOwns()
{
// C++ maintains the hardref, keeping the PyObject alive w/o outstanding ref
PyGILState_STATE state = PyGILState_Ensure();
Copy link
Author

Choose a reason for hiding this comment

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

I forgot to wrap CPyCppyy::DispatchPtr::PythonOwns around the GIL.

@Vipul-Cariappa
Copy link
Author

This is used in multiple places, other than the Dispatcher itself. Example:

void CPyCppyy::CPPInstance::PythonOwns()
{
fFlags |= kIsOwner;
if ((fFlags & kIsExtended) && DISPATCHPTR(this))
DISPATCHPTR(this)->PythonOwns();
}
//----------------------------------------------------------------------------
void CPyCppyy::CPPInstance::CppOwns()
{
fFlags &= ~kIsOwner;
if ((fFlags & kIsExtended) && DISPATCHPTR(this))
DISPATCHPTR(this)->CppOwns();
}

This implementation is simple, else we will need to search for all of the usages and wrap them around the GIL.
I guess in the above example, the GIL is held anyway.

Let me know your opinion. I can make modifications accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants