Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-145272: Fix data race when accessing func_code in PyFunctionObject#145534

Open
Krishna-web-hub wants to merge 3 commits intopython:mainfrom
Krishna-web-hub:New_fix_clean
Open

gh-145272: Fix data race when accessing func_code in PyFunctionObject#145534
Krishna-web-hub wants to merge 3 commits intopython:mainfrom
Krishna-web-hub:New_fix_clean

Conversation

@Krishna-web-hub
Copy link
Contributor

@Krishna-web-hubKrishna-web-hub commentedMar 5, 2026
edited
Loading

  • Fix a data race when accessing PyFunctionObject.func_code in free-threaded builds.

  • This patch replaces the direct read with_Py_atomic_load_ptr() and updates the setter to use_Py_atomic_exchange_ptr() so that updates tofunc_code are performed atomically.

  • The race was reproduced usingThreadSanitizer and verified to be fixed after this change.

Copy link
Member

@vstinnervstinner left a comment

Choose a reason for hiding this comment

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

Is this issue specific to func_code? What about otherPyFunction methods such as PyFunction_GET_GLOBALS() or PyFunction_GET_MODULE()?

@Krishna-web-hub
Copy link
ContributorAuthor

@vstinner actually this issue was mentioned only to fixation of the func_code. But the func_code have high chance of being reassigned at runtime. But others like func_globals and func_module are set at the time of the function creation. But there are few objects like func_annotations, func_defaults they can cause the race situation but i think applying the atomic pointer fix can slow down the cpython. Based on this i want to know your opinion on what i should be doing next ?

Comment on lines +634 to +635
PyCodeObject *code = _Py_atomic_load_ptr(&op->func_code);
return Py_NewRef(code);
Copy link
Contributor

Choose a reason for hiding this comment

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

This isn't right.

code might have been deallocated by the time you doPy_NewRef

@@ -0,0 +1 @@
Fixing race condition in PyFunctionObject.func_code
Copy link
Member

Choose a reason for hiding this comment

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

I think we should be less precise in terms of implementation and rather mention__func__.__code__ directly.

Copy link
Member

@picnixzpicnixz left a comment

Choose a reason for hiding this comment

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

But others like func_globals and func_module are set at the time of the function creation. But there are few objects like func_annotations, func_defaults they can cause the race situation but i think applying the atomic pointer fix can slow down the cpython

Regular build shouldn't be affected if add a Py_GIL_DISABLED guard. But I would indeed have a macro that atomically access attributes instead.

}

PyObject *func_code = PyFunction_GET_CODE(op);
PyCodeObject *func_code =(PyCodeObject *)PyFunction_GET_CODE(op);
Copy link
Member

Choose a reason for hiding this comment

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

Why this change?

return_PyFunction_CAST(func)->func_code;
PyFunctionObject*op=_PyFunction_CAST(func);
#ifdefPy_GIL_DISABLED
return (PyObject*)_Py_atomic_load_ptr(&op->func_code);
Copy link
Member

Choose a reason for hiding this comment

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

Aren'tall attributes of function objects subject to this issue then?

handle_func_event(PyFunction_EVENT_MODIFY_CODE, op, value);
_PyFunction_ClearVersion(op);
Py_XSETREF(op->func_code, Py_NewRef(value));
PyCodeObject *new = (PyCodeObject *)Py_NewRef(value);
Copy link
Member

Choose a reason for hiding this comment

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

This should be GIL_DISABLED-guarded

Copy link
Member

Choose a reason for hiding this comment

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

Also, don't we have a FT-helper alterantive to Py_XSETREF?

@bedevere-app
Copy link

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 phraseI 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.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@picnixzpicnixzpicnixz requested changes

@vstinnervstinnerAwaiting requested review from vstinner

+1 more reviewer

@da-woodsda-woodsda-woods left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

4 participants

@Krishna-web-hub@vstinner@da-woods@picnixz

[8]ページ先頭

©2009-2026 Movatter.jp