Skip to content

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

Closed
copybara-service[bot] wants to merge 1 commit into
mainfrom
test_499370977
Closed

fix: update code for breaking changes in Python 3.11#131
copybara-service[bot] wants to merge 1 commit into
mainfrom
test_499370977

Conversation

@copybara-service
Copy link
Copy Markdown

@copybara-service copybara-service Bot commented Jan 4, 2023

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
Copy link
Copy Markdown

google-cla Bot commented Jan 4, 2023

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.

@copybara-service copybara-service Bot force-pushed the test_499370977 branch 4 times, most recently from d3ac149 to a9d7d60 Compare January 5, 2023 20:45
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()`.
- `PyCodeObject`s may be GC'ed while the handler is running, but we get around that by patching the PyCode deallocator function [`PyCode_Type.tp_dealloc = &CodeDealloc`](https://github.com/GoogleCloudPlatform/cloud-profiler-python/blob/ae0c31b7ed962c8158e8a24ba44be6442c7374f8/googlecloudprofiler/src/profiler.h#L60)

The problem is that the getters described in [the changelog](https://docs.python.org/3/whatsnew/3.11.html#pyframeobject-3-11-hiding) (`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](https://man7.org/linux/man-pages/man7/signal-safety.7.html).

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.

PiperOrigin-RevId: 499370977
@aabmass
Copy link
Copy Markdown
Collaborator

aabmass commented Aug 10, 2023

Closing in favor of #137

@aabmass aabmass closed this Aug 10, 2023
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.

Cannot compile with Python 3.11

1 participant