wlav / cppyy

Other
401 stars 41 forks source link

cppyy with context switches (aka coroutines) #27

Closed yanghao closed 2 years ago

yanghao commented 2 years ago

In a recent tryout to use cppyy with context swtich libraries (e.g. SystemC, which are using QuickThread) some random failures happen. One run could report segmentation fault, the next run could report cannot find a proper function for the give parameters, which in the segmentation fault case was successfully found.

Before I try to create a minimal example that reproduce the issue, just want to check quickly if cppyy need any special handling to work with those libraries? or maybe this is even a known issue? Thanks.

wlav commented 2 years ago

I'm not familiar with QuickThread and its readme is too minimal to come to any conclusions, but if (based solely on the name), it is some form of micro-threads, then there is the fact that cppyy works with boost_fiber, so presumably, whatever is causing these segfaults, it's solvable. There are, of course, the usual caveats: the Python GIL needs to be released to see any benefit and C++ exceptions can't cross threads, but that's true for any type of Python/C++ threading.

I'm thinking that that latter may be the issue here, based on your "cannot find a proper function" description. Point being that to get a Python exception through layers of C++, C++ exceptions are used. That whole stack has to remain on the same thread until the exception is handled (in either C++ or Python).

yanghao commented 2 years ago

It turns out this is not actually a cppyy issue. There are coner cases specifically related to how SystemC interacts with the cppyy-wrapped python side, especially the coroutine scheduling part's implementation seems relying on the calling stack and cannot be done directly from cppyy wrapped python code. Now after refacterizing those scheduling code into C++ code all works perfectly fine and cppyy does not reporting errors/warnings, thus closing the issue.

Thanks @wlav for the hints!