Skip to content

Conversation

@lgritz
Copy link
Collaborator

@lgritz lgritz commented May 19, 2025

Only symptomatic on debug builds, but I was seeing exceptions from pybind11 about bad GIL release. I traced it to the recent implementations of demosaic. Apparently it's not safe to release the GIL until AFTER the call to py_to_stdvector. Which makes sense when you think about it -- we need the lock whenever we are interacting directly with Python calls or data structures (such as what py_to_stdvector is doing), but we release it when we're done with the python and only calling OIIO internals (such as IBA::demosaic() itself).

Only symptomatic on debug builds, but I was getting exceptions from
pybind11 about bad GIL release. I traced it to the recent
implementations of demosaic. Apparently it's not safe to release the
GIL until AFTER the call to py_to_stdvector. Which makes sense when
you think about it -- we need the lock whenever we are interacting
directly with Python calls or data structures (such as what
py_to_stdvector is doing), but we release it when we're done with the
python and only calling OIIO internals (such as IBA::demosaic()
itself).

Signed-off-by: Larry Gritz <[email protected]>
@lgritz
Copy link
Collaborator Author

lgritz commented May 19, 2025

I've only seen it symptomatic on my Mac laptop. We obviously run the testsuite in debug mode in CI, not sure why it wasn't showing up. Maybe only certain combos of python version + pybind11 version catch the unsafe behavior and throw an exception for it?

@EmilDohne
Copy link
Contributor

I'm curious why the GIL is released in the first place, it should only ever be released if you want to make the c++ function parallelizable from python (if e.g. you have c++ code to get something from a server) but seeing as demosaic will likely saturate all threads this shouldn't really be necessary.

@lgritz
Copy link
Collaborator Author

lgritz commented May 20, 2025

This allows any parallelization happening in the python program that's calling it to proceed in other threads and not get locked up during the long execution of the IBA function. It's not to parallelize this call, it's to allow other python threads to make progress during the expensive part of this operation.

@EmilDohne
Copy link
Contributor

Right I see, thanks for clearing up!

@lgritz lgritz merged commit f698ae3 into AcademySoftwareFoundation:main May 20, 2025
67 checks passed
lgritz added a commit to lgritz/OpenImageIO that referenced this pull request May 21, 2025
…areFoundation#4777)

Only symptomatic on debug builds, but I was seeing exceptions from
pybind11 about bad GIL release. I traced it to the recent
implementations of demosaic. Apparently it's not safe to release the GIL
until AFTER the call to py_to_stdvector. Which makes sense when you
think about it -- we need the lock whenever we are interacting directly
with Python calls or data structures (such as what py_to_stdvector is
doing), but we release it when we're done with the python and only
calling OIIO internals (such as IBA::demosaic() itself).

Signed-off-by: Larry Gritz <[email protected]>
@lgritz lgritz deleted the lg-gil branch May 21, 2025 05:17
zachlewis pushed a commit to zachlewis/OpenImageIO that referenced this pull request Aug 1, 2025
…areFoundation#4777)

Only symptomatic on debug builds, but I was seeing exceptions from
pybind11 about bad GIL release. I traced it to the recent
implementations of demosaic. Apparently it's not safe to release the GIL
until AFTER the call to py_to_stdvector. Which makes sense when you
think about it -- we need the lock whenever we are interacting directly
with Python calls or data structures (such as what py_to_stdvector is
doing), but we release it when we're done with the python and only
calling OIIO internals (such as IBA::demosaic() itself).

Signed-off-by: Larry Gritz <[email protected]>
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