FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-128714: Fix function object races in FT build by xuantengh · Pull Request #129016 · python/cpython · GitHub

/ cpython Public

gh-128714: Fix function object races in FT build - #129016

Merged
kumaraditya303 merged 8 commits into
python:mainfrom
xuantengh:ft-annotations
Feb 6, 2025
Merged

gh-128714: Fix function object races in FT build#129016
kumaraditya303 merged 8 commits into
python:mainfrom
xuantengh:ft-annotations

Conversation

xuantengh commented Jan 19, 2025
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Contributor

This PR aims to fix the potential data races when getting and setting funcobject __annotations__. And it also add the test to validate the consistency under concurrent reads and writes.

StanFromIreland commented Jan 19, 2025
edited
Loading

Copy link
Copy Markdown
Member

This PR needs a NEWS entry.

Can a triager please add the interpreter-core label.

picnixz commented Jan 19, 2025

Copy link
Copy Markdown
Member

Can a triager please add the interpreter-core label.

Labels such as these are usually added on the issues rather than the PR.

Copy link
Copy Markdown
Member

@picnixz My bad.

Comment thread Objects/funcobject.c

Copy link
Copy Markdown
Member

There seem to be similar races on all other settable attributes on function objects, so it probably makes sense to fix them all at once.

Copy link
Copy Markdown
Contributor Author

There seem to be similar races on all other settable attributes on function objects, so it probably makes sense to fix them all at once.

Agree. I'll try to fix them.

xuantengh changed the title gh-128714: Fix func.__annotations__ races in FT build gh-128714: Fix function object races in FT build Jan 21, 2025
kumaraditya303 self-assigned this Jan 21, 2025

ZeroIntensity left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I suspect some of these fields (e.g. func_code) are also accessed by the eval loop or other functions without the Python-level getter, and therefore without the lock. Can we look into finding those cases?

kumaraditya303 commented Jan 27, 2025
edited
Loading

Copy link
Copy Markdown
Contributor

I think it's fine to just fix getters and setters of function in this PR. Fixing it for interp will require more massive changes which can be done separately if needed.

Comment thread Objects/funcobject.c Outdated
static PyGetSetDef func_getsetlist[] = {
FUNCTION___CODE___GETSETDEF
{"__defaults__", func_get_defaults, func_set_defaults},
{"__kwdefaults__", func_get_kwdefaults, func_set_kwdefaults},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Please add critical sections to all other ones as well

bedevere-app Bot commented Jan 27, 2025

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

kumaraditya303 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Please reduce the diff, the getters and setters doesn't need to be moved, you should add @critical_section where it was already defined.

xuantengh commented Jan 31, 2025
edited
Loading

Copy link
Copy Markdown
Contributor Author

There are public C APIs like PyFunction_GetCode and PyFunction_GetGlobals which access only one field in function object. Should we use atomic load in these APIs?

Copy link
Copy Markdown
Contributor Author

I have made the requested changes; please review again.

bedevere-app Bot commented Jan 31, 2025

Copy link
Copy Markdown

Thanks for making the requested changes!

@kumaraditya303: please review the changes made to this pull request.

bedevere-app Bot requested a review from kumaraditya303 January 31, 2025 11:53

Copy link
Copy Markdown
Contributor

There are public C APIs like PyFunction_GetCode and PyFunction_GetGlobals which access only one field in function object. Should we use atomic load in these APIs?

They should use also critical sections, not atomics.

Comment thread Objects/funcobject.c

colesbury left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Thanks again for taking this on @xuantengh.

We do not want to add locking for most of the PyFunctionObject attributes. In particular, we do not want locking for anything that will be accessed during function calls, like func_code, func_builtins, func_defaults, etc. both because it will affect single-threaded performance and because it will inhibit efficient multithreaded scaling.

Locking is probably fine for func_annotations, func_annotate, and func_typeparams. We should limit this PR to those fields.

We have a few general strategies for thread-safe accesses:

  1. Locking around both reading and writing the field.
  2. Locking around writers, and something like _Py_TryXGetRef for readers (a non-locking fast-path)
  3. Locking around writers with some sort of delayed deallocation (like QSBR). Readers do not use locks, but still need atomic loads.
  4. Stop-the-world around writers and no synchronization for readers

We will probably want to use either (3) or (4) for fields like func_code, but I want to be careful about making those changes and don't think we should pursue those as part of this PR.

Copy link
Copy Markdown
Contributor Author

Locking is probably fine for func_annotations, func_annotate, and func_typeparams. We should limit this PR to those fields.

I've reverted the PR to change only __annotate__, __annotations__ and __type_params. And now the PR only changes attributes which are not performance critical.

corona10 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

lgtm

Comment thread Objects/funcobject.c Outdated
Comment thread Objects/funcobject.c Outdated
kumaraditya303 merged commit 55f17b7 into python:main Feb 6, 2025

Copy link
Copy Markdown

Thanks @xuantengh for the PR, and @kumaraditya303 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

Copy link
Copy Markdown

Sorry, @xuantengh and @kumaraditya303, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 55f17b77c305be877ac856d6426b13591cbc7fc8 3.13

xuantengh deleted the ft-annotations branch February 6, 2025 14:50
xuantengh added a commit to xuantengh/cpython that referenced this pull request Feb 6, 2025
…tations__` and `__type_params__` in free-threading build (python#129016)

(cherry picked from commit 55f17b7)

bedevere-app Bot commented Feb 6, 2025

Copy link
Copy Markdown

GH-129729 is a backport of this pull request to the 3.13 branch.

bedevere-app Bot removed the needs backport to 3.13 bugs and security fixes label Feb 6, 2025
srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this pull request Feb 7, 2025
…tations__` and `__type_params__` in free-threading build (python#129016)
cmaloney pushed a commit to cmaloney/cpython that referenced this pull request Feb 8, 2025
…tations__` and `__type_params__` in free-threading build (python#129016)
terryjreedy pushed a commit that referenced this pull request Feb 24, 2025
…otations__` and `__type_params__` in free-threading build (GH-129016) (#129729)

* gh-128714: Fix function object races in `__annotate__`, `__annotations__` and `__type_params__` in  free-threading build (#129016)

(cherry picked from commit 55f17b7)

---------

Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

8 participants


Back | FazBrowse Home | New Git URL