GoogleCloudPlatform / cloud-profiler-python

Stackdriver Profiler Python agent is a tool that continuously gathers CPU usage information from Python applications
Apache License 2.0
27 stars 23 forks source link

fix: update code for breaking changes in Python 3.11 #131

Closed copybara-service[bot] closed 1 year ago

copybara-service[bot] commented 1 year ago

fix: update code for breaking changes in Python 3.11

Fixes #127. Struct members were hidden from PyFrameObject and PyThreadState. See https://docs.python.org/3/whatsnew/3.11.html#pyframeobject-3-11-hiding. This PR is passing public and internal tests.

However, I am not particularly confident that this fix is safe/correct. The handler operates possibly without the GIL, as it does not currently modify any of the PyObjects it works with. It gets the thread local ThreadState with PyGILState_GetThisThreadState().

The problem is that the getters described in the changelog (PyThreadState_GetFrame(), PyFrame_GetCode(), PyFrame_GetBack()) increment the refcount for those PyObjects which afaict is NOT safe to do without holding the GIL. Trying to acquire the GIL in the handler seems to deadlock some of the internal integration tests; I'm also not confident that this would be async-signal-safe.

Folks are welcome to try the profiler in Python 3.11 from this PR but I am not comfortable merging it as is. If you do try it, please report back on if you encountered any issues.

google-cla[bot] commented 1 year ago

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

aabmass commented 1 year ago

Closing in favor of #137