Repository navigation
gh-153531: fix thread safety of setting func.__doc__ and func.__module__ - #154851
Conversation
|
Thanks @kumaraditya303 for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15. |
|
GH-154856 is a backport of this pull request to the 3.15 branch. |
|
GH-154857 is a backport of this pull request to the 3.14 branch. |
|
|
|
The Android failure was another instance of #137756; it should be fixed now. |
…gh-159269) Setting __name__, __qualname__, __doc__ or __module__ on a function stopped the world in the free-threaded build since gh-154826 and gh-154851, making functools.wraps() orders of magnitude slower with multiple threads. Use the per-object critical section and _PyObject_XSetRefDelayed() instead, as gen_set_name() already does. Use acquire loads in the readers and hold strong references across error reporting, tracing, and eval-frame recording that may execute Python or detach the thread. The __code__, __defaults__, __kwdefaults__ and __closure__ setters still stop the world to clear func_version. Document synchronization requirements for direct field access and the lifetime of borrowed function attributes.
…me attributes (pythongh-159269) Setting __name__, __qualname__, __doc__ or __module__ on a function stopped the world in the free-threaded build since pythongh-154826 and pythongh-154851, making functools.wraps() orders of magnitude slower with multiple threads. Use the per-object critical section and _PyObject_XSetRefDelayed() instead, as gen_set_name() already does. Use acquire loads in the readers and hold strong references across error reporting, tracing, and eval-frame recording that may execute Python or detach the thread. The __code__, __defaults__, __kwdefaults__ and __closure__ setters still stop the world to clear func_version. Document synchronization requirements for direct field access and the lifetime of borrowed function attributes. (cherry picked from commit 010bebb) Co-authored-by: Neil Schemenauer <nas-github@arctrix.com>
Fixes thread safety of setting
func.__doc__andfunc.__module__with stop the world pause as there are C-API functions which return borrowed refs to the attributes. Also remove stw fromfunc.__annotate__which was already safe with critical section and there are no C-API functions to access them.sentinel()construction racesfunc.__module__reassignment incaller()under free-threading #153531