Skip to content

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-hub Krishna-web-hub commented Mar 5, 2026

  • 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 to func_code are performed atomically.

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

Copy link
Member

@vstinner vstinner 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 other PyFunction methods such as PyFunction_GET_GLOBALS() or PyFunction_GET_MODULE()?

@Krishna-web-hub
Copy link
Contributor Author

@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 ?

@Krishna-web-hub Krishna-web-hub requested a review from vstinner March 7, 2026 07:11
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 do Py_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

@picnixz picnixz 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);
#ifdef Py_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't all 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

bedevere-app bot commented Mar 7, 2026

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.

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.

4 participants