-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[BUG]: Missing calls from a cProfile profile when calling into a pybind11 extension #5600
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
Comments
ChatGPT ideas: 🧪 Possible directions to investigate: Python 3.12 introduced a lot of changes to the interpreter internals, including optimizations in frame evaluation (PEP 659, PEP 523 hooks, etc.). If pybind11 doesn’t cooperate cleanly with these changes, it could mess with the profiler. pybind11 behavior: pybind11 generates trampoline functions that get invoked from Python. If the C++ lambda is inlined or not instrumented properly, cProfile might just "skip" over it. If the transition to native code is not properly tracked, then the profiler might not resume correctly after the C++ call. Profiler hooks and compiled code: cProfile is written in C and relies on Python's internal frame evaluation hooks. Calling C++ code via pybind11 bypasses Python bytecode execution, so the profiler may not “see” into the C++ part, but it should still see everything leading up to it. 🔍 Recommendations Use sys.setprofile() to set a manual profile hook—see if it gets the same behavior. Try a more direct profiler like Py-Spy or Linux perf (Ralf mentioned this). Consider opening an issue on the pybind11 or CPython bug trackers if you can make a minimal reproducible example. |
Some extra information only, no conclusion(s). I update this link in ChatGPT: In the update I pointed out this code to ChatGPT:
Note that this code was changed recently by PR #5580. — I do not know if that matters for this bug. In the updated ChatGPT conversation, look for How this might break in Python 3.12 |
Required prerequisites
What version (or hash if on master) of pybind11 are you using?
tried with both 2.13.6 and master
Problem description
Consider the following Python code using cProfile:
where
ext
is the following pybind11 extension:Running this under Python 3.12.8 produces the following output:
Notably, the resulting profile doesn't include anything about the Python call to
call_me()
, nor the 10 calls to the pybind11call_me()
method on theobj
instance. We can see from the "hello" prints in the output that the method was indeed called.Interestingly, when changing the call inside the loop to
ext.return_zero()
instead, the profile produced is as expected:Using Python 3.10 instead of 3.12, the second profile is identical, while the first one is still missing some calls, but it's not missing the initial Python
call_me()
call:I think this is likely pybind11-specific, because I tried reproducing this with an equivalent C++ extension using hand crafted C-API, and couldn't observe the issue.
Reproducible example code
Is this a regression? Put the last known working version here if it is.
Not a regression
The text was updated successfully, but these errors were encountered: