Skip to content

gh-128421: Add locking to most frame object functions #131479

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

Merged
merged 6 commits into from
Mar 21, 2025

Conversation

colesbury
Copy link
Contributor

@colesbury colesbury commented Mar 19, 2025

This makes more operations on frame objects thread-safe in the free threaded build, which fixes some data races that occurred when passing exceptions between threads.

However, accessing local variables or the line number of a frame from another thread while its still executing is not thread-safe and may crash the interpreter.

This makes more operations on frame objects thread-safe in the free
threaded build, which fixes some data races that occurred when passing
exceptions between threads.

However, accessing local variables from another thread while its running
is still not thread-safe and may crash the interpreter.
@colesbury colesbury marked this pull request as ready for review March 19, 2025 21:20
@colesbury colesbury requested a review from markshannon as a code owner March 19, 2025 21:20
@colesbury
Copy link
Contributor Author

I added the additional critical sections, although I'm pretty ambivalent about having them in the C APIs.

Copy link
Contributor

@kumaraditya303 kumaraditya303 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@colesbury colesbury merged commit 4f32516 into python:main Mar 21, 2025
45 checks passed
@colesbury colesbury deleted the gh-128421-frame branch March 21, 2025 15:10
colesbury added a commit to colesbury/cpython that referenced this pull request Mar 25, 2025
The recent PR pythongh-131479 added locking to `take_ownership` in the free
threading build. The cost is not really the locking -- that path isn't
taken frequently -- but the inlined code causes extra register spills
and slows down RETURN_VALUE, even when it's not taken.

Mark `take_ownership` as `Py_NO_INLINE` to avoid the regression.
colesbury added a commit to colesbury/cpython that referenced this pull request Mar 25, 2025
The recent PR pythongh-131479 added locking to `take_ownership` in the free
threading build. The cost is not really the locking -- that path isn't
taken frequently -- but the inlined code causes extra register spills
and slows down RETURN_VALUE, even when it's not taken.

Mark `take_ownership` as `Py_NO_INLINE` to avoid the regression.

Also limit locking in PyFrameObject to Python functions, not the C API.
colesbury added a commit to colesbury/cpython that referenced this pull request Mar 27, 2025
The recent PR pythongh-131479 added locking to `take_ownership` in the free
threading build. The cost is not really the locking -- that path isn't
taken frequently -- but the inlined code causes extra register spills
and slows down RETURN_VALUE, even when it's not taken.

Mark `take_ownership` as `Py_NO_INLINE` to avoid the regression.

Also limit locking in PyFrameObject to Python functions, not the C API.
PyFrame_GetCode is called frequently by coverage and tracing tools.
seehwan pushed a commit to seehwan/cpython that referenced this pull request Apr 16, 2025
…-131479)

This makes more operations on frame objects thread-safe in the free
threaded build, which fixes some data races that occurred when passing
exceptions between threads.

However, accessing local variables from another thread while its running
is still not thread-safe and may crash the interpreter.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants